Completed
Pull Request — master (#16)
by Yuichi
09:45 queued 07:37
created

CacheClient::cacheClear()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace CybozuHttp;
4
5
use Doctrine\Common\Cache\FilesystemCache;
6
use CybozuHttp\Subscriber\Cache\CacheStorage;
7
use CybozuHttp\Subscriber\CacheSubscriber;
8
use GuzzleHttp\Message\RequestInterface;
9
10
/**
11
 * @author ochi51 <[email protected]>
12
 */
13
class CacheClient extends Client
14
{
15
    /**
16
     * @var CacheStorage
17
     */
18
    private $storage;
19
20
    /**
21
     * CacheClient constructor.
22
     * @param array $config
23
     */
24
    public function __construct($config = [])
25
    {
26
        $config['use_cache'] = true;
27
        parent::__construct($config);
28
29
        $dir = $this->getConfig()->get('cache_dir');
30
        $ttl = (int)$this->getConfig()->get('cache_ttl');
31
        $this->attachCacheSubscriber($dir, $ttl);
0 ignored issues
show
Bug introduced by
It seems like $dir defined by $this->getConfig()->get('cache_dir') on line 29 can also be of type boolean; however, CybozuHttp\CacheClient::attachCacheSubscriber() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
32
    }
33
34
    /**
35
     * @param string $dir
36
     * @param int    $ttl
37
     */
38
    private function attachCacheSubscriber($dir, $ttl = 0)
39
    {
40
        $storage = new CacheStorage(new FilesystemCache($dir), $ttl);
41
        CacheSubscriber::attach($this, $storage);
42
        $this->storage = $storage;
43
    }
44
45
    /**
46
     * Clear all cache
47
     */
48
    public function cacheClear()
49
    {
50
        $dir = $this->getConfig()->get('cache_dir');
51
        array_map('unlink', glob($dir . "/*/*"));
52
        foreach (glob($dir . '/*') as $d) {
53
            rmdir($d);
54
        }
55
    }
56
57
    /**
58
     * Delete cache by request
59
     * @param RequestInterface $request
60
     */
61
    public function deleteCache(RequestInterface $request)
62
    {
63
        $this->storage->delete($request);
64
    }
65
}