1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace TMV\HTTPlugModule\DIFactory; |
6
|
|
|
|
7
|
|
|
use Psr\Http\Client\ClientInterface; |
8
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
9
|
|
|
use Psr\Http\Message\StreamFactoryInterface; |
10
|
|
|
use Http\Client\Common\HttpMethodsClient; |
11
|
|
|
use Psr\Container\ContainerInterface; |
12
|
|
|
use InvalidArgumentException; |
13
|
|
|
use Laminas\ServiceManager\Exception\ServiceNotCreatedException; |
14
|
|
|
use Laminas\ServiceManager\Exception\ServiceNotFoundException; |
15
|
|
|
use Laminas\ServiceManager\Factory\AbstractFactoryInterface; |
16
|
|
|
|
17
|
|
|
use function explode; |
18
|
|
|
use function preg_match; |
19
|
|
|
|
20
|
|
|
class HttpMethodsClientAbstractFactory implements AbstractFactoryInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Can the factory create an instance for the service? |
24
|
|
|
* |
25
|
|
|
* @param string $requestedName |
26
|
|
|
*/ |
27
|
|
|
public function canCreate(ContainerInterface $container, $requestedName): bool |
28
|
|
|
{ |
29
|
|
|
if (! preg_match('/^httplug\.clients\.[^.]+\.http_methods$/', $requestedName)) { |
30
|
|
|
return false; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
[,, $clientName] = explode('.', $requestedName); |
34
|
|
|
|
35
|
|
|
return $container->has('httplug.clients.' . $clientName); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Create an object. |
40
|
|
|
* |
41
|
|
|
* @param string $requestedName |
42
|
|
|
* @param null|array<mixed> $options |
43
|
|
|
* |
44
|
|
|
* @throws ServiceNotFoundException if unable to resolve the service |
45
|
|
|
* @throws ServiceNotCreatedException if an exception is raised when |
46
|
|
|
* creating a service |
47
|
|
|
*/ |
48
|
|
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null): HttpMethodsClient |
49
|
|
|
{ |
50
|
|
|
if (! preg_match('/^httplug\.clients\.[^.]+\.http_methods$/', $requestedName)) { |
51
|
|
|
throw new InvalidArgumentException('Invalid service name'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
[,, $clientName] = explode('.', $requestedName); |
55
|
|
|
|
56
|
|
|
/** @var ClientInterface $httpClient */ |
57
|
|
|
$httpClient = $container->get('httplug.clients.' . $clientName); |
58
|
|
|
|
59
|
|
|
/** @var RequestFactoryInterface $requestFactory */ |
60
|
|
|
$requestFactory = $container->get('httplug.request_factory'); |
61
|
|
|
|
62
|
|
|
/** @var StreamFactoryInterface $streamFactory */ |
63
|
|
|
$streamFactory = $container->get('httplug.stream_factory'); |
64
|
|
|
|
65
|
|
|
return new HttpMethodsClient( |
66
|
|
|
$httpClient, |
67
|
|
|
$requestFactory, |
68
|
|
|
$streamFactory |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|