Builder::add()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Swap.
7
 *
8
 * (c) Florian Voutzinos <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Swap;
15
16
use Exchanger\Contract\ExchangeRateService;
17
use Exchanger\Exchanger;
18
use Exchanger\Service\Chain;
19
use Http\Client\HttpClient;
20
use Psr\SimpleCache\CacheInterface;
21
use Swap\Service\Factory;
22
use Psr\Http\Client\ClientInterface;
23
use Psr\Http\Message\RequestFactoryInterface;
24
25
/**
26
 * Helps building Swap.
27
 *
28
 * @author Florian Voutzinos <[email protected]>
29
 */
30
final class Builder
31
{
32
    /**
33
     * The services.
34
     *
35
     * @var array
36
     */
37
    private $services = [];
38
39
    /**
40
     * The options.
41
     *
42
     * @var array
43
     */
44
    private $options = [];
45
46
    /**
47
     * The http client.
48
     *
49
     * @var HttpClient
50
     */
51
    private $httpClient;
52
53
    /**
54
     * The request factory.
55
     *
56
     * @var RequestFactoryInterface
57
     */
58
    private $requestFactory;
59
60
    /**
61
     * The cache.
62
     *
63
     * @var CacheInterface
64
     */
65
    private $cache;
66
67
    /**
68
     * Constructor.
69
     *
70
     * @param array $options
71
     */
72
    public function __construct(array $options = [])
73
    {
74
        $this->options = $options;
75
    }
76
77
    /**
78
     * Adds a service by service name.
79
     *
80
     * @param string $serviceName
81
     * @param array  $options
82
     *
83
     * @return Builder
84
     */
85
    public function add(string $serviceName, array $options = []): self
86
    {
87
        $this->services[$serviceName] = $options;
88
89
        return $this;
90
    }
91
92
    /**
93
     * Add a service by service instance.
94
     *
95
     * @param ExchangeRateService $exchangeRateService
96
     *
97
     * @return Builder
98
     */
99
    public function addExchangeRateService(ExchangeRateService $exchangeRateService): self
100
    {
101
        $this->services[spl_object_hash($exchangeRateService)] = $exchangeRateService;
102
103
        return $this;
104
    }
105
106
    /**
107
     * Uses the given http client.
108
     *
109
     * @param HttpClient|ClientInterface $httpClient
110
     *
111
     * @return Builder
112
     */
113 View Code Duplication
    public function useHttpClient($httpClient): self
0 ignored issues
show
Duplication introduced by florianv
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
    {
115
        if (!$httpClient instanceof ClientInterface && !$httpClient instanceof HttpClient) {
116
            throw new \LogicException('Client must be an instance of Http\\Client\\HttpClient or Psr\\Http\\Client\\ClientInterface');
117
        }
118
119
        $this->httpClient = $httpClient;
0 ignored issues
show
Documentation Bug introduced by florianv
$httpClient is of type object<Psr\Http\Client\ClientInterface>, but the property $httpClient was declared to be of type object<Http\Client\HttpClient>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
120
121
        return $this;
122
    }
123
124
    /**
125
     * Uses the given request factory.
126
     *
127
     * @param RequestFactoryInterface $requestFactory
128
     *
129
     * @return Builder
130
     */
131
    public function useRequestFactory(RequestFactoryInterface $requestFactory): self
132
    {
133
        $this->requestFactory = $requestFactory;
134
135
        return $this;
136
    }
137
138
    /**
139
     * Uses the given simple cache.
140
     *
141
     * @param CacheInterface $cache
142
     *
143
     * @return Builder
144
     */
145
    public function useSimpleCache(CacheInterface $cache): self
146
    {
147
        $this->cache = $cache;
148
149
        return $this;
150
    }
151
152
    /**
153
     * Builds Swap.
154
     *
155
     * @return Swap
156
     */
157
    public function build(): Swap
158
    {
159
        $serviceFactory = new Factory($this->httpClient, $this->requestFactory);
160
161
        /** @var ExchangeRateService[] $services */
162
        $services = [];
163
164
        foreach ($this->services as $name => $optionsOrService) {
165
            /** @var array|ExchangeRateService $optionsOrService */
166
            if ($optionsOrService instanceof ExchangeRateService) {
167
                $services[] = $optionsOrService;
168
            } else {
169
                $services[] = $serviceFactory->create($name, $optionsOrService);
170
            }
171
        }
172
173
        $service = new Chain($services);
174
        $exchanger = new Exchanger($service, $this->cache, $this->options);
175
176
        return new Swap($exchanger);
177
    }
178
}
179