Passed
Push — master ( c10be1...cb219e )
by Joshua
03:34 queued 11s
created

Client::getContentCollection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 11
ccs 0
cts 7
cp 0
crap 2
rs 10
c 0
b 0
f 0
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 10
    public function __construct(ClientInterface $client, string $workspace)
52
    {
53 10
        $this->client = $client;
54 10
        $this->workspace = $workspace;
55 10
    }
56
57
    /**
58
     * @return WorkspaceCollection
59
     */
60 1
    public function getWorkspaceCollection(): WorkspaceCollection
61
    {
62 1
        $json = $this->makeApiRequest('get', sprintf('/workspace/%s', $this->workspace));
63
64 1
        $data = json_decode($json, true);
65 1
        return WorkspaceCollection::fromArray($data);
66
    }
67
68
69
    /**
70
     * @return AssetCollection
71
     */
72 6
    public function getAssetCollection(): AssetCollection
73
    {
74 6
        $json = $this->makeApiRequest('get', sprintf('/workspace/%s/assets', $this->workspace));
75
76 1
        $data = json_decode($json, true);
77 1
        return AssetCollection::fromArray($data);
78
    }
79
80
    /**
81
     * @return ContentTypeCollection
82
     */
83 1
    public function getContentTypeCollection(): ContentTypeCollection
84
    {
85 1
        $json = $this->makeApiRequest('get', sprintf('/workspace/%s/types', $this->workspace));
86
87 1
        $data = json_decode($json, true);
88 1
        return ContentTypeCollection::fromArray($data);
89
    }
90
91
    /**
92
     * @param string $type
93
     * @return ContentType
94
     */
95 1
    public function getContentType(string $type): ContentType
96
    {
97 1
        $json = $this->makeApiRequest('get', sprintf('/workspace/%s/type/%s', $this->workspace, $type));
98
99 1
        $data = json_decode($json, true);
100 1
        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(
114
            'get',
115
            sprintf('/workspace/%s/type/%s/entries?%s', $this->workspace, $type, $queryString)
116
        );
117
118
        $data = json_decode($json, true);
119
        return ContentCollection::fromArray($data);
120
    }
121
122
    /**
123
     * @param string $entryId
124
     *
125
     * @return Content
126
     */
127
    public function getEntry(string $entryId): Content
128
    {
129
        $json = $this->makeApiRequest('get', sprintf('/workspace/%s/entry/%s', $this->workspace, $entryId));
130
131
        $data = json_decode($json, true);
132
        return Content::fromArray($data);
133
    }
134
135
    /**
136
     * @param string $method
137
     * @param string|Uri $url
138
     * @return string
139
     */
140 9
    private function makeApiRequest(string $method, $url): string
141
    {
142
        try {
143 9
            $response = $this->client->request($method, $url);
144 5
        } catch (GuzzleBadResponseException $e) {
145 4
            if ($e->getCode() == 401) {
146 1
                throw new UnauthorizedException('Invalid API key');
147
            }
148
149 3
            $response = $e->getResponse();
150 3
            if (is_null($response)) {
151
                throw new BaseException('Guzzle exception', $e->getCode(), $e);
152
            }
153
154 3
            if ($e->getCode() == 429) {
155 1
                throw new RateLimitException(
156 1
                    (int)($response->getHeader('x-ratelimit-limit')[0]),
157 1
                    (int)($response->getHeader('x-ratelimit-reset')[0]),
158 1
                    'Rate-limit in effect'
159
                );
160
            }
161
162 2
            $message = $e->getMessage();
163
164
            // Extract message from json error body, if available
165 2
            $body = (string)$response->getBody();
166 2
            $json = json_decode($body, true);
167 2
            if ($json && isset($json['error'])) {
168 1
                $message = $json['error'];
169
            }
170
171 2
            throw new BadResponseException($message, $e->getCode());
172 1
        } catch (GuzzleException $e) {
173 1
            throw new BaseException('Guzzle exception', $e->getCode(), $e);
174
        }
175
176 4
        $body = (string)$response->getBody();
177 4
        return $body;
178
    }
179
}
180