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

Writer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 6
dl 0
loc 57
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B write() 0 53 6
1
<?php
2
3
4
namespace Maslosoft\Addendum\Cache\PhpCache;
5
6
7
use function array_filter;
8
use function array_sum;
9
use function chmod;
10
use function file_exists;
11
use function file_put_contents;
12
use Maslosoft\Addendum\Helpers\Cacher;
13
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
14
use Maslosoft\Addendum\Utilities\ClassChecker;
15
use Maslosoft\Addendum\Utilities\NameNormalizer;
16
use Maslosoft\Cli\Shared\Helpers\PhpExporter;
17
use Maslosoft\Cli\Shared\Io;
18
use function sprintf;
19
20
class Writer extends CacheComponent
21
{
22
23 35
	public function write($className, $data): bool
24
	{
25 35
		NameNormalizer::normalize($className, false);
26
27
28
		// Set partials cache
29
		$filter = function ($partial) use ($className) {
30 34
			$interface = AnnotatedInterface::class;
31 34
			NameNormalizer::normalize($interface, false);
32 34
			NameNormalizer::normalize($partial, false);
33 34
			if ($partial === $interface)
34
			{
35 34
				return false;
36
			}
37 34
			if ($partial === $className)
38
			{
39 34
				return false;
40
			}
41 12
			return true;
42 35
		};
43 35
		$partials = array_filter(ClassChecker::getPartials($className), $filter);
44 35
		$success = [];
45 35
		if (!empty($partials))
46
		{
47
			// Create directory for current class
48
			// This directory will hold class names
49
			// of partials
50 12
			$partialsDir = $this->getPartialsDir($className);
51 12
			if (!file_exists($partialsDir))
52
			{
53 12
				Io::mkdir($partialsDir);
54
			}
55
56 12
			foreach ($partials as $partialClass)
57
			{
58 12
				$partialFile = Cacher::classToFile($partialClass);
59
60
				// Write current class name to partial dir
61
				// so that it can be retrieved on modification
62
				// time check
63 12
				$classMarkerFile = sprintf(
64 12
					'%s/%s.php',
65 12
					$partialsDir,
66 12
					$partialFile
67
				);
68 12
				$success[] = (bool)file_put_contents($classMarkerFile, PhpExporter::export(true));
69
			}
70
		}
71 35
		$fileName = $this->getFilename($className);
72 35
		$success[] = (bool)file_put_contents($fileName, PhpExporter::export($data));
73 33
		@chmod($fileName, 0666);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
74 33
		return array_sum($success) === count($success);
75
	}
76
}