OsnovaResource::makeClient()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Osnova;
4
5
use GuzzleHttp\Client;
6
use Osnova\Api\ApiProvider;
7
8
abstract class OsnovaResource
9
{
10
    /** @var ApiProvider */
11
    protected $apiProvider;
12
13
    /**
14
     * OsnovaResource constructor.
15
     *
16
     * @param ApiProvider $apiProvider
17
     */
18
    public function __construct(ApiProvider $apiProvider)
19
    {
20
        $this->apiProvider = $apiProvider;
21
    }
22
23
    /**
24
     * Get the resource domain.
25
     *
26
     * @return string
27
     */
28
    abstract public static function domain();
29
30
    /**
31
     * Get the original API Provider instance.
32
     *
33
     * @return ApiProvider
34
     */
35
    public function getApiProvider()
36
    {
37
        return $this->apiProvider;
38
    }
39
40
    /**
41
     * Generate URL for the current resource.
42
     *
43
     * @param string $path = '' URL path.
44
     *
45
     * @return string
46
     */
47
    public function generateUrl(string $path = '')
48
    {
49
        return 'https://'.static::domain().($path ? '/'.ltrim($path, '/') : '');
50
    }
51
52
    /**
53
     * Make new resource instance.
54
     *
55
     * @param string $version
56
     *
57
     * @return static
58
     */
59
    public static function make(string $version)
60
    {
61
        return new static(
62
            new ApiProvider(
63
                static::makeClient($version)
64
            )
65
        );
66
    }
67
68
    /**
69
     * Make new HTTP client instance.
70
     *
71
     * @param string $version
72
     *
73
     * @return Client
74
     */
75
    protected static function makeClient(string $version)
76
    {
77
        return new Client([
78
            'base_uri' => 'https://api.'.static::domain().'/v'.$version.'/',
79
            'headers' => [
80
                'Accept' => 'application/json',
81
                'User-Agent' => '',
82
            ],
83
        ]);
84
    }
85
}
86