Client::createPage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 6
1
<?php
2
3
/**
4
 * This file is part of the Telegraph package.
5
 *
6
 * (c) Arthur Edamov <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types = 1);
13
14
namespace Telegraph;
15
16
use Psr\Http\Message\ResponseInterface;
17
18
/**
19
 * Class Client
20
 * @package Telegraph
21
 */
22
class Client
23
{
24
    const BASE_URI = 'https://api.telegra.ph/';
25
26
    /**
27
     * @var \GuzzleHttp\Client
28
     */
29
    private $httpClient;
30
31
    /**
32
     * Client constructor.
33
     */
34
    public function __construct()
35
    {
36
        $this->httpClient = new \GuzzleHttp\Client(['base_uri' => self::BASE_URI]);
37
    }
38
39
    /**
40
     * @param string $shortName
41
     * @param string $authorName
42
     * @param string $authorUrl
43
     * @return Account
44
     */
45 View Code Duplication
    public function createAccount(string $shortName, string $authorName = '', string $authorUrl = ''): Account
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
    {
47
        $response = $this->httpClient->post('/createAccount', [
48
            'json' => [
49
                'short_name' => $shortName,
50
                'author_name' => $authorName,
51
                'author_url' => $authorUrl
52
            ]
53
        ]);
54
55
        return new Account(self::getDecodedResponseResult($response));
56
    }
57
58
    /**
59
     * @param string $accessToken
60
     * @param string $shortName
61
     * @param string $authorName
62
     * @param string $authorUrl
63
     * @return Account
64
     */
65 View Code Duplication
    public function editAccountInfo(string $accessToken, string $shortName, string $authorName = '', string $authorUrl = ''): Account
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
    {
67
        $response = $this->httpClient->post('/editAccountInfo', [
68
            'json' => [
69
                'access_token' => $accessToken,
70
                'short_name' => $shortName,
71
                'author_name' => $authorName,
72
                'author_url' => $authorUrl
73
            ]
74
        ]);
75
76
        return new Account(self::getDecodedResponseResult($response));
77
    }
78
79
    /**
80
     * @param string $accessToken
81
     * @param array $fields
82
     * @return Account
83
     */
84 View Code Duplication
    public function getAccountInfo(string $accessToken, array $fields = ['short_name', 'author_name', 'author_url']): Account
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
    {
86
        $response = $this->httpClient->post('/getAccountInfo', [
87
            'json' => [
88
                'access_token' => $accessToken,
89
                'fields' => $fields
90
            ]
91
        ]);
92
93
        return new Account(self::getDecodedResponseResult($response));
94
    }
95
96
    /**
97
     * @param string $accessToken
98
     * @return Account
99
     */
100
    public function revokeAccessToken(string $accessToken): Account
101
    {
102
        $response = $this->httpClient->post('/revokeAccessToken', [
103
            'json' => [
104
                'access_token' => $accessToken
105
            ]
106
        ]);
107
108
        return new Account(self::getDecodedResponseResult($response));
109
    }
110
111
112
    /**
113
     * @param string $accessToken
114
     * @param string $title
115
     * @param string|NodeElement[] $content
116
     * @param string $authorName
117
     * @param string $authorUrl
118
     * @param bool $returnContent
119
     * @return Page
120
     */
121
    public function createPage(string $accessToken, string $title, $content, string $authorName = '', string $authorUrl = '', bool $returnContent = false): Page
122
    {
123
        $response = $this->httpClient->post('/createPage', [
124
            'json' => [
125
                'access_token' => $accessToken,
126
                'title' => $title,
127
                'content' => self::decoratePageContent($content),
128
                'author_name' => $authorName,
129
                'author_url' => $authorUrl,
130
                'return_content' => $returnContent
131
            ]
132
        ]);
133
134
        return new Page(self::getDecodedResponseResult($response));
135
    }
136
137
    public function editPage(string $accessToken, string $path, string $title, $content, string $authorName = null, string $authorUrl = null, bool $returnContent = false)
138
    {
139
        $response = $this->httpClient->post('/editPage/' . $path, [
140
            'json' => array_filter([
141
                'access_token' => $accessToken,
142
                'title' => $title,
143
                'content' => self::decoratePageContent($content),
144
                'author_name' => $authorName,
145
                'author_url' => $authorUrl,
146
                'return_content' => $returnContent,
147
            ])
148
        ]);
149
150
        return new Page(self::getDecodedResponseResult($response));
151
    }
152
153
    public function getPage(string $path, bool $returnContent = false)
154
    {
155
        $response = $this->httpClient->post('/getPage/' . $path, [
156
            'json' => [
157
                'return_content' => $returnContent
158
            ]
159
        ]);
160
161
        return new Page(self::getDecodedResponseResult($response));
162
    }
163
164
    public function getPageList(string $accessToken, int $offset = 0, int $limit = 50)
165
    {
166
        $response = $this->httpClient->post('/getPageList', [
167
            'json' => [
168
                'access_token' => $accessToken,
169
                'offset' => $offset,
170
                'limit' => $limit
171
            ]
172
        ]);
173
174
        return new PageList(self::getDecodedResponseResult($response));
175
    }
176
177
    public function getViews(string $path, int $year = null, int $month = null, int $day = null, int $hour = null)
178
    {
179
        $response = $this->httpClient->post('/getPage', [
180
            'json' => [
181
                'path' => $path,
182
                'year' => $year,
183
                'month' => $month,
184
                'day' => $day,
185
                'hour' => $hour,
186
            ]
187
        ]);
188
189
        return new PageViews(self::getDecodedResponseResult($response));
190
    }
191
192
    /**
193
     * @param ResponseInterface $response
194
     * @return array
195
     * @throws RequestException
196
     */
197
    private static function getDecodedResponseResult(ResponseInterface $response): array
198
    {
199
        $decodedResponse = self::decodeResponse($response);
200
201
        if ($decodedResponse['ok'] === false) {
202
            throw new RequestException($decodedResponse['error']);
203
        }
204
205
        return $decodedResponse['result'];
206
    }
207
208
    /**
209
     * @param ResponseInterface $response
210
     * @return mixed
211
     */
212
    private static function decodeResponse(ResponseInterface $response)
213
    {
214
        return json_decode($response->getBody()->getContents(), true);
215
    }
216
217
    /**
218
     * @param NodeElement[]|string $content
219
     * @return array
220
     * @throws InvalidContentTypeException
221
     */
222
    private static function decoratePageContent($content): array
223
    {
224
        if (is_string($content)) {
225
            return [$content];
226
        } elseif (is_array($content)) {
227
            $result = [];
228
            foreach ($content as $item) {
229
                if (!$item instanceof NodeElement) {
230
                    throw new InvalidContentTypeException();
231
                }
232
                $result[] = $item->toArray();
233
            }
234
235
            return $result;
236
        }
237
238
        throw new InvalidContentTypeException();
239
    }
240
}
241