Passed
Push — master ( 69013a...5649e5 )
by Joshua
11:51 queued 09:34
created

Client   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Test Coverage

Coverage 7.55%

Importance

Changes 0
Metric Value
eloc 48
dl 0
loc 140
ccs 4
cts 53
cp 0.0755
rs 10
c 0
b 0
f 0
wmc 15

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getWorkspaceCollection() 0 6 1
A __construct() 0 4 1
A getContentType() 0 6 1
A getAssetCollection() 0 6 1
A getContentTypeCollection() 0 6 1
A getEntry() 0 6 1
A getContentCollection() 0 10 1
B makeApiRequest() 0 38 8
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the -SeamsCMSDeliverySdk package.
7
 *
8
 * (c) Seams-CMS.com
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace SeamsCMS\Delivery;
15
16
use GuzzleHttp\ClientInterface;
17
use GuzzleHttp\Exception\BadResponseException as GuzzleBadResponseException;
18
use GuzzleHttp\Exception\GuzzleException;
19
use GuzzleHttp\Psr7\Uri;
20
use SeamsCMS\Delivery\Exception\BadResponseException;
21
use SeamsCMS\Delivery\Exception\BaseException;
22
use SeamsCMS\Delivery\Exception\RateLimitException;
23
use SeamsCMS\Delivery\Exception\UnauthorizedException;
24
use SeamsCMS\Delivery\Model\AssetCollection;
25
use SeamsCMS\Delivery\Model\Content;
26
use SeamsCMS\Delivery\Model\ContentCollection;
27
use SeamsCMS\Delivery\Model\ContentType;
28
use SeamsCMS\Delivery\Model\ContentTypeCollection;
29
use SeamsCMS\Delivery\Model\WorkspaceCollection;
30
31
/**
32
 * Class Client
33
 * @package SeamsCMS\Delivery
34
 *
35
 * @SuppressWarnings("Coupling")
36
 */
37
class Client
38
{
39
    /** @var string */
40
    protected $workspace;
41
42
    /** @var ClientInterface */
43
    protected $client;
44
45
    /**
46
     * Client constructor.
47
     *
48
     * @param ClientInterface $client
49
     * @param string $workspace
50
     */
51 1
    public function __construct(ClientInterface $client, string $workspace)
52
    {
53 1
        $this->client = $client;
54 1
        $this->workspace = $workspace;
55 1
    }
56
57
    /**
58
     * @return WorkspaceCollection
59
     */
60
    public function getWorkspaceCollection(): WorkspaceCollection
61
    {
62
        $json = $this->makeApiRequest('get', sprintf('/workspace/%s', $this->workspace));
63
64
        $data = json_decode($json, true);
65
        return WorkspaceCollection::fromArray($data);
66
    }
67
68
69
    /**
70
     * @return AssetCollection
71
     */
72
    public function getAssetCollection(): AssetCollection
73
    {
74
        $json = $this->makeApiRequest('get', sprintf('/workspace/%s/assets', $this->workspace));
75
76
        $data = json_decode($json, true);
77
        return AssetCollection::fromArray($data);
78
    }
79
80
    /**
81
     * @return ContentTypeCollection
82
     */
83
    public function getContentTypeCollection(): ContentTypeCollection
84
    {
85
        $json = $this->makeApiRequest('get', sprintf('/workspace/%s/types', $this->workspace));
86
87
        $data = json_decode($json, true);
88
        return ContentTypeCollection::fromArray($data);
89
    }
90
91
    /**
92
     * @param string $type
93
     * @return ContentType
94
     */
95
    public function getContentType(string $type): ContentType
96
    {
97
        $json = $this->makeApiRequest('get', sprintf('/workspace/%s/type/%s', $this->workspace, $type));
98
99
        $data = json_decode($json, true);
100
        return ContentType::fromArray($data);
101
    }
102
103
    /**
104
     * @param string $type
105
     *
106
     * @param Filter|null $filter
107
     * @return ContentCollection
108
     */
109
    public function getContentCollection(string $type, Filter $filter = null): ContentCollection
110
    {
111
        $queryString = ParseFilter::generateQueryString($filter);
112
113
        $json = $this->makeApiRequest('get',
114
            sprintf('/workspace/%s/type/%s/entries?%s', $this->workspace, $type, $queryString)
115
        );
116
117
        $data = json_decode($json, true);
118
        return ContentCollection::fromArray($data);
119
    }
120
121
    /**
122
     * @param string $entryId
123
     *
124
     * @return Content
125
     */
126
    public function getEntry(string $entryId): Content
127
    {
128
        $json = $this->makeApiRequest('get', sprintf('/workspace/%s/entry/%s', $this->workspace, $entryId));
129
130
        $data = json_decode($json, true);
131
        return Content::fromArray($data);
132
    }
133
134
    /**
135
     * @param string $method
136
     * @param string|Uri $url
137
     * @return string
138
     */
139
    private function makeApiRequest(string $method, $url): string
140
    {
141
        try {
142
            $response = $this->client->request($method, $url);
143
        } catch (GuzzleBadResponseException $e) {
144
            if ($e->getCode() == 401) {
145
                throw new UnauthorizedException('Invalid API key');
146
            }
147
148
            $response = $e->getResponse();
149
            if (is_null($response)) {
150
                throw new BaseException('Guzzle exception', $e->getCode(), $e);
151
            }
152
153
            if ($e->getCode() == 429) {
154
                throw new RateLimitException(
155
                    (int)($response->getHeader('x-ratelimit-limit')[0]),
156
                    (int)($response->getHeader('x-ratelimit-reset')[0]),
157
                    'Rate-limit in effect'
158
                );
159
            }
160
161
            $message = $e->getMessage();
162
163
            // Extract message from json error body, if available
164
            $body = (string)$response->getBody();
165
            $json = json_decode($body, true);
166
            if ($json && isset($json['error'])) {
167
                $message = $json['error'];
168
            }
169
170
            throw new BadResponseException($message, $e->getCode());
171
        } catch (GuzzleException $e) {
172
            throw new BaseException('Guzzle exception', $e->getCode(), $e);
173
        }
174
175
        $body = (string)$response->getBody();
176
        return $body;
177
    }
178
}
179