Completed
Push — master ( 2c4db9...c28851 )
by Tobias
01:44
created

LocoClient::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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\Localise;
11
12
use FAPI\Localise\Api\Asset;
13
use FAPI\Localise\Api\Translation;
14
use FAPI\Localise\Hydrator\ModelHydrator;
15
use FAPI\Localise\Hydrator\Hydrator;
16
use Http\Client\HttpClient;
17
18
/**
19
 * @author Tobias Nyholm <[email protected]>
20
 */
21
final class LocoClient
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
     * @param HttpClient          $httpClient
43
     * @param Hydrator|null       $hydrator
44
     * @param RequestBuilder|null $requestBuilder
45
     */
46
    public function __construct(
47
        HttpClient $httpClient,
48
        Hydrator $hydrator = null,
49
        RequestBuilder $requestBuilder = null
50
    ) {
51
        $this->httpClient = $httpClient;
52
        $this->hydrator = $hydrator ?: new ModelHydrator();
53
        $this->requestBuilder = $requestBuilder ?: new RequestBuilder();
54
    }
55
56
    /**
57
     * @param HttpClientConfigurator $httpClientConfigurator
58
     * @param Hydrator|null          $hydrator
59
     * @param RequestBuilder|null    $requestBuilder
60
     *
61
     * @return ApiClient
62
     */
63
    public static function configure(
64
        HttpClientConfigurator $httpClientConfigurator,
65
        Hydrator $hydrator = null,
66
        RequestBuilder $requestBuilder = null
67
    ): self {
68
        $httpClient = $httpClientConfigurator->createConfiguredClient();
69
70
        return new self($httpClient, $hydrator, $requestBuilder);
71
    }
72
73
    /**
74
     * @return Api\Translation
75
     */
76
    public function translations(): Translation
77
    {
78
        return new Api\Translation($this->httpClient, $this->hydrator, $this->requestBuilder);
79
    }
80
81
    /**
82
     * @return Api\Asset
83
     */
84
    public function asset(): Asset
85
    {
86
        return new Api\Asset($this->httpClient, $this->hydrator, $this->requestBuilder);
87
    }
88
}
89