Completed
Push — develop ( 71fd61...bbac44 )
by Jaap
06:03 queued 02:27
created

CacheMiddleware   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 79.17%

Importance

Changes 0
Metric Value
dl 0
loc 82
ccs 19
cts 24
cp 0.7917
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A disable() 0 4 1
B execute() 0 21 5
A updateCache() 0 7 1
A getItemName() 0 4 1
1
<?php
2
3
/**
4
 * This file is part of phpDocumentor.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @copyright 2010-2015 Mike van Riel<[email protected]>
10
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
11
 * @link      http://phpdoc.org
12
 */
13
14
namespace phpDocumentor\Parser\Middleware;
15
16
use phpDocumentor\Reflection\Middleware\Middleware;
17
use phpDocumentor\Reflection\Php\Factory\File\CreateCommand;
18
use phpDocumentor\Reflection\Php\File;
19
use Stash\Item;
20
use Stash\Pool;
21
22
final class CacheMiddleware implements Middleware
23
{
24
    /**
25
     * Cache namespace used for this repository.
26
     */
27
    const CACHE_NAMESPACE = 'Documentation\\Api\\Php';
28
29
    /**
30
     * Cache pool used to store files.
31
     *
32
     * @var Pool
33
     */
34
    private $dataStore;
35
36
    private $enabled = true;
37
38 2
    public function __construct(Pool $dataStore)
39
    {
40 2
        $this->dataStore = $dataStore;
41 2
    }
42
43
    public function disable()
44
    {
45
        $this->enabled = false;
46
    }
47
48
    /**
49
     * Executes this middle ware class.
50
     * A middle ware class MUST return a File object or call the $next callable.
51
     *
52
     * @param CreateCommand $command
53
     * @param callable $next
54
     *
55
     * @return File
56
     */
57 2
    public function execute($command, callable $next)
58
    {
59 2
        $itemName = $this->getItemName($command->getFile()->path());
60 2
        $item = $this->dataStore->getItem($itemName);
61 2
        if ($item->isMiss() || $this->enabled === false) {
62 1
            return $this->updateCache($command, $next, $item);
0 ignored issues
show
Compatibility introduced by
$item of type object<Stash\Interfaces\ItemInterface> is not a sub-type of object<Stash\Item>. It seems like you assume a concrete implementation of the interface Stash\Interfaces\ItemInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
63
        }
64
65
        /** @var File $cachedFile */
66 1
        $cachedFile = $item->get();
67
68 1
        if ($cachedFile === null) {
69
            return $this->updateCache($command, $next, $item);
0 ignored issues
show
Compatibility introduced by
$item of type object<Stash\Interfaces\ItemInterface> is not a sub-type of object<Stash\Item>. It seems like you assume a concrete implementation of the interface Stash\Interfaces\ItemInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
70
        }
71
72 1
        if ($cachedFile->getHash() !== $command->getFile()->md5()) {
73 1
            return $this->updateCache($command, $next, $item);
0 ignored issues
show
Compatibility introduced by
$item of type object<Stash\Interfaces\ItemInterface> is not a sub-type of object<Stash\Item>. It seems like you assume a concrete implementation of the interface Stash\Interfaces\ItemInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
74
        }
75
76
        return $cachedFile;
77
    }
78
79
    /**
80
     * @param CreateCommand $command
81
     * @param callable $next
82
     * @param Item $item
83
     * @return mixed
84
     */
85 2
    private function updateCache(CreateCommand $command, callable $next, $item)
86
    {
87 2
        $file = $next($command);
88 2
        $item->lock();
89 2
        $this->dataStore->save($item->set($file));
90 2
        return $file;
91
    }
92
93
    /**
94
     * Convert path to ItemName
95
     *
96
     * @param path
97
     * @return string
98
     */
99 2
    private function getItemName($path)
100
    {
101 2
        return static::CACHE_NAMESPACE . '\\' . md5($path);
102
    }
103
}
104