ldc-cocotte/app/Providers/CrmApiServiceProvider.php

50 lines
1.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Providers;
use App\Auth\CrmUserProvider;
use App\Services\CrmService;
use Domain\Ports\Services\CrmCustomerGatewayService;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Cache\Repository;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Hashing\Hasher;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\ServiceProvider;
use LDC\LDCCRM\CrmApiV4;
use Webmozart\Assert\Assert;
class CrmApiServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
App::singleton(CrmService::class, fn (Application $app) => new CrmService(
$app->make(CrmApiV4::class),
$app->make(Repository::class),
));
App::bind(CrmCustomerGatewayService::class, fn (Application $app): CrmCustomerGatewayService => $app->make(CrmService::class));
Auth::provider(CrmUserProvider::class, function (Application $app, array $config) {
$hasher = $app->make(Hasher::class);
$crmService = $app->make(CrmService::class);
$model = Arr::string($config, 'model');
Assert::classExists($model);
Assert::implementsInterface($model, Authenticatable::class);
Assert::subclassOf($model, Model::class);
return new CrmUserProvider($hasher, $model, $crmService);
});
}
}