ClientAbstractFactory   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 61.29%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 33
c 1
b 0
f 0
dl 0
loc 89
ccs 19
cts 31
cp 0.6129
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 31 5
A retrievePlugins() 0 22 5
A canCreate() 0 11 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TMV\HTTPlugModule\DIFactory;
6
7
use Http\Client\Common\Plugin;
8
use Http\Client\Common\PluginClient;
9
use Psr\Container\ContainerInterface;
10
use InvalidArgumentException;
11
use Psr\Http\Client\ClientInterface;
12
use Laminas\ServiceManager\Exception\ServiceNotCreatedException;
13
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
14
use Laminas\ServiceManager\Factory\AbstractFactoryInterface;
15
use TMV\HTTPlugModule\ClientFactory\AutoDiscoveryFactory;
16
use TMV\HTTPlugModule\ClientFactory\ClientFactory;
17
use TMV\HTTPlugModule\PluginFactoryManager;
18
19
use function is_array;
20
use function is_string;
21
22
class ClientAbstractFactory implements AbstractFactoryInterface
23
{
24
    /**
25
     * @param string $requestedName
26
     */
27
    public function canCreate(ContainerInterface $container, $requestedName): bool
28
    {
29
        if (! preg_match('/^httplug\.clients\.[^.]+$/', $requestedName)) {
30
            return false;
31
        }
32
33
        [,, $clientName] = explode('.', $requestedName);
34
35
        $config = $container->get('config')['httplug']['clients'][$clientName] ?? null;
36
37
        return is_array($config);
38
    }
39
40
    /**
41
     * Create an object.
42
     *
43
     * @param string $requestedName
44
     * @param null|array<mixed> $options
45
     *
46
     * @throws ServiceNotFoundException if unable to resolve the service
47
     * @throws ServiceNotCreatedException if an exception is raised when
48
     *                                    creating a service
49
     */
50
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null): ClientInterface
51
    {
52
        if (! preg_match('/^httplug\.clients\.[^.]+$/', $requestedName)) {
53
            throw new InvalidArgumentException('Invalid service name');
54
        }
55
56
        [,, $clientName] = explode('.', $requestedName);
57
58
        $config = $container->get('config')['httplug']['clients'][$clientName] ?? null;
59
60 1
        if (! is_array($config)) {
61
            throw new InvalidArgumentException('Invalid service name');
62 1
        }
63
64
        if (! empty($config['service'])) {
65
            $client = $container->get($config['service']);
66 1
        } else {
67
            /** @var ClientFactory $factory */
68 1
            $factory = $container->get($config['factory'] ?? AutoDiscoveryFactory::class);
69
            $client = $factory->createClient($config['config'] ?? []);
70 1
        }
71
72
        $plugins = $this->retrievePlugins($container, $config['plugins'] ?? []);
73
74 1
        if (0 === count($plugins)) {
75
            return $client;
76
        }
77
78 1
        return new PluginClient(
79 1
            $client,
80
            $plugins
81
        );
82 1
    }
83
84 1
    /**
85
     * @param array<string, mixed> $config
86
     *
87
     * @return Plugin[]
88 1
     */
89 1
    private function retrievePlugins(ContainerInterface $container, array $config): array
90
    {
91
        /** @var PluginFactoryManager $pluginFactoryManager */
92
        $pluginFactoryManager = $container->get(PluginFactoryManager::class);
93
94
        $plugins = [];
95
96
        foreach ($config as $nameOrConfig) {
97
            if (is_string($nameOrConfig)) {
98
                $plugins[] = $container->get($nameOrConfig);
99
                continue;
100 1
            }
101
102
            if (! is_array($nameOrConfig) || ! is_string($nameOrConfig['name'] ?? null)) {
103 1
                throw new InvalidArgumentException('Invalid client plugin');
104
            }
105 1
106
            $plugins[] = $pluginFactoryManager->getFactory($nameOrConfig['name'])
107 1
                ->createPlugin($nameOrConfig['config'] ?? []);
108 1
        }
109 1
110 1
        return $plugins;
111
    }
112
}
113