Pug::sendRequest()   A
last analyzed

Complexity

Conditions 2
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 3
nop 1
crap 2
1
<?php
2
3
namespace Olyckne\Pug;
4
5
use Exception;
6
use GuzzleHttp\ClientInterface;
7
8
class Pug
9
{
10
11
    /**
12
     * @var ClientInterface
13
     */
14
    private $client;
15
16
    /**
17
     * baseUrl to the pugme api
18
     *
19
     * @var string
20
     */
21
    private $baseUrl = "http://pugme.herokuapp.com/";
22
23
    /**
24
     *
25
     * @param ClientInterface $client
26
     */
27 4
    public function __construct(ClientInterface $client)
28
    {
29 4
        $this->client = $client;
30 4
    }
31
32
    /**
33
     * send a request to pugme api
34
     *
35
     * @param string $url
36
     * @return mixed
37
     * @throws PugNotFoundException
38
     */
39 3
    private function sendRequest($url)
40
    {
41
        try {
42 3
            $response = $this->client->get($this->baseUrl . $url);
43 2
            $pug = json_decode($response->getBody(), true);
44 1
        } catch (Exception $e) {
45 1
            throw new PugNotFoundException;
46
        }
47 2
        return $pug;
48
    }
49
50
    /**
51
     * Gets a link to a pug
52
     *
53
     * @return string
54
     */
55 1
    public function random()
56
    {
57 1
        return $this->get();
58
    }
59
60
    /**
61
     *
62
     * Gets a link to a pug
63
     *
64
     * @return string
65
     * @throws PugNotFoundException
66
     */
67 2
    public function get()
68
    {
69 2
        $pug = $this->sendRequest('random');
70 1
        return isset($pug['pug']) ? $pug['pug'] : '';
71
    }
72
73
74
    /**
75
     * Get multiple links to pugs
76
     *
77
     * @param int $count
78
     * @return string
79
     * @throws PugNotFoundException
80
     */
81 1
    public function bomb($count=5)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$count" and equals sign; expected 1 but found 0
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$count"; expected 1 but found 0
Loading history...
82
    {
83 1
        $pug = $this->sendRequest('bomb?count='.$count);
84 1
        return isset($pug['pugs']) ? $pug['pugs'] : '';
85
    }
86
}
87