Test Setup Failed
Pull Request — master (#2)
by Craig
02:55
created

LaravelYourlsPlugin::dbStats()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 4
c 2
b 1
f 0
nc 1
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
namespace Phpsa\LaravelYourlsPlugin;
4
5
use GuzzleHttp\Client;
6
use Phpsa\LaravelYourlsPlugin\Response;
7
8
class LaravelYourlsPlugin
9
{
10
    /**
11
     * Athentication params.
12
     *
13
     * @var array
14
     */
15
    protected $authParam;
16
17
    /**
18
     * Request Client.
19
     *
20
     * @var Client::class
21
     */
22
    protected $client;
23
24
    /**
25
     * Available file formats to comunicate with Yourls API.
26
     *
27
     * @var string
28
     */
29
    private $formats = ['json', 'xml', 'simple'];
30
31
    /**
32
     * Available filters for statistics.
33
     *
34
     * @var string
35
     */
36
    private $filters = ['top', 'bottom', 'rand', 'last'];
37
38
    /**
39
     * Response format.
40
     *
41
     * @var string
42
     */
43
    public $format = 'json';
44
    /**
45
     * Response filter for stats.
46
     *
47
     * @var string
48
     */
49
    public $filter = 'top';
50
51
    /**
52
     * Last request.
53
     *
54
     * @var \stdClass
55
     */
56
    protected $lastResponse;
57
58
    /**
59
     * Construct our instance.
60
     *
61
     * @param array $configs
62
     */
63
    public function __construct(array $configs)
64
    {
65
        $this->format = $configs['format'];
66
        $this->authParam = $this->getRequestAuthentication($configs);
67
        $this->client = new Client(['base_uri' => $configs['url']]);
68
    }
69
70
    /**
71
     * setup our authentication params.
72
     *
73
     * @param array $config
74
     *
75
     * @return array array for authentication
76
     */
77
    protected function getRequestAuthentication(array $config)
78
    {
79
        if ($config['signature']) {
80
            return ['signature' => $config['signature']];
81
        } else {
82
            return [
83
                'username' => $config['username'],
84
                'password' => $config['password'],
85
            ];
86
        }
87
    }
88
89
    /**
90
     * Get short URL for a link.
91
     *
92
     * @param string $url to shorten
93
     * @param string $title title for url
94
     * @param string $keyword [optional] for custom short URLs
95
     * @param string $format [optional] either "json" or "xml"
96
     * @return string
97
     */
98
    public function shorturl(string $url, string $title = null, string $keyword = null, string $format = null)
99
    {
100
        $params = [
101
            'action' => 'shorturl',
102
            'url' => $url,
103
            'format' => $this->setFormat($format),
104
        ];
105
        if ($title) {
106
            $params['title'] = $title;
107
        }
108
        if ($keyword) {
109
            $params['keyword'] = $keyword;
110
        }
111
        $body = $this->process($params);
112
        return $body->shorturl;
113
    }
114
115
    /**
116
     * Get long URL of a shorturl.
117
     *
118
     * @param string $shorturl to expand (can be either 'abc' or 'http://site/abc')
119
     * @param string $format [optional] either "json" or "xml"
120
     * @return Response
121
     */
122
    public function expand(string $shorturl, string $format = null)
123
    {
124
        $params = [
125
            'action' => 'expand',
126
            'shorturl' => $shorturl,
127
            'format' => $this->setFormat($format),
128
        ];
129
130
        return $this->process($params);
131
    }
132
133
    /**
134
     * Get stats about one short URL.
135
     *
136
     * @param string $shorturl for which to get stats (can be either 'abc' or 'http://site/abc')
137
     * @param string $format [optional] either "json" or "xml"
138
     * @return Response
139
     */
140
    public function urlStats(string $shorturl, string $format = null)
141
    {
142
143
        $params = [
144
            'action' => 'url-stats',
145
            'shorturl' => $shorturl,
146
            'format' => $this->setFormat($format)
147
        ];
148
149
        return $this->process($params);
150
    }
151
152
    /**
153
     * Get stats about your links.
154
     *
155
     * @param string $filter [optional] either "top", "bottom" , "rand" or "last"
156
     * @param int [optional] $limit maximum number of links to return
157
     * @param string $format [optional] either "json" or "xml"
158
     * @return Response
159
     */
160
    public function stats(string $filter = null, int $limit = null, string $format = null)
161
    {
162
        $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

162
        $filter = empty($filter) || !in_array($filter, /** @scrutinizer ignore-type */ $this->filters) ? $this->filter : $filter;
Loading history...
163
164
        $params = [
165
            'action' => 'stats',
166
            'filter' => $filter,
167
            'format' => $this->setFormat($format)
168
        ];
169
        if (! empty($limit)) {
170
            $params = array_merge($params, ['limit' => $limit]);
171
        }
172
173
        return $this->process($params);
174
    }
175
176
    /**
177
     * Get database stats.
178
     *
179
     * @param string $format [optional] either "json" or "xml"
180
     * @return Response
181
     */
182
    public function dbStats(string $format = null)
183
    {
184
185
        $params = [
186
            'action' => 'db-stats',
187
            'format' => $this->setFormat($format)
188
        ];
189
190
        return $this->process($params);
191
    }
192
193
    /**
194
     * processes our request.
195
     *
196
     * @param array $request
197
     *
198
     * @throws \Exception
199
     *
200
     * @return Response
201
     */
202
    protected function process(array $request)
203
    {
204
        $format = $request['format'];
0 ignored issues
show
Unused Code introduced by
The assignment to $format is dead and can be removed.
Loading history...
205
        $form_params = array_merge($request, $this->authParam);
206
207
        $result = $this->client->request('POST', 'yourls-api.php', ['form_params' => $form_params]);
208
        if(!$result || '200' != $result->getStatusCode()) {
209
            throw new \Exception('Failed to process request');
210
        }
211
212
        $body = $result->getBody();
213
        $this->lastResponse = new Response($body);
0 ignored issues
show
Documentation Bug introduced by
It seems like new Phpsa\LaravelYourlsPlugin\Response($body) of type Phpsa\LaravelYourlsPlugin\Response is incompatible with the declared type stdClass 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...
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

213
        $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...
214
215
        return $this->lastResponse;
216
217
218
    }
219
220
    /**
221
     * Returns the result of the last request in.
222
     *
223
     * @return string|\stdClass
224
     */
225
    public function getLastResponse()
226
    {
227
        return $this->lastResponse;
228
    }
229
230
    /**
231
     * checks the format is of a correct value else defaults to the default format
232
     *
233
     * @param string $format
234
     *
235
     * @return string
236
     */
237
    protected function setFormat(string $format = NULL){
238
        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

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