Completed
Push — master ( be1cb1...d3d836 )
by Tomáš
05:39
created

src/Cache/DefaultCache.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
declare(strict_types=1);
3
4
namespace Portiny\Doctrine\Cache;
5
6
use Doctrine\Common\Cache\CacheProvider;
7
use Doctrine\ORM\Mapping\ClassMetadata;
8
use Nette\Caching\Cache;
9
use Nette\Caching\IStorage;
10
use Nette\Utils\Strings;
11
use ReflectionClass;
12
13
final class DefaultCache extends CacheProvider
14
{
15
	/**
16
	 * @var string
17
	 */
18
	public const CACHE_NS = 'Doctrine';
19
20
	/**
21
	 * @var bool
22
	 */
23
	private $debugMode = FALSE;
24
25
	/**
26
	 * @var Cache
27
	 */
28
	private $cache;
29
30 6
	public function __construct(IStorage $storage, string $namespace = self::CACHE_NS, bool $debugMode = FALSE)
31
	{
32 6
		$this->cache = new Cache($storage, $namespace);
33 6
		$this->debugMode = $debugMode;
34 6
	}
35
36
	/**
37
	 * {@inheritdoc}
38
	 */
39
	protected function doFetch($id)
40
	{
41
		$cached = $this->cache->load($id);
42
		return $cached !== NULL ? $cached : FALSE;
43
	}
44
45
	/**
46
	 * {@inheritdoc}
47
	 */
48
	protected function doContains($id)
49
	{
50
		return $this->cache->load($id) !== NULL;
51
	}
52
53
	/**
54
	 * @param string $id
55
	 * @param string|object $data
56
	 * @param int $lifeTime
57
	 * @return bool
58
	 */
59
	protected function doSave($id, $data, $lifeTime = 0)
60
	{
61
		if ($this->debugMode !== TRUE) {
62
			return $this->doSaveDependingOnFiles($id, $data, [], $lifeTime);
63
		}
64
65
		$files = [];
66
		if ($data instanceof ClassMetadata) {
0 ignored issues
show
The class Doctrine\ORM\Mapping\ClassMetadata does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
67
			$files[] = self::getClassFilename($data->name);
68
			foreach ($data->parentClasses as $class) {
69
				$files[] = self::getClassFilename($class);
70
			}
71
		}
72
73
		if (! empty($data)) {
74
			$m = Strings::match($id, '~(?P<class>[^@$[\].]+)(?:\$(?P<prop>[^@$[\].]+))?\@\[Annot\]~i');
75
			if ($m !== NULL && class_exists($m['class'])) {
76
				$files[] = self::getClassFilename($m['class']);
77
			}
78
		}
79
80
		return $this->doSaveDependingOnFiles($id, $data, $files, $lifeTime);
81
	}
82
83
	/**
84
	 * @param string $id
85
	 * @param mixed $data
86
	 * @param int $lifeTime
87
	 * @return bool
88
	 */
89
	protected function doSaveDependingOnFiles($id, $data, array $files, $lifeTime = 0)
90
	{
91
		$dependencies = [
92
			Cache::TAGS => ['doctrine'],
93
			Cache::FILES => $files,
94
		];
95
		if ($lifeTime > 0) {
96
			$dependencies[Cache::EXPIRE] = time() + $lifeTime;
97
		}
98
99
		$this->cache->save($id, $data, $dependencies);
100
101
		return TRUE;
102
	}
103
104
	/**
105
	 * {@inheritdoc}
106
	 */
107
	protected function doDelete($id)
108
	{
109
		$this->cache->save($id, NULL);
110
111
		return TRUE;
112
	}
113
114
	/**
115
	 * {@inheritdoc}
116
	 */
117
	protected function doFlush()
118
	{
119
		$this->cache->clean([
120
			Cache::TAGS => ['doctrine'],
121
		]);
122
123
		return TRUE;
124
	}
125
126
	/**
127
	 * {@inheritdoc}
128
	 */
129
	protected function doGetStats()
130
	{
131
		return [
132
			self::STATS_HITS => NULL,
133
			self::STATS_MISSES => NULL,
134
			self::STATS_UPTIME => NULL,
135
			self::STATS_MEMORY_USAGE => NULL,
136
			self::STATS_MEMORY_AVAILABLE => NULL,
137
		];
138
	}
139
140
	private static function getClassFilename(string $className): string
141
	{
142
		$reflection = new ReflectionClass($className);
143
		return (string) $reflection->getFileName();
144
	}
145
}
146