Completed
Pull Request — master (#2)
by Craig
02:58
created

LaravelYourlsPlugin::setFormat()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 1
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Phpsa\LaravelYourlsPlugin;
4
5
use GuzzleHttp\Client;
6
7
class LaravelYourlsPlugin
8
{
9
    /**
10
     * Athentication params.
11
     *
12
     * @var array
13
     */
14
    protected $authParam;
15
16
    /**
17
     * Request Client.
18
     *
19
     * @var Client::class
20
     */
21
    protected $client;
22
23
    /**
24
     * Available file formats to comunicate with Yourls API.
25
     *
26
     * @var array
27
     */
28
    private $formats = ['json', 'xml', 'simple'];
29
30
    /**
31
     * Available filters for statistics.
32
     *
33
     * @var array
34
     */
35
    private $filters = ['top', 'bottom', 'rand', 'last'];
36
37
    /**
38
     * Response format.
39
     *
40
     * @var string
41
     */
42
    public $format = 'json';
43
    /**
44
     * Response filter for stats.
45
     *
46
     * @var string
47
     */
48
    public $filter = 'top';
49
50
    /**
51
     * Last request.
52
     *
53
     * @var Response
54
     */
55
    protected $lastResponse;
56
57
    /**
58
     * Construct our instance.
59
     *
60
     * @param array $configs
61
     */
62
    public function __construct(array $configs)
63
    {
64
        $this->format = $configs['format'];
65
        $this->authParam = $this->getRequestAuthentication($configs);
66
        $this->client = new Client(['base_uri' => $configs['url']]);
67
    }
68
69
    /**
70
     * setup our authentication params.
71
     *
72
     * @param array $config
73
     *
74
     * @return array array for authentication
75
     */
76
    protected function getRequestAuthentication(array $config)
77
    {
78
        if ($config['signature']) {
79
            return ['signature' => $config['signature']];
80
        } else {
81
            return [
82
                'username' => $config['username'],
83
                'password' => $config['password'],
84
            ];
85
        }
86
    }
87
88
    /**
89
     * Get short URL for a link.
90
     *
91
     * @param string $url to shorten
92
     * @param string $title title for url
93
     * @param string $keyword [optional] for custom short URLs
94
     * @param string $format [optional] either "json" or "xml"
95
     * @return string
96
     */
97
    public function shorturl(string $url, string $title = null, string $keyword = null, string $format = null)
98
    {
99
        $params = [
100
            'action' => 'shorturl',
101
            'url' => $url,
102
            'format' => $this->setFormat($format),
103
        ];
104
        if ($title) {
105
            $params['title'] = $title;
106
        }
107
        if ($keyword) {
108
            $params['keyword'] = $keyword;
109
        }
110
        $body = $this->process($params);
111
        return $body->shorturl;
112
    }
113
114
    /**
115
     * Get long URL of a shorturl.
116
     *
117
     * @param string $shorturl to expand (can be either 'abc' or 'http://site/abc')
118
     * @param string $format [optional] either "json" or "xml"
119
     * @return Response
120
     */
121
    public function expand(string $shorturl, string $format = null)
122
    {
123
        $params = [
124
            'action' => 'expand',
125
            'shorturl' => $shorturl,
126
            'format' => $this->setFormat($format),
127
        ];
128
129
        return $this->process($params);
130
    }
131
132
    /**
133
     * Get stats about one short URL.
134
     *
135
     * @param string $shorturl for which to get stats (can be either 'abc' or 'http://site/abc')
136
     * @param string $format [optional] either "json" or "xml"
137
     * @return Response
138
     */
139
    public function urlStats(string $shorturl, string $format = null)
140
    {
141
142
        $params = [
143
            'action' => 'url-stats',
144
            'shorturl' => $shorturl,
145
            'format' => $this->setFormat($format),
146
        ];
147
148
        return $this->process($params);
149
    }
150
151
    /**
152
     * Get stats about your links.
153
     *
154
     * @param string $filter [optional] either "top", "bottom" , "rand" or "last"
155
     * @param int [optional] $limit maximum number of links to return
156
     * @param string $format [optional] either "json" or "xml"
157
     * @return Response
158
     */
159
    public function stats(string $filter = null, int $limit = null, string $format = null)
160
    {
161
        $filter = empty($filter) || ! in_array($filter, $this->filters) ? $this->filter : $filter;
162
163
        $params = [
164
            'action' => 'stats',
165
            'filter' => $filter,
166
            'format' => $this->setFormat($format),
167
        ];
168
        if (! empty($limit)) {
169
            $params = array_merge($params, ['limit' => $limit]);
170
        }
171
172
        return $this->process($params);
173
    }
174
175
    /**
176
     * Get database stats.
177
     *
178
     * @param string $format [optional] either "json" or "xml"
179
     * @return Response
180
     */
181
    public function dbStats(string $format = null)
182
    {
183
184
        $params = [
185
            'action' => 'db-stats',
186
            'format' => $this->setFormat($format),
187
        ];
188
189
        return $this->process($params);
190
    }
191
192
    /**
193
     * processes our request.
194
     *
195
     * @param array $request
196
     *
197
     * @throws \Exception
198
     *
199
     * @return Response
200
     */
201
    protected function process(array $request)
202
    {
203
        $form_params = array_merge($request, $this->authParam);
204
205
        $result = $this->client->request('POST', 'yourls-api.php', ['form_params' => $form_params]);
206
        if (! $result || '200' != $result->getStatusCode()) {
207
            throw new \Exception('Failed to process request');
208
        }
209
210
        $body = $result->getBody();
211
        $this->lastResponse = new Response($body, $request['format']);
212
213
        return $this->lastResponse;
214
215
    }
216
217
    /**
218
     * Returns the result of the last request in.
219
     *
220
     * @return Response
221
     */
222
    public function getLastResponse()
223
    {
224
        return $this->lastResponse;
225
    }
226
227
    /**
228
     * checks the format is of a correct value else defaults to the default format.
229
     *
230
     * @param string $format
231
     *
232
     * @return string
233
     */
234
    protected function setFormat(string $format = NULL)
235
    {
236
        return empty($format) || ! in_array($format, $this->formats) ? $this->format : $format;
237
    }
238
}
239