DolibarrApiKeyRequester::request()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 4
dl 0
loc 9
rs 10
c 1
b 0
f 1
cc 2
nc 2
nop 0
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