XKCDService   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 70%

Importance

Changes 0
Metric Value
wmc 4
eloc 9
dl 0
loc 41
ccs 7
cts 10
cp 0.7
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getComic() 0 3 1
A getNewestComic() 0 3 1
A getComicFromUrl() 0 8 2
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
}