Completed
Push — master ( b36f65...4f5144 )
by Tobias
01:50
created

ApiClient::invoice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This software may be modified and distributed under the terms
7
 * of the MIT license. See the LICENSE file for details.
8
 */
9
10
namespace FAPI\Fortnox;
11
12
use FAPI\Fortnox\Api\Customer;
13
use FAPI\Fortnox\Api\Invoice;
14
use FAPI\Fortnox\Hydrator\Hydrator;
15
use FAPI\Fortnox\Hydrator\ModelHydrator;
16
use Http\Client\HttpClient;
17
18
/**
19
 * @author Tobias Nyholm <[email protected]>
20
 */
21
final class ApiClient
22
{
23
    /**
24
     * @var HttpClient
25
     */
26
    private $httpClient;
27
28
    /**
29
     * @var Hydrator
30
     */
31
    private $hydrator;
32
33
    /**
34
     * @var RequestBuilder
35
     */
36
    private $requestBuilder;
37
38
    /**
39
     * The constructor accepts already configured HTTP clients.
40
     * Use the configure method to pass a configuration to the Client and create an HTTP Client.
41
     */
42
    public function __construct(
43
        HttpClient $httpClient,
44
        Hydrator $hydrator = null,
45
        RequestBuilder $requestBuilder = null
46
    ) {
47
        $this->httpClient = $httpClient;
48
        $this->hydrator = $hydrator ?: new ModelHydrator();
49
        $this->requestBuilder = $requestBuilder ?: new RequestBuilder();
50
    }
51
52
    /**
53
     * @return ApiClient
54
     */
55
    public static function configure(
56
        HttpClientConfigurator $httpClientConfigurator,
57
        Hydrator $hydrator = null,
58
        RequestBuilder $requestBuilder = null
59
    ): self {
60
        $httpClient = $httpClientConfigurator->createConfiguredClient();
61
62
        return new self($httpClient, $hydrator, $requestBuilder);
63
    }
64
65
    public static function create(string $clientSecret, string $accessToken): self
66
    {
67
        $httpClientConfigurator = (new HttpClientConfigurator())
68
            ->setClientSecret($clientSecret)
69
            ->setAccessToken($accessToken);
70
71
        return self::configure($httpClientConfigurator);
72
    }
73
74
    public function customer(): Customer
75
    {
76
        return new Api\Customer($this->httpClient, $this->hydrator, $this->requestBuilder);
77
    }
78
79
    public function invoice(): Invoice
80
    {
81
        return new Api\Invoice($this->httpClient, $this->hydrator, $this->requestBuilder);
82
    }
83
}
84