Completed
Push — master ( cb219e...c0b8c4 )
by Joshua
13s queued 10s
created

Client::getWorkspace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 11
    public function __construct(ClientInterface $client, string $workspace)
52
    {
53 11
        $this->client = $client;
54 11
        $this->workspace = $workspace;
55 11
    }
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
     * Returns the current configured workspace
137
     *
138
     * @return string
139
     */
140 1
    public function getWorkspace(): string
141
    {
142 1
        return $this->workspace;
143
    }
144
145
    /**
146
     * @param string $method
147
     * @param string|Uri $url
148
     * @return string
149
     */
150 9
    private function makeApiRequest(string $method, $url): string
151
    {
152
        try {
153 9
            $response = $this->client->request($method, $url);
154 5
        } catch (GuzzleBadResponseException $e) {
155 4
            if ($e->getCode() == 401) {
156 1
                throw new UnauthorizedException('Invalid API key');
157
            }
158
159 3
            $response = $e->getResponse();
160 3
            if (is_null($response)) {
161
                throw new BaseException('Guzzle exception', $e->getCode(), $e); // @codeCoverageIgnore
162
            }
163
164 3
            if ($e->getCode() == 429) {
165 1
                throw new RateLimitException(
166 1
                    (int)($response->getHeader('x-ratelimit-limit')[0]),
167 1
                    (int)($response->getHeader('x-ratelimit-reset')[0]),
168 1
                    'Rate-limit in effect'
169
                );
170
            }
171
172 2
            $message = $e->getMessage();
173
174
            // Extract message from json error body, if available
175 2
            $body = (string)$response->getBody();
176 2
            $json = json_decode($body, true);
177 2
            if ($json && isset($json['error'])) {
178 1
                $message = $json['error'];
179
            }
180
181 2
            throw new BadResponseException($message, $e->getCode());
182 1
        } catch (GuzzleException $e) {
183 1
            throw new BaseException('Guzzle exception', $e->getCode(), $e);
184
        }
185
186 4
        $body = (string)$response->getBody();
187 4
        return $body;
188
    }
189
}
190