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

LaravelYourlsPlugin::shorturl()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 3
eloc 10
c 2
b 1
f 0
nc 4
nop 4
dl 0
loc 15
rs 9.9332
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 string
27
     */
28
    private $formats = ['json', 'xml', 'simple'];
29
30
    /**
31
     * Available filters for statistics.
32
     *
33
     * @var string
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 Request
0 ignored issues
show
Bug introduced by
The type Phpsa\LaravelYourlsPlugin\Request was not found. Did you mean Request? If so, make sure to prefix the type with \.
Loading history...
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;
0 ignored issues
show
Bug introduced by
$this->filters of type string is incompatible with the type array expected by parameter $haystack of in_array(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

161
        $filter = empty($filter) || ! in_array($filter, /** @scrutinizer ignore-type */ $this->filters) ? $this->filter : $filter;
Loading history...
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
        $format = $request['format'];
0 ignored issues
show
Unused Code introduced by
The assignment to $format is dead and can be removed.
Loading history...
204
        $form_params = array_merge($request, $this->authParam);
205
206
        $result = $this->client->request('POST', 'yourls-api.php', ['form_params' => $form_params]);
207
        if(! $result || '200' != $result->getStatusCode()) {
208
            throw new \Exception('Failed to process request');
209
        }
210
211
        $body = $result->getBody();
212
        $this->lastResponse = new Response($body);
0 ignored issues
show
Bug introduced by
The call to Phpsa\LaravelYourlsPlugin\Response::__construct() has too few arguments starting with format. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

212
        $this->lastResponse = /** @scrutinizer ignore-call */ new Response($body);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Documentation Bug introduced by
It seems like new Phpsa\LaravelYourlsPlugin\Response($body) of type Phpsa\LaravelYourlsPlugin\Response is incompatible with the declared type Phpsa\LaravelYourlsPlugin\Request of property $lastResponse.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
213
214
        return $this->lastResponse;
215
216
    }
217
218
    /**
219
     * Returns the result of the last request in.
220
     *
221
     * @return Response
222
     */
223
    public function getLastResponse()
224
    {
225
        return $this->lastResponse;
226
    }
227
228
    /**
229
     * checks the format is of a correct value else defaults to the default format
230
     *
231
     * @param string $format
232
     *
233
     * @return string
234
     */
235
    protected function setFormat(string $format = NULL){
236
        return empty($format) || !in_array($format, $this->formats) ? $this->format: $format;
0 ignored issues
show
Bug introduced by
$this->formats of type string is incompatible with the type array expected by parameter $haystack of in_array(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

236
        return empty($format) || !in_array($format, /** @scrutinizer ignore-type */ $this->formats) ? $this->format: $format;
Loading history...
237
    }
238
}
239