promopult /
dadata-client
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Promopult\Dadata; |
||
| 4 | |||
| 5 | use Psr\Http\Client\ClientInterface; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * Class Client |
||
| 9 | * |
||
| 10 | * Services |
||
| 11 | * @property \Promopult\Dadata\Services\Suggestions $suggestions |
||
| 12 | * @property \Promopult\Dadata\Services\Clean $clean |
||
| 13 | * @property \Promopult\Dadata\Services\Profile $profile |
||
| 14 | */ |
||
| 15 | class Client implements ServiceFactoryInterface |
||
| 16 | { |
||
| 17 | private RequestFactoryInterface $requestFactory; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 18 | private ClientInterface $httpClient; |
||
| 19 | private CredentialsInterface $credentials; |
||
| 20 | |||
| 21 | public function __construct( |
||
| 22 | CredentialsInterface $credentials, |
||
| 23 | ClientInterface $httpClient |
||
| 24 | ) { |
||
| 25 | $this->credentials = $credentials; |
||
| 26 | $this->httpClient = $httpClient; |
||
| 27 | } |
||
| 28 | |||
| 29 | public function getService(string $serviceName): Service |
||
| 30 | { |
||
| 31 | $serviceClass = __NAMESPACE__ . '\\Services\\' . ucfirst($serviceName); |
||
| 32 | |||
| 33 | if (!class_exists($serviceClass)) { |
||
| 34 | throw new \Promopult\Dadata\Exceptions\ServiceNotFoundException( |
||
| 35 | "Service with {$serviceName} is not found." |
||
| 36 | ); |
||
| 37 | } |
||
| 38 | |||
| 39 | return new $serviceClass( |
||
| 40 | $this->credentials, |
||
| 41 | $this->httpClient |
||
| 42 | ); |
||
| 43 | } |
||
| 44 | |||
| 45 | public function __get($name): Service |
||
| 46 | { |
||
| 47 | return $this->getService($name); |
||
| 48 | } |
||
| 49 | } |
||
| 50 |