Passed
Push — master ( 425228...dd83ab )
by Laurent
54s queued 11s
created

DolibarrApiKeyRequester   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A request() 0 9 2
1
<?php
2
3
4
namespace Dolibarr\Client\Security\Authentication;
5
6
use Dolibarr\Client\Exception\ApiException;
7
use Dolibarr\Client\Service\LoginService;
8
9
/**
10
 * Requests the API key.
11
 *
12
 * @package Dolibarr\Client\Security\Authentication
13
 */
14
final class DolibarrApiKeyRequester
15
{
16
17
    /**
18
     * @var LoginService
19
     */
20
    private $loginService;
21
22
    /**
23
     * @var Authentication
24
     */
25
    private $authentication;
26
27
    /**
28
     * @param LoginService   $loginService
29
     * @param Authentication $authentication
30
     */
31
    public function __construct(LoginService $loginService, Authentication $authentication)
32
    {
33
        $this->loginService = $loginService;
34
        $this->authentication = $authentication;
35
    }
36
37
    /**
38
     * @return Authentication
39
     *
40
     * @throws ApiException
41
     */
42
    public function request()
43
    {
44
        if (!($this->authentication instanceof LoginAuthentication)) {
45
            return $this->authentication;
46
        }
47
48
        $token = $this->loginService->login($this->authentication->getLogin(), $this->authentication->getPassword());
49
50
        return new TokenAuthentication($token);
51
    }
52
}
53