XKCDService::getComicFromUrl()   A
last analyzed

Complexity

Conditions 2
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.5

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 8
ccs 3
cts 6
cp 0.5
rs 10
c 0
b 0
f 0
cc 2
nc 3
nop 1
crap 2.5
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: famoser
5
 * Date: 04/09/2017
6
 * Time: 13:24
7
 */
8
9
namespace Famoser\XKCDCache\Services;
10
11
12
use Exception;
13
use Famoser\XKCDCache\Exceptions\ServerException;
14
use Famoser\XKCDCache\Models\XKCD\XKCDJson;
15
use Famoser\XKCDCache\Services\Base\BaseService;
16
use Famoser\XKCDCache\Services\Interfaces\XKCDServiceInterface;
17
use Famoser\XKCDCache\Types\ServerError;
18
19
class XKCDService extends BaseService implements XKCDServiceInterface
20
{
21
    /**
22
     * fetches the url from the specified URL
23
     *
24
     * @param $url
25
     * @return XKCDJson
26
     * @throws ServerException
27
     */
28 4
    protected function getComicFromUrl($url)
29
    {
30
        try {
31 4
            $newestJson = file_get_contents($url);
32 4
            return json_decode($newestJson);
33
        } catch (Exception $ex) {
34
            $this->getLoggingService()->log("failed to fetch comic from xkcd: " . $ex);
35
            throw new ServerException(ServerError::CONNECTION_FAILED);
36
        }
37
    }
38
39
    /**
40
     * returns the newest XKCD comic
41
     *
42
     * @param $number
43
     * @return XKCDJson
44
     * @throws ServerException
45
     */
46 3
    public function getComic($number)
47
    {
48 3
        return $this->getComicFromUrl("https://xkcd.com/" . $number . "/info.0.json");
49
    }
50
51
    /**
52
     * returns the XKCD comic with the specified number
53
     *
54
     * @return XKCDJson
55
     * @throws ServerException
56
     */
57 4
    public function getNewestComic()
58
    {
59 4
        return $this->getComicFromUrl("https://xkcd.com/info.0.json");
60
    }
61
}