Completed
Push — master ( 4b86e5...e12edd )
by Alexey
02:17
created

Client::createAccount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 12
loc 12
rs 9.4285
cc 1
eloc 7
nc 1
nop 3
1
<?php
2
3
namespace Deploykit\Telegraph;
4
5
use Deploykit\Telegraph\Entities\Page;
6
use Deploykit\Telegraph\Entities\Account;
7
use Deploykit\Telegraph\Exceptions\TelegraphApiException;
8
use Psr\Http\Message\ResponseInterface;
9
use Deploykit\Telegraph\Entities\PageList;
10
use Deploykit\Telegraph\Entities\PageViews;
11
12
class Client
13
{
14
    protected $url = 'https://api.telegra.ph/';
15
16
    protected $http;
17
18
    public function __construct($httpClient = null)
19
    {
20
        $this->http = $httpClient ?: new \GuzzleHttp\Client(['base_uri' => $this->url]);
21
    }
22
23 View Code Duplication
    public function createAccount($shortName, $authorName = '', $authorUrl = '')
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...
24
    {
25
        $response = $this->http->post('/createAccount', [
26
            'json' => [
27
                'short_name' => $shortName,
28
                'author_name' => $authorName,
29
                'author_url' => $authorUrl
30
            ]
31
        ]);
32
33
        return new Account($this->handleResponse($response));
34
    }
35
36 View Code Duplication
    public function editAccountInfo($account, $shortName = '', $authorName = '', $authorUrl = '')
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...
37
    {
38
        $response = $this->http->post('/editAccountInfo', [
39
            'json' => [
40
                'access_token' => $this->getAccessToken($account),
41
                'short_name' => $shortName,
42
                'author_name' => $authorName,
43
                'author_url' => $authorUrl
44
            ]
45
        ]);
46
47
        return new Account($this->handleResponse($response));
48
    }
49
50
    public function getAccountInfo($account, $fields = ['short_name', 'author_name', 'author_url'])
51
    {
52
        $availableFields = ['short_name', 'author_name', 'author_url', 'auth_url', 'page_count'];
53
54
        foreach ($fields as $field) {
55
            if (!in_array($field, $availableFields)) {
56
                throw new \Exception();
57
            }
58
        }
59
60
        $response = $this->http->post('/getAccountInfo', [
61
            'json' => [
62
                'access_token' => $this->getAccessToken($account),
63
                'fields' => $fields
64
            ]
65
        ]);
66
67
        return new Account($this->handleResponse($response));
68
    }
69
70
    public function revokeAccessToken($account)
71
    {
72
        $response = $this->http->post('/revokeAccessToken', [
73
            'json' => [
74
                'access_token' => $this->getAccessToken($account)
75
            ]
76
        ]);
77
78
        return new Account($this->handleResponse($response));
79
    }
80
81
    public function createPage($account, $title, $content, $authorName = '', $authorUrl = '', $returnContent = false)
82
    {
83
        $response = $this->http->post('/createPage', [
84
            'json' => [
85
                'access_token' => $this->getAccessToken($account),
86
                'title' => $title,
87
                'content' => $content,
88
                'author_name' => $authorName,
89
                'author_url' => $authorUrl,
90
                'return_content' => $returnContent
91
            ]
92
        ]);
93
94
        return new Page($this->handleResponse($response));
95
    }
96
97
    public function editPage($account, $path, $title, $content, $authorName = null, $authorUrl = null, $returnContent = false)
98
    {
99
        $json = array_filter([
100
            'access_token' => $this->getAccessToken($account),
101
            'path' => $path,
102
            'title' => $title,
103
            'content' => $content,
104
            'author_name' => $authorName,
105
            'author_url' => $authorUrl,
106
            'return_content' => $returnContent
107
        ]);
108
109
        $response = $this->http->post('/editPage', [
110
            'json' => $json
111
        ]);
112
113
        return new Page($this->handleResponse($response));
114
    }
115
116
    public function getPage($path, $returnContent = false)
117
    {
118
        $response = $this->http->post('/getPage', [
119
            'json' => [
120
                'path' => $path,
121
                'return_content' => $returnContent
122
            ]
123
        ]);
124
125
        return new Page($this->handleResponse($response));
126
    }
127
128
    public function getPageList($account, $offset = 0, $limit = 50)
129
    {
130
        $response = $this->http->post('/getPageList', [
131
            'json' => [
132
                'access_token' => $this->getAccessToken($account),
133
                'offset' => $offset,
134
                'limit' => $limit
135
            ]
136
        ]);
137
138
        return new PageList($this->handleResponse($response));
139
    }
140
141
    public function getViews($path, $year = null, $month = null, $day = null, $hour = null)
142
    {
143
        $json = array_filter(compact('path', 'year', 'month', 'day', 'hour'));
144
145
        $response = $this->http->post('/getViews', [
146
            'json' => $json
147
        ]);
148
149
        return new PageViews($this->handleResponse($response));
150
    }
151
152
    /**
153
     * @param  \Deploykit\Telegraph\Entities\Account|string  $account
154
     * @return  mixed
155
     */
156
    protected function getAccessToken($account)
157
    {
158
        if ($account instanceof Account) {
159
            return $account['access_token'];
160
        }
161
162
        return $account;
163
    }
164
165
    /**
166
     * @param  \Psr\Http\Message\ResponseInterface  $response
167
     * @return  array  mixed
168
     */
169
    protected function handleResponse(ResponseInterface $response)
170
    {
171
        $response = json_decode($response->getBody()->getContents(), true);
172
173
        if (!$response['ok']) {
174
            throw new TelegraphApiException($response['error']);
175
        }
176
177
        return $response['result'];
178
    }
179
}
180