Passed
Pull Request — develop (#23)
by Adam
01:31
created

Builder::addPathPlugin()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace IBM\Watson\Common\HttpClient;
6
7
use Http\Client\Common\Plugin\AddHostPlugin;
8
use Http\Client\Common\Plugin\AddPathPlugin;
9
use Http\Client\Common\Plugin\AuthenticationPlugin;
10
use Http\Client\Common\PluginClient;
11
use Http\Client\HttpClient;
12
use Http\Message\Authentication;
13
use Http\Message\UriFactory;
14
use IBM\Watson\Common\Util\DiscoveryTrait;
15
use Psr\Http\Message\UriInterface;
16
17
/**
18
 * Builder creates a user configured HTTP client.
19
 */
20
class Builder
21
{
22
    use DiscoveryTrait;
23
24
    /**
25
     * @var \Http\Client\HttpClient
26
     */
27
    private $httpClient;
28
29
    /**
30
     * @var \Http\Message\UriFactory
31
     */
32
    private $uriFactory;
33
34
    /**
35
     * @var \Http\Message\Authentication
36
     */
37
    private $authentication;
38
39
    /**
40
     * @var string
41
     */
42
    private $hostname;
43
44
    /**
45
     * @var string
46
     */
47
    private $path;
48
49
    /**
50
     * @var array
51
     */
52
    private $plugins = [];
53
54
    /**
55
     * @param HttpClient|null               $httpClient HTTP client for sending requests.
56
     * @param \Http\Message\UriFactory|null $uriFactory URI factory for creating URI.
57
     */
58
    public function __construct(
59
        HttpClient $httpClient = null,
60
        UriFactory $uriFactory = null
61
    ) {
62
        $this->httpClient = $httpClient ?: $this->discoverHttpClient();
63
        $this->uriFactory = $uriFactory ?: $this->discoverUriFactory();
64
    }
65
66
    /**
67
     * @return \Http\Client\Common\PluginClient HTTP client with configured plugins.
68
     */
69
    public function createConfiguredClient(): PluginClient
70
    {
71
        $this->addHostPlugin();
72
        $this->addPathPlugin();
73
        $this->addAuthenticationPlugin();
74
75
        return new PluginClient($this->httpClient, $this->plugins);
76
    }
77
78
    /**
79
     * @param \Http\Message\Authentication $authentication Authentication method.
80
     *
81
     * @return $this
82
     */
83
    public function withAuthentication(Authentication $authentication): self
84
    {
85
        $this->authentication = $authentication;
86
87
        return $this;
88
    }
89
90
    /**
91
     * @param string $hostname API Hostname.
92
     *
93
     * @return $this
94
     */
95
    public function withHostname(string $hostname): self
96
    {
97
        $this->hostname = $hostname;
98
99
        return $this;
100
    }
101
102
    /**
103
     * @param string $path Default API path.
104
     *
105
     * @return $this
106
     */
107
    public function withPath(string $path): self
108
    {
109
        $this->path = $path;
110
111
        return $this;
112
    }
113
114
    /**
115
     * Add authentication plugin to client.
116
     *
117
     * @return $this
118
     */
119
    private function addAuthenticationPlugin(): self
120
    {
121
        if (null !== $this->authentication) {
122
            $this->plugins[] = new AuthenticationPlugin($this->authentication);
123
        }
124
125
        return $this;
126
    }
127
128
    /**
129
     * @return \Psr\Http\Message\UriInterface
130
     */
131
    private function getHostUri(): UriInterface
132
    {
133
        return $this->uriFactory->createUri($this->hostname);
134
    }
135
136
    /**
137
     * @return \Psr\Http\Message\UriInterface
138
     */
139
    private function getPathUri(): UriInterface
140
    {
141
        return $this->uriFactory->createUri($this->path);
142
    }
143
144
    /**
145
     * @return \IBM\Watson\Common\HttpClient\Builder
146
     */
147
    private function addHostPlugin(): self
148
    {
149
        if (null !== $this->hostname) {
150
            $this->plugins[] = new AddHostPlugin($this->getHostUri());
151
        }
152
153
        return $this;
154
    }
155
156
    /**
157
     * @return $this
158
     */
159
    private function addPathPlugin(): self
160
    {
161
        if (null !== $this->path) {
162
            $this->plugins[] = new AddPathPlugin($this->getPathUri());
163
        }
164
165
        return $this;
166
    }
167
}
168