Completed
Push — master ( 9a00a0...a4cded )
by Florian
45s
created

Builder   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 124
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A build() 0 14 2
A add() 0 6 1
A useHttpClient() 0 6 1
A useRequestFactory() 0 6 1
A useCacheItemPool() 0 6 1
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 add($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 useHttpClient(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 useRequestFactory(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 useCacheItemPool(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