ZfCacheAdapter   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 54
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A contains() 0 4 1
A cache() 0 3 1
A delete() 0 3 1
A fetch() 0 16 3
A purge() 0 4 1
A buildKey() 0 3 1
1
<?php
2
3
namespace Columnis\Model;
4
5
use Zend\Cache\Storage\StorageInterface;
6
use GuzzleHttp\Subscriber\Cache\CacheStorageInterface;
7
use GuzzleHttp\Message\RequestInterface;
8
use GuzzleHttp\Message\Response;
9
use GuzzleHttp\Message\ResponseInterface;
10
use GuzzleHttp\Message\MessageFactory;
11
12
class ZfCacheAdapter implements CacheStorageInterface {
13
14
    /**
15
     *
16
     * @var StorageInterface 
17
     */
18
    private $storageCache = null;
19
20
    /**
21
     * @param StorageInterface $cache Zend Framework 2 cache adapter
22
     */
23
    public function __construct(StorageInterface $cache) {
24
        $this->storageCache = $cache;
25
    }
26
27
    public function contains(RequestInterface $request) {
28
        $ret = $this->storageCache->hasItem($this->buildKey($request));
29
        return $ret;
30
    }
31
32
    public function cache(RequestInterface $request, ResponseInterface $response) {
33
        return $this->storageCache->setItem($this->buildKey($request), $response);
34
    }
35
36
    public function delete(RequestInterface $request) {
37
        return $this->storageCache->removeItem($this->buildKey($request));
38
    }
39
40
    public function fetch(RequestInterface $request) {
41
        $message = $this->storageCache->getItem($this->buildKey($request));
42
	
43
	if (!$message) {
44
	    return null;
45
	}
46
	
47
	$factory = new MessageFactory();
48
	$response = $factory->fromMessage($message);
0 ignored issues
show
Bug Compatibility introduced by
The expression $factory->fromMessage($message); of type GuzzleHttp\Message\Respo...essage\RequestInterface adds the type GuzzleHttp\Message\RequestInterface to the return on line 54 which is incompatible with the return type declared by the interface GuzzleHttp\Subscriber\Ca...StorageInterface::fetch of type null|GuzzleHttp\Message\ResponseInterface.
Loading history...
49
	
50
	if ($response->getStatusCode() != 200) {
0 ignored issues
show
Bug introduced by
The method getStatusCode does only exist in GuzzleHttp\Message\ResponseInterface, but not in GuzzleHttp\Message\RequestInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
51
	    return null;
52
	}
53
	        
54
	return $response;
55
    }
56
57
    public function purge($url) {
58
	// @@TODO REVISAR ESTE METODO
59
        return $this->storageCache->removeItem($this->buildKey($url));
0 ignored issues
show
Documentation introduced by
$url is of type string, but the function expects a object<GuzzleHttp\Message\RequestInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
60
    }
61
62
    private function buildKey(RequestInterface $request) {        
63
        return md5($request->getUrl());
64
    }
65
}
66
67
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
68