AuthenticateHttpClient   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
eloc 9
dl 0
loc 41
rs 10
c 1
b 0
f 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A authenticate() 0 5 1
A createHandler() 0 6 1
1
<?php
2
3
4
namespace Dolibarr\Client\HttpClient;
5
6
use Dolibarr\Client\Exception\ApiException;
7
use Dolibarr\Client\HttpClient\Middleware\AuthenticationMiddleware;
8
use Dolibarr\Client\Security\Authentication\Authentication;
9
use Dolibarr\Client\Security\Authentication\DolibarrApiKeyRequester;
10
use GuzzleHttp\HandlerStack;
11
12
/**
13
 * @package Dolibarr\Client\HttpClient
14
 */
15
final class AuthenticateHttpClient extends HttpClient
16
{
17
18
    /**
19
     * @var DolibarrApiKeyRequester
20
     */
21
    private $dolibarrApikeyRequester;
22
23
    /**
24
     * @param array                   $options
25
     * @param DolibarrApiKeyRequester $dolibarrApiKeyRequester
26
     */
27
    public function __construct(array $options, DolibarrApiKeyRequester $dolibarrApiKeyRequester)
28
    {
29
        parent::__construct($options);
30
        $this->dolibarrApikeyRequester = $dolibarrApiKeyRequester;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     *
36
     * @throws ApiException
37
     */
38
    protected function authenticate()
39
    {
40
        $authentication = $this->dolibarrApikeyRequester->request();
41
42
        return ['handler' => $this->createHandler($authentication)];
43
    }
44
45
    /**
46
     * @param Authentication $authentication
47
     *
48
     * @return HandlerStack
49
     */
50
    private function createHandler(Authentication $authentication)
51
    {
52
        $stack = HandlerStack::create();
53
        $stack->push(new AuthenticationMiddleware($authentication));
54
55
        return $stack;
56
    }
57
}
58