Client   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 14
c 1
b 0
f 0
dl 0
loc 47
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __get() 0 3 1
A __construct() 0 6 1
A createService() 0 18 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Promopult\TikTokMarketingApi;
6
7
use Psr\Http\Client\ClientInterface;
8
9
/**
10
 * Class Client
11
 *
12
 * @property Service\Ad $ad
13
 * @property Service\AdGroup $adGroup
14
 * @property Service\Advertiser $advertiser
15
 * @property Service\Bc $bc
16
 * @property Service\Campaign $campaign
17
 * @property Service\Report $report
18
 * @property Service\Tools $tools
19
 * @property Service\User $user
20
 * @property Service\Leads $leads
21
 * @property Service\Pages $pages
22
 * @property Service\Creatives $creatives
23
 * @property Service\Images $images
24
 * @property Service\Video $video
25
 *
26
 * * @psalm-suppress UnusedClass
27
 *
28
 */
29
final class Client implements ServiceFactoryInterface
30
{
31
    private CredentialsInterface $credentials;
32
    private ClientInterface $httpClient;
33
    /**
34
     * @var ServiceInterface[]
35
     */
36
    private array $services = [];
37
38
    public function __construct(
39
        CredentialsInterface $credentials,
40
        ClientInterface $httpClient
41
    ) {
42
        $this->credentials = $credentials;
43
        $this->httpClient = $httpClient;
44
    }
45
46
    /**
47
     * @param string $serviceName
48
     * @return ServiceInterface
49
     */
50
    public function __get(string $serviceName): ServiceInterface
51
    {
52
        return $this->createService($serviceName);
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58
    public function createService(string $serviceName): ServiceInterface
59
    {
60
        if (empty($this->services[$serviceName])) {
61
            $serviceClass = __NAMESPACE__ . '\\Service\\' . ucfirst($serviceName);
62
63
            if (!class_exists($serviceClass)) {
64
                throw new \InvalidArgumentException("Service {$serviceName} is not found.");
65
            }
66
            /**
67
             * @var ServiceInterface $instance
68
             * @psalm-suppress MixedMethodCall
69
             */
70
            $instance = new $serviceClass($this->credentials, $this->httpClient);
71
72
            $this->services[$serviceName] = $instance;
73
        }
74
75
        return $this->services[$serviceName];
76
    }
77
}
78