RequestService   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A requestContent() 0 23 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: thiago
5
 * Date: 31/03/18
6
 * Time: 10:46
7
 */
8
9
namespace MyWonderland\Service;
10
11
use GuzzleHttp\Client;
12
use phpFastCache\Core\Pool\ExtendedCacheItemPoolInterface;
13
14
class RequestService
15
{
16
    /**
17
     * @var ExtendedCacheItemPoolInterface
18
     */
19
    protected $instanceCache;
20
21
    /**
22
     * ClientService constructor.
23
     */
24
    public function __construct()
25
    {
26
        $this->instanceCache = \phpFastCache\CacheManager::getInstance('files');
27
    }
28
29
30
    /**
31
     * @param $method
32
     * @param string $uri
33
     * @param array $options
34
     * @param string $cacheKey
35
     * @return mixed
36
     */
37
    public function requestContent($method, $uri = '', array $options = [], $cacheKey = '') {
38
        $cacheKey = md5($cacheKey);
39
40
        $cachedString = $this->instanceCache->getItem($cacheKey);
41
42
        if (is_null($cachedString->get())) {
43
            $client = new Client();
44
45
            $res = $client->request($method, $uri, $options);
46
            $content = json_decode($res->getBody()->getContents(), true);
47
48
            //in seconds, also accepts Datetime
49
            $cachedString->set($content)->expiresAfter(30 * 24 * 3600);
50
51
            // Save the cache item just like you do with doctrine and entities
52
            $this->instanceCache->save($cachedString);
53
54
            // "FIRST LOAD // WROTE OBJECT TO CACHE // RELOAD THE PAGE AND SEE // ";
55
            return $cachedString->get();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $cachedString->get() targeting Psr\Cache\CacheItemInterface::get() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
56
        }
57
58
        // "READ FROM CACHE // ";
59
        return $cachedString->get();
60
    }
61
62
}