Passed
Pull Request — develop (#23)
by Adam
01:31
created

WatsonService::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace IBM\Watson\Common;
6
7
use Http\Client\HttpClient;
8
use Http\Message\Authentication;
9
use IBM\Watson\Common\HttpClient\Builder;
10
use IBM\Watson\Common\Hydrator\HydratorInterface;
11
use IBM\Watson\Common\Hydrator\ModelHydrator;
12
use IBM\Watson\Common\Util\DiscoveryTrait;
13
14
/**
15
 * Base service client which all other service clients should extend from.
16
 */
17
class WatsonService implements WatsonServiceInterface
18
{
19
    use DiscoveryTrait;
20
21
    /**
22
     * @var \Http\Client\HttpClient|null
23
     */
24
    protected $httpClient;
25
26
    /**
27
     * @var \IBM\Watson\Common\Hydrator\ModelHydrator
28
     */
29
    protected $hydrator;
30
31
    /**
32
     * @var \IBM\Watson\Common\RequestBuilder
33
     */
34
    protected $requestBuilder;
35
36
    /**
37
     * @var string
38
     */
39
    protected $hostname;
40
41
    /**
42
     * @param \Http\Client\HttpClient|null                       $httpClient     HTTP client to send requests.
43
     * @param \IBM\Watson\Common\Hydrator\HydratorInterface|null $hydrator       Hydrator to hydrate responses.
44
     * @param \IBM\Watson\Common\RequestBuilderInterface|null    $requestBuilder Request builder to create requests.
45
     */
46
    public function __construct(
47
        HttpClient $httpClient = null,
48
        HydratorInterface $hydrator = null,
49
        RequestBuilderInterface $requestBuilder = null
50
    ) {
51
        $this->httpClient = $httpClient ?: $this->discoverHttpClient();
52
        $this->hydrator = $hydrator ?: new ModelHydrator();
53
        $this->requestBuilder = $requestBuilder ?: new RequestBuilder();
54
    }
55
56
    /**
57
     * Create configured service.
58
     *
59
     * @param \Http\Message\Authentication $authentication Authentication method.
60
     *
61
     * @return \IBM\Watson\Common\WatsonServiceInterface
62
     */
63
    public static function create(Authentication $authentication): WatsonServiceInterface
64
    {
65
        $httpClient = (new Builder())
66
            ->withAuthentication($authentication)
67
            ->createConfiguredClient();
68
69
        return new self($httpClient);
70
    }
71
}
72