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

Builder::with()   A

Complexity

Conditions 1
Paths 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 6
Code Lines 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
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;
13
14
use Exchanger\Exchanger;
15
use Exchanger\Service\Chain;
16
use Http\Client\HttpClient;
17
use Http\Message\RequestFactory;
18
use Psr\Cache\CacheItemPoolInterface;
19
use Swap\Service\Factory;
20
21
/**
22
 * Helps building Swap.
23
 *
24
 * @author Florian Voutzinos <[email protected]>
25
 */
26
class Builder
27
{
28
    /**
29
     * The services.
30
     *
31
     * @var array
32
     */
33
    private $services = [];
34
35
    /**
36
     * The options.
37
     *
38
     * @var array
39
     */
40
    private $options = [];
41
42
    /**
43
     * The http client.
44
     *
45
     * @var HttpClient
46
     */
47
    private $httpClient;
48
49
    /**
50
     * The request factory.
51
     *
52
     * @var RequestFactory
53
     */
54
    private $requestFactory;
55
56
    /**
57
     * The cache item pool.
58
     *
59
     * @var CacheItemPoolInterface
60
     */
61
    private $cacheItemPool;
62
63
    /**
64
     * Constructor.
65
     *
66
     * @param array $options
67
     */
68
    public function __construct(array $options)
69
    {
70
        $this->options = $options;
71
    }
72
73
    /**
74
     * Adds a service.
75
     *
76
     * @param string $serviceName
77
     * @param array  $options
78
     *
79
     * @return Builder
80
     */
81
    public function with($serviceName, array $options = [])
82
    {
83
        $this->services[$serviceName] = $options;
84
85
        return $this;
86
    }
87
88
    /**
89
     * Uses the given http client.
90
     *
91
     * @param HttpClient $httpClient
92
     *
93
     * @return Builder
94
     */
95
    public function withHttpClient(HttpClient $httpClient)
96
    {
97
        $this->httpClient = $httpClient;
98
99
        return $this;
100
    }
101
102
    /**
103
     * Uses the given request factory.
104
     *
105
     * @param RequestFactory $requestFactory
106
     *
107
     * @return Builder
108
     */
109
    public function withRequestFactory(RequestFactory $requestFactory)
110
    {
111
        $this->requestFactory = $requestFactory;
112
113
        return $this;
114
    }
115
116
    /**
117
     * Uses the given cache item pool.
118
     *
119
     * @param CacheItemPoolInterface $cacheItemPool
120
     *
121
     * @return Builder
122
     */
123
    public function withCacheItemPool(CacheItemPoolInterface $cacheItemPool)
124
    {
125
        $this->cacheItemPool = $cacheItemPool;
126
127
        return $this;
128
    }
129
130
    /**
131
     * Builds Swap.
132
     *
133
     * @return Swap
134
     */
135
    public function build()
136
    {
137
        $serviceFactory = new Factory($this->httpClient, $this->requestFactory);
138
        $services = [];
139
140
        foreach ($this->services as $name => $options) {
141
            $services[] = $serviceFactory->create($name, $options);
142
        }
143
144
        $service = new Chain($services);
145
        $exchanger = new Exchanger($service, $this->cacheItemPool, $this->options);
146
147
        return new Swap($exchanger);
148
    }
149
}
150