Completed
Push — master ( b81f06...ddd2b8 )
by Florian
11s
created

Factory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 83
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 3
A setHttpClient() 0 4 1
A setRequestFactory() 0 4 1
A create() 0 20 4
1
<?php
2
3
/*
4
 * This file is part of Swap.
5
 *
6
 * (c) Florian Voutzinos <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Swap\Service;
13
14
use Exchanger\Service\Service;
15
use Http\Client\HttpClient;
16
use Http\Discovery\HttpClientDiscovery;
17
use Http\Discovery\MessageFactoryDiscovery;
18
use Http\Message\RequestFactory;
19
20
/**
21
 * Helps building services.
22
 *
23
 * @author Florian Voutzinos <[email protected]>
24
 */
25
class Factory
26
{
27
    /**
28
     * The client.
29
     *
30
     * @var HttpClient
31
     */
32
    private $httpClient;
33
34
    /**
35
     * The request factory.
36
     *
37
     * @var RequestFactory
38
     */
39
    private $requestFactory;
40
41
    /**
42
     * The service registry.
43
     *
44
     * @var Registry
45
     */
46
    private $registry;
47
48
    /**
49
     * @param HttpClient|null     $httpClient
50
     * @param RequestFactory|null $requestFactory
51
     */
52
    public function __construct(HttpClient $httpClient = null, RequestFactory $requestFactory = null)
53
    {
54
        $this->httpClient = $httpClient ?: HttpClientDiscovery::find();
55
        $this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find();
56
        $this->registry = new Registry();
57
    }
58
59
    /**
60
     * Sets the http client.
61
     *
62
     * @param HttpClient $httpClient
63
     */
64
    public function setHttpClient(HttpClient $httpClient)
65
    {
66
        $this->httpClient = $httpClient;
67
    }
68
69
    /**
70
     * Sets the request factory.
71
     *
72
     * @param RequestFactory $requestFactory
73
     */
74
    public function setRequestFactory(RequestFactory $requestFactory)
75
    {
76
        $this->requestFactory = $requestFactory;
77
    }
78
79
    /**
80
     * Creates a new service.
81
     *
82
     * @param string $serviceName
83
     * @param array  $args
84
     *
85
     * @return \Exchanger\Contract\ExchangeRateService
86
     */
87
    public function create($serviceName, array $args = [])
88
    {
89
        if (!$this->registry->has($serviceName)) {
90
            throw new \InvalidArgumentException(sprintf('The service "%s" is not registered.', $serviceName));
91
        }
92
93
        $classOrCallable = $this->registry->get($serviceName);
94
95
        if (is_callable($classOrCallable)) {
96
            return call_user_func($classOrCallable);
97
        }
98
99
        if (is_subclass_of($classOrCallable, Service::class)) {
100
            return new $classOrCallable($this->httpClient, $this->requestFactory, $args);
101
        }
102
103
        $r = new \ReflectionClass($classOrCallable);
104
105
        return $r->newInstanceArgs($args);
106
    }
107
}
108