iDoklad   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 8
dl 0
loc 114
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A execute() 0 12 1
B resolveUri() 0 33 8
A resolveOptions() 0 28 2
A getToken() 0 4 1
A getConfig() 0 4 1
A assertConfiguration() 0 4 1
1
<?php
2
3
namespace Fousky\Component\iDoklad;
4
5
use Fousky\Component\iDoklad\Functions\iDokladAbstractFunction;
6
use Fousky\Component\iDoklad\Functions\iDokladFunctionInterface;
7
use Fousky\Component\iDoklad\Model\Auth\AccessToken;
8
use Fousky\Component\iDoklad\Model\iDokladModelInterface;
9
use GuzzleHttp\Client;
10
use GuzzleHttp\RequestOptions;
11
12
/**
13
 * @author Lukáš Brzák <[email protected]>
14
 */
15
class iDoklad
16
{
17
    /** @var array */
18
    protected $config;
19
20
    /** @var iDokladTokenFactory */
21
    protected $helper;
22
23
    /** @var iDokladTokenFactory */
24
    protected $tokenFactory;
25
26
    /** @var Client */
27
    protected $client;
28
29
    public function __construct(array $config, iDokladTokenFactory $tokenFactory)
30
    {
31
        $this->config = $this->assertConfiguration($config);
32
        $this->tokenFactory = $tokenFactory;
33
        $this->client = new Client([
34
            'base_uri' => $this->config['url'],
35
        ]);
36
    }
37
38
    public function execute(iDokladFunctionInterface $function): iDokladModelInterface
39
    {
40
        return $function
41
            ->setConfig($this->getConfig())
42
            ->handleResponse(
43
                $this->client->request(
44
                    $function->getHttpMethod(),
45
                    $this->resolveUri($function),
46
                    $this->resolveOptions($function)
47
                )
48
            );
49
    }
50
51
    protected function resolveUri(iDokladFunctionInterface $function): string
52
    {
53
        $uri = $function->getUri();
54
55
        $parts = [];
56
57
        if ($function->hasSortable()) {
58
            $parts = array_merge_recursive($parts, $function->getSortable()->getHttpQuery());
59
        }
60
61
        if ($function->hasPaginator()) {
62
            $parts = array_merge_recursive($parts, $function->getPaginator()->getHttpQuery());
63
        }
64
65
        if ($function->hasFilter()) {
66
            $parts = array_merge_recursive($parts, $function->getFilter()->getHttpQuery());
67
        }
68
69
        if (count($parts) > 0) {
70
            // URI does not have "?"
71
            if (false === strpos($uri, '?')) {
72
                $uri .= '?';
73
            }
74
            // URI has "?", but does not ends with "&"
75
            $lastChar = substr($uri, -1);
76
            if (false !== strpos($uri, '?') && !in_array($lastChar, ['&', '?'], true)) {
77
                $uri .= '&';
78
            }
79
            $uri .= http_build_query($parts);
80
        }
81
82
        return $uri;
83
    }
84
85
    protected function resolveOptions(iDokladFunctionInterface $function): array
86
    {
87
        $defaults = [
88
            RequestOptions::HEADERS => [
89
                'Accept' => 'application/json',
90
                'Accept-Language' => $this->config['language'],
91
            ],
92
        ];
93
94
        if ($function->injectAccessTokenToHeaders()) {
95
            $token = $this->getToken();
96
97
            $defaults = array_merge_recursive(
98
                $defaults,
99
                [
100
                    'headers' => [
101
                        'Authorization' => sprintf(
102
                            '%s %s',
103
                            $token->getType(),
104
                            $token->getToken()
105
                        ),
106
                    ],
107
                ]
108
            );
109
        }
110
111
        return array_merge_recursive($function->getGuzzleOptions(), $defaults);
112
    }
113
114
    public function getToken(): AccessToken
115
    {
116
        return $this->tokenFactory->getToken($this->client, $this->config);
117
    }
118
119
    public function getConfig(): array
120
    {
121
        return $this->config;
122
    }
123
124
    protected function assertConfiguration(array $config): array
125
    {
126
        return iDokladAbstractFunction::assertConfiguration($config);
127
    }
128
}
129