Completed
Push — master ( 346425...6beaf7 )
by Jan Philip
05:33
created

CachedNetworkService   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 58
ccs 0
cts 25
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getWeekWithLocation() 0 19 2
A buildCacheKey() 0 4 1
1
<?php
2
3
namespace JPBernius\FMeat\Services;
4
5
use GuzzleHttp\Client as HttpClient;
6
use JPBernius\FMeat\Utilities\UrlBuilder;
7
use JPBernius\FMeat\Entities\{
8
    CalendarWeek,
9
    Week
10
};
11
use Stash\Driver\FileSystem as FileSystemDriver;
12
use Stash\Pool as CachePool;
13
14
/**
15
 * Class CachedNetworkService
16
 * @package JPBernius\FMeat\Services
17
 */
18
class CachedNetworkService extends NetworkService
19
{
20
    /** @var CachePool */
21
    private $pool;
22
23
    /**
24
     * CachedNetworkService constructor.
25
     * @param HttpClient $httpClient
26
     * @param UrlBuilder $urlBuilder
27
     * @param FileSystemDriver $fileSystemDriver
28
     * @param CachePool $pool
29
     */
30
    public function __construct(HttpClient $httpClient, UrlBuilder $urlBuilder,
31
                                FileSystemDriver $fileSystemDriver, CachePool $pool)
32
    {
33
        parent::__construct($httpClient, $urlBuilder);
34
35
        $pool->setDriver($fileSystemDriver);
36
        $this->pool = $pool;
37
    }
38
39
    /**
40
     * @param int $week
0 ignored issues
show
Bug introduced by
There is no parameter named $week. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
41
     * @param int $year
0 ignored issues
show
Bug introduced by
There is no parameter named $year. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
42
     * @param string $location
43
     * @return Week
44
     */
45
    public function getWeekWithLocation(CalendarWeek $calendarWeek, string $location): Week
46
    {
47
        $cacheKey = $this->buildCacheKey($location, $calendarWeek);
48
        $cacheItem = $this->pool->getItem($cacheKey);
49
50
        if($cacheItem->isHit()) {
51
            $jsonString = $cacheItem->get();
52
            $jsonObject = json_decode($jsonString);
53
54
            return Week::fromJson($jsonObject);
55
        }
56
57
        $retrievedWeek = parent::getWeekWithLocation($calendarWeek, $location);
58
59
        $retrievedWeekJson = json_encode($retrievedWeek);
60
        $this->pool->save($cacheItem->set($retrievedWeekJson));
61
62
        return $retrievedWeek;
63
    }
64
65
    /**
66
     * @param string $location
67
     * @param int $year
0 ignored issues
show
Bug introduced by
There is no parameter named $year. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
68
     * @param int $week
0 ignored issues
show
Bug introduced by
There is no parameter named $week. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
69
     * @return string
70
     */
71
    private function buildCacheKey(string $location, CalendarWeek $calendarWeek): string
72
    {
73
        return sprintf('%s/%s/%s', $location, $calendarWeek->getYear(), $calendarWeek->getWeek());
74
    }
75
}