Completed
Push — develop ( 9193e7...62056c )
by Jaap
12:45 queued 02:43
created

CacheMiddleware::updateCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 3
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
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
    public function __construct(Pool $dataStore)
37
    {
38
        $this->dataStore = $dataStore;
39
    }
40
41
    /**
42
     * Executes this middle ware class.
43
     * A middle ware class MUST return a File object or call the $next callable.
44
     *
45
     * @param CreateCommand $command
46
     * @param callable $next
47
     *
48
     * @return File
49
     */
50
    public function execute($command, callable $next)
51
    {
52
        $itemName = $this->getItemName($command->getFile()->path());
53
        $item = $this->dataStore->getItem($itemName);
54
        if ($item->isMiss()) {
55
            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...
56
        }
57
58
        /** @var File $cachedFile */
59
        $cachedFile = $item->get();
60
61
        if ($cachedFile === null) {
62
            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
        if ($cachedFile->getHash() !== $command->getFile()->md5()) {
66
            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...
67
        }
68
69
        return $cachedFile;
70
    }
71
72
    /**
73
     * @param CreateCommand $command
74
     * @param callable $next
75
     * @param Item $item
76
     * @return mixed
77
     */
78
    private function updateCache(CreateCommand $command, callable $next, $item)
79
    {
80
        $file = $next($command);
81
        $item->lock();
82
        $this->dataStore->save($item->set($file));
83
        return $file;
84
    }
85
86
    /**
87
     * Convert path to ItemName
88
     *
89
     * @param path
90
     * @return string
91
     */
92
    private function getItemName($path)
93
    {
94
        return static::CACHE_NAMESPACE . '\\' . md5($path);
95
    }
96
}
97