Completed
Push — master ( 176090...b969a1 )
by Peter
02:10
created

ClassCache::getInstanceId()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.1755

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 7
cts 9
cp 0.7778
rs 9.6
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4.1755
1
<?php
2
3
4
namespace Maslosoft\Addendum\Cache;
5
6
7
use function dirname;
8
use function get_class;
9
use function is_object;
10
use function is_writable;
11
use Maslosoft\Addendum\Addendum;
12
use Maslosoft\Addendum\Cache\PhpCache\Checker;
13
use Maslosoft\Addendum\Cache\PhpCache\Cleaner;
14
use Maslosoft\Addendum\Cache\PhpCache\Reader;
15
use Maslosoft\Addendum\Cache\PhpCache\Writer;
16
use Maslosoft\Addendum\Helpers\Cacher;
17
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
18
use Maslosoft\Addendum\Options\MetaOptions;
19
use Maslosoft\Cli\Shared\ConfigDetector;
20
use UnexpectedValueException;
21
use function var_dump;
22
23
class ClassCache
24
{
25
	/**
26
	 * @var string
27
	 */
28
	private static $runtimePath = null;
29
30
	/**
31
	 * @var string
32
	 */
33
	private $className = '';
34
35
	/**
36
	 * Root path of storage
37
	 * @var string
38
	 */
39
	private $storagePath = '';
40
41
	/**
42
	 * Base path for current meta class
43
	 * @var string
44
	 */
45
	private $basePath = '';
46
47
	/**
48
	 * @var string
49
	 */
50
	private $metaClass = '';
51
52
	/**
53
	 * @var NsCache
54
	 */
55
	private $nsCache;
56
57
	/**
58
	 * @var Addendum
59
	 */
60
	private $addendum;
61
62
	/**
63
	 * @var Writer
64
	 */
65
	private $writer;
66
67
	/**
68
	 * @var Checker
69
	 */
70
	private $checker;
71
72
	/**
73
	 * @var Reader
74
	 */
75
	private $reader;
76
77
	/**
78
	 * @var Cleaner
79
	 */
80
	private $cleaner;
81
82
	/**
83
	 *
84
	 * @param string                           $metaClass
85
	 * @param AnnotatedInterface|object|string $component
86
	 * @param MetaOptions|Addendum             $options
87
	 */
88 1
	public function __construct(string $metaClass, $component, $options = null)
89
	{
90 1
		if (null === self::$runtimePath)
91
		{
92 1
			self::$runtimePath = (new ConfigDetector)->getRuntimePath();
93
		}
94 1
		$this->storagePath = self::$runtimePath . '/addendum';
95 1
		$this->metaClass = $metaClass;
96 1
		if(is_object($component))
97
		{
98 1
			$component = get_class($component);
99
		}
100 1
		$this->className = $component;
101 1
		$this->setOptions($options);
0 ignored issues
show
Bug introduced by
It seems like $options defined by parameter $options on line 88 can also be of type object<Maslosoft\Addendum\Addendum>; however, Maslosoft\Addendum\Cache\ClassCache::setOptions() does only seem to accept null|object<Maslosoft\Ad...um\Options\MetaOptions>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
102
103 1
		$this->reader = new Reader($this->basePath);
104 1
		$this->writer = new Writer($this->basePath);
105 1
		$this->cleaner = new Cleaner($this->basePath);
106 1
		$this->checker = new Checker($this->basePath);
107 1
	}
108
109 38
	public function get($className)
110
	{
111 38
		assert(!empty($className));
112
//		var_dump('TRY:' . $className);
113 38
		if($this->addendum->checkMTime)
114
		{
115
			if(!$this->checker->isValid($className))
116
			{
117
				var_dump('INV:' . $className);
0 ignored issues
show
Security Debugging Code introduced by
\var_dump('INV:' . $className); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
118
				return false;
119
			}
120
		}
121
//		var_dump('HIT:' . $className);
122 38
		$data = $this->reader->read($className);
123
124 38
		return $data;
125
	}
126
127 31
	public function set($className, $data): bool
128
	{
129
//		var_dump('SET:' . $className);
130 31
		$this->cleaner->clean($className);
131 31
		return $this->writer->write($className, $data);
132
	}
133
134 38
	public function setOptions(MetaOptions $options = null)
135
	{
136 38
		$instanceId = $this->getInstanceId($options);
137 38
		$this->addendum = Addendum::fly($instanceId);
138 38
		$this->basePath = sprintf('%s/%s@%s',
139 38
			$this->storagePath,
140 38
			Cacher::classToFile($this->metaClass),
141 38
			$instanceId
142
		);
143 38
		$this->nsCache = new NsCache($this->basePath, $this->addendum);
144 38
		$this->nsCache->setOptions($options);
145 38
	}
146
147 38
	private function getInstanceId(MetaOptions $options = null)
148
	{
149 38
		if (empty($options))
150
		{
151 33
			$instanceId = Addendum::DefaultInstanceId;
152
		}
153 5
		elseif ($options instanceof Addendum)
154
		{
155
			$instanceId = $options->getInstanceId();
156
		}
157 5
		elseif ($options instanceof MetaOptions)
158
		{
159 5
			$instanceId = $options->instanceId;
160
		}
161
		else
162
		{
163
			throw new UnexpectedValueException('Unknown options');
164
		}
165 38
		return $instanceId;
166
	}
167
}