Completed
Push — master ( d61f2e...9d8019 )
by Florian
11s
created

Builder::useSimpleCache()   A

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