ClientBuilder::userAgent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 6
c 1
b 1
f 0
dl 0
loc 14
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Dolibarr\Client;
4
5
use Doctrine\Common\Annotations\AnnotationRegistry;
6
use Dolibarr\Client\HttpClient\AuthenticateHttpClient;
7
use Dolibarr\Client\HttpClient\HttpClient;
8
use Dolibarr\Client\HttpClient\Middleware\AuthenticationMiddleware;
9
use Dolibarr\Client\Security\Authentication\Authentication;
10
use Dolibarr\Client\Security\Authentication\DolibarrApiKeyRequester;
11
use Dolibarr\Client\Service\LoginService;
12
use GuzzleHttp\HandlerStack;
13
use JMS\Serializer\Serializer;
14
use JMS\Serializer\SerializerBuilder;
15
use Webmozart\Assert\Assert;
16
17
/**
18
 * @package Dolibarr\Api\Client
19
 */
20
final class ClientBuilder
21
{
22
23
    /**
24
     * @var string
25
     */
26
    private $baseUri;
27
28
    /**
29
     * @var Authentication
30
     */
31
    private $authentication;
32
33
    /**
34
     * @var boolean
35
     */
36
    private $debug;
37
38
    /**
39
     * @var array
40
     */
41
    private $options;
42
43
    /**
44
     * @param string         $baseUri        The base uri of the api
45
     * @param Authentication $authentication The authentication to access the api
46
     */
47
    public function __construct(
48
        $baseUri,
49
        Authentication $authentication
50
    ) {
51
        Assert::stringNotEmpty($baseUri, "The baseUri should not be empty");
52
53
        $this->baseUri = $baseUri;
54
        $this->authentication = $authentication;
55
        $this->options = [];
56
    }
57
58
    /**
59
     * @return Client
60
     */
61
    public function build()
62
    {
63
        return new Client(
64
            $this->createHttpClient(),
65
            $this->createSerializer()
66
        );
67
    }
68
69
    /**
70
     * @deprecated please use debug
71
     *
72
     * @param boolean $debug
73
     *
74
     * @return $this
75
     */
76
    public function setDebug($debug)
77
    {
78
        $this->debug = (bool)$debug;
79
80
        return $this;
81
    }
82
83
    /**
84
     * Enables the debug mode.
85
     *
86
     * @return $this
87
     */
88
    public function debug()
89
    {
90
        $this->debug = true;
91
92
        return $this;
93
    }
94
95
    /**
96
     * Set the user agent.
97
     *
98
     * @param string $userAgent
99
     *
100
     * @return ClientBuilder
101
     */
102
    public function userAgent($userAgent)
103
    {
104
        Assert::stringNotEmpty($userAgent);
105
106
        $this->options = array_merge(
107
            $this->options,
108
            [
109
                'headers' => [
110
                    'User-Agent' => $userAgent
111
                ]
112
            ]
113
        );
114
115
        return $this;
116
    }
117
118
    /**
119
     * @return Serializer
120
     */
121
    private function createSerializer()
122
    {
123
        AnnotationRegistry::registerLoader('class_exists');
124
125
        return SerializerBuilder::create()
126
          ->addDefaultHandlers()
127
          ->build();
128
    }
129
130
    /**
131
     * @return HttpClient
132
     */
133
    private function createHttpClient()
134
    {
135
        $httpClient = new AuthenticateHttpClient(
136
            $this->clientOptions(),
137
            $this->defaultKeyRequester()
138
        );
139
140
        return $httpClient;
141
    }
142
143
    /**
144
     * @return DolibarrApiKeyRequester
145
     */
146
    private function defaultKeyRequester()
147
    {
148
        return new DolibarrApiKeyRequester(
149
            $this->loginService(),
150
            $this->authentication
151
        );
152
    }
153
154
    /**
155
     * @return LoginService
156
     */
157
    private function loginService()
158
    {
159
        $client = new HttpClient($this->clientOptions());
160
161
        return new LoginService($client, $this->createSerializer());
162
    }
163
164
    /**
165
     * @return array
166
     */
167
    private function clientOptions()
168
    {
169
        return array_merge(
170
            [
171
                'base_uri' => $this->baseUri,
172
                'debug'    => $this->debug,
173
            ],
174
            $this->options
175
        );
176
    }
177
}
178