Completed
Push — master ( 13e315...f7f936 )
by Chiribuc
07:25 queued 11s
created

HelpDocs   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 183
Duplicated Lines 44.81 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 7
dl 82
loc 183
rs 10
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace DigitalEquation\Teamwork\Services;
4
5
use DigitalEquation\Teamwork\Exceptions\TeamworkHttpException;
6
use GuzzleHttp\Client;
7
use GuzzleHttp\Exception\ClientException;
8
use GuzzleHttp\Pool as GuzzlePool;
9
use GuzzleHttp\Psr7\Request as GuzzleRequest;
10
use GuzzleHttp\Psr7\Response;
11
use GuzzleHttp\Psr7\Stream;
12
13
class HelpDocs
14
{
15
    /**
16
     * @var \GuzzleHttp\Client
17
     */
18
    private Client $client;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
19
20
    /**
21
     * HelpDocs constructor.
22
     *
23
     * @param \GuzzleHttp\Client $client
24
     */
25
    public function __construct(Client $client)
26
    {
27
        $this->client = $client;
28
    }
29
30
    /**
31
     * Get HelpDocs sites.
32
     *
33
     * @return array
34
     * @throws \DigitalEquation\Teamwork\Exceptions\TeamworkHttpException
35
     * @throws \GuzzleHttp\Exception\GuzzleException
36
     * @throws \JsonException
37
     */
38
    public function getSites(): array
39
    {
40
        try {
41
            /** @var Response $response */
42
            $response = $this->client->get('helpdocs/sites.json');
43
            /** @var Stream $body */
44
            $body = $response->getBody();
45
46
            return json_decode($body->getContents(), true, 512, JSON_THROW_ON_ERROR);
47
        } catch (ClientException $e) {
48
            throw new TeamworkHttpException($e->getMessage(), 400);
49
        }
50
    }
51
52
    /**
53
     * Get HelpDocs site.
54
     *
55
     * @param int $siteID
56
     *
57
     * @return array
58
     * @throws \DigitalEquation\Teamwork\Exceptions\TeamworkHttpException
59
     * @throws \GuzzleHttp\Exception\GuzzleException
60
     * @throws \JsonException
61
     */
62
    public function getSite(int $siteID): array
63
    {
64
        try {
65
            /** @var Response $response */
66
            $response = $this->client->get(sprintf('helpdocs/sites/%s.json', $siteID));
67
            /** @var Stream $body */
68
            $body = $response->getBody();
69
70
            return json_decode($body->getContents(), true, 512, JSON_THROW_ON_ERROR);
71
        } catch (ClientException $e) {
72
            throw new TeamworkHttpException($e->getMessage(), 400);
73
        }
74
    }
75
76
    /**
77
     * Get articles within a category.
78
     *
79
     * @param int $categoryID
80
     * @param int $page
81
     *
82
     * @return array
83
     * @throws \DigitalEquation\Teamwork\Exceptions\TeamworkHttpException
84
     * @throws \GuzzleHttp\Exception\GuzzleException
85
     * @throws \JsonException
86
     */
87
    public function getCategoryArticles($categoryID, $page = 1): array
88
    {
89
        try {
90
            /** @var Response $response */
91
            $response = $this->client->get(sprintf('helpdocs/categories/%s/articles.json', $categoryID), [
92
                'query' => compact('page'),
93
            ]);
94
            /** @var Stream $body */
95
            $body = $response->getBody();
96
97
            return json_decode($body->getContents(), true, 512, JSON_THROW_ON_ERROR);
98
        } catch (ClientException $e) {
99
            throw new TeamworkHttpException($e->getMessage(), 400);
100
        }
101
    }
102
103
    /**
104
     * Get articles within a site.
105
     *
106
     * @param int $siteID
107
     * @param int $page
108
     *
109
     * @return array
110
     * @throws \DigitalEquation\Teamwork\Exceptions\TeamworkHttpException
111
     * @throws \GuzzleHttp\Exception\GuzzleException
112
     * @throws \JsonException
113
     */
114
    public function getSiteArticles(int $siteID, $page = 1): array
115
    {
116
        try {
117
            /** @var Response $response */
118
            $response = $this->client->get(sprintf('helpdocs/sites/%s/articles.json', $siteID), [
119
                'query' => compact('page'),
120
            ]);
121
            /** @var Stream $body */
122
            $body = $response->getBody();
123
124
            return json_decode($body->getContents(), true, 512, JSON_THROW_ON_ERROR);
125
        } catch (ClientException $e) {
126
            throw new TeamworkHttpException($e->getMessage(), 400);
127
        }
128
    }
129
130
    /**
131
     * Get article by id.
132
     *
133
     * @param int $articleID
134
     *
135
     * @return array
136
     * @throws \DigitalEquation\Teamwork\Exceptions\TeamworkHttpException
137
     * @throws \GuzzleHttp\Exception\GuzzleException
138
     * @throws \JsonException
139
     */
140
    public function getArticle(int $articleID): array
141
    {
142
        try {
143
            /** @var Response $response */
144
            $response = $this->client->get(sprintf('helpdocs/articles/%s.json', $articleID));
145
            /** @var Stream $body */
146
            $body = $response->getBody();
147
148
            return json_decode($body->getContents(), true, 512, JSON_THROW_ON_ERROR);
149
        } catch (ClientException $e) {
150
            throw new TeamworkHttpException($e->getMessage(), 400);
151
        }
152
    }
153
154
    /**
155
     * Get articles (in bulk).
156
     *
157
     * @param int[] $articleIDs
158
     *
159
     * @return array
160
     */
161
    public function getArticles(array $articleIDs): array
162
    {
163
        $articles = [];
164
165
        $requests = array_map(static function ($articleID) {
166
            return new GuzzleRequest('GET', sprintf('helpdocs/articles/%s.json', $articleID));
167
        }, $articleIDs);
168
169
        $pool = new GuzzlePool($this->client, $requests, [
170
            'concurrency' => 10,
171
            'fulfilled'   => function ($response) use (&$articles) {
172
                $response = json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR);
173
174
                $articles[] = $response['article'];
175
            },
176
        ]);
177
178
        $promise = $pool->promise();
179
        $promise->wait();
180
181
        return $articles;
182
    }
183
184
    /**
185
     * Get categories within a site.
186
     *
187
     * @param int $siteID
188
     *
189
     * @return array
190
     * @throws \DigitalEquation\Teamwork\Exceptions\TeamworkHttpException
191
     * @throws \GuzzleHttp\Exception\GuzzleException
192
     * @throws \JsonException
193
     */
194
    public function getSiteCategories(int $siteID): array
195
    {
196
        try {
197
            /** @var Response $response */
198
            $response = $this->client->get(sprintf('helpdocs/sites/%s/categories.json', $siteID));
199
            /** @var Stream $body */
200
            $body = $response->getBody();
201
202
            return json_decode($body->getContents(), true, 512, JSON_THROW_ON_ERROR);
203
        } catch (ClientException $e) {
204
            throw new TeamworkHttpException($e->getMessage(), 400);
205
        }
206
    }
207
}
208