Completed
Push — master ( bf6574...290f71 )
by Tobias
01:41
created

BillogramClient   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 8
dl 0
loc 104
ccs 14
cts 21
cp 0.6667
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A configure() 0 9 1
A create() 0 6 1
A customers() 0 4 1
A items() 0 4 1
A invoices() 0 4 1
A report() 0 4 1
A settings() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
/*
5
 * This software may be modified and distributed under the terms
6
 * of the MIT license. See the LICENSE file for details.
7
 */
8
9
namespace Billogram;
10
11
use Billogram\Api\Customer;
12
use Billogram\Api\Invoice;
13
use Billogram\Api\Item;
14
use Billogram\Api\Report;
15
use Billogram\Api\Settings;
16
use Billogram\Hydrator\ModelHydrator;
17
use Billogram\Hydrator\Hydrator;
18
use Http\Client\HttpClient;
19
20
/**
21
 * @author Tobias Nyholm <[email protected]>
22
 */
23
final class BillogramClient
24
{
25
    /**
26
     * @var HttpClient
27
     */
28
    private $httpClient;
29
30
    /**
31
     * @var Hydrator
32
     */
33
    private $hydrator;
34
35
    /**
36
     * @var RequestBuilder
37
     */
38
    private $requestBuilder;
39
40
    /**
41
     * The constructor accepts already configured HTTP clients.
42
     * Use the configure method to pass a configuration to the Client and create an HTTP Client.
43
     *
44
     * @param HttpClient          $httpClient
45
     * @param Hydrator|null       $hydrator
46
     * @param RequestBuilder|null $requestBuilder
47
     */
48 9
    public function __construct(
49
        HttpClient $httpClient,
50
        Hydrator $hydrator = null,
51
        RequestBuilder $requestBuilder = null
52
    ) {
53 9
        $this->httpClient = $httpClient;
54 9
        $this->hydrator = $hydrator ?: new ModelHydrator();
55 9
        $this->requestBuilder = $requestBuilder ?: new RequestBuilder();
56 9
    }
57
58
    /**
59
     * @param HttpClientConfigurator $httpClientConfigurator
60
     * @param Hydrator|null          $hydrator
61
     * @param RequestBuilder|null    $requestBuilder
62
     *
63
     * @return BillogramClient
64
     */
65 9
    public static function configure(
66
        HttpClientConfigurator $httpClientConfigurator,
67
        Hydrator $hydrator = null,
68
        RequestBuilder $requestBuilder = null
69
    ): self {
70 9
        $httpClient = $httpClientConfigurator->createConfiguredClient();
71
72 9
        return new self($httpClient, $hydrator, $requestBuilder);
73
    }
74
75
    /**
76
     * @param string $apiKey
77
     *
78
     * @return BillogramClient
79
     */
80
    public static function create(string $username, string $apiKey): BillogramClient
81
    {
82
        $httpClientConfigurator = (new HttpClientConfigurator())->setAuth($username, $apiKey);
83
84
        return self::configure($httpClientConfigurator);
85
    }
86
87
    /**
88
     * @return Api\Customer
89
     */
90
    public function customers(): Customer
91
    {
92
        return new Api\Customer($this->httpClient, $this->hydrator, $this->requestBuilder);
93
    }
94
95
    /**
96
     * @return Api\Item
97
     */
98 5
    public function items(): Item
99
    {
100 5
        return new Api\Item($this->httpClient, $this->hydrator, $this->requestBuilder);
101
    }
102
103
    /**
104
     * @return Api\Invoice
105
     */
106
    public function invoices(): Invoice
107
    {
108
        return new Api\Invoice($this->httpClient, $this->hydrator, $this->requestBuilder);
109
    }
110
111
    /**
112
     * @return Api\Report
113
     */
114 2
    public function report(): Report
115
    {
116 2
        return new Api\Report($this->httpClient, $this->hydrator, $this->requestBuilder);
117
    }
118
119
    /**
120
     * @return Api\Settings
121
     */
122 2
    public function settings(): Settings
123
    {
124 2
        return new Api\Settings($this->httpClient, $this->hydrator, $this->requestBuilder);
125
    }
126
}
127