Passed
Push — master ( 1c8d05...42c076 )
by Enjoys
01:48
created

StrategyAbstract   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 86.96%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 89
ccs 20
cts 23
cp 0.8696
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getNamespace() 0 3 1
A createDirectory() 0 14 3
A __construct() 0 11 1
A writeFile() 0 6 2
1
<?php
2
3
4
namespace Enjoys\AssetsCollector\CollectStrategy;
5
6
7
use Enjoys\AssetsCollector\Asset;
8
use Enjoys\AssetsCollector\Assets;
9
use Enjoys\AssetsCollector\Environment;
10
use Psr\Log\LoggerInterface;
11
12
abstract class StrategyAbstract implements StrategyInterface
13
{
14
    /**
15
     * @var array<Asset>
16
     */
17
    protected array $assetsCollection;
18
19
    /**
20
     * @var string css|js
21
     */
22
    protected string $type;
23
24
    /**
25
     * @var Environment
26
     */
27
    protected Environment $environment;
28
29
    protected LoggerInterface $logger;
30
31
    protected string $namespace;
32
33
    /**
34
     * StrategyAbstract constructor.
35
     * @param Environment $environment
36
     * @param array<Asset> $assetsCollection
37
     * @param string $type
38
     * @param string $namespace
39
     */
40 7
    public function __construct(
41
        Environment $environment,
42
        array $assetsCollection,
43
        string $type,
44
        string $namespace = Assets::NAMESPACE_COMMON
45
    ) {
46 7
        $this->environment = $environment;
47 7
        $this->assetsCollection = $assetsCollection;
48 7
        $this->type = $type;
49 7
        $this->logger = $environment->getLogger();
50 7
        $this->namespace = $namespace;
51 7
    }
52
53
    /**
54
     * @return array<string>
55
     */
56
    abstract public function getResult(): array;
57
58
    /**
59
     * @return string
60
     */
61 2
    public function getNamespace(): string
62
    {
63 2
        return $this->namespace;
64
    }
65
66
    /**
67
     * @param string $file
68
     * @param string $data
69
     * @param string $mode
70
     * @return void
71
     */
72 2
    protected function writeFile(string $file, string $data, string $mode = 'w'): void
73
    {
74 2
        $f = fopen($file, $mode);
75 2
        if ($f) {
0 ignored issues
show
introduced by
$f is of type resource, thus it always evaluated to false.
Loading history...
76 2
            fwrite($f, $data);
77 2
            fclose($f);
78
        }
79 2
    }
80
81
    /**
82
     * @param string $path
83
     * @param int $permissions
84
     * @return void
85
     * @throws \Exception
86
     */
87 4
    protected function createDirectory(string $path, int $permissions = 0777): void
88
    {
89
        //Clear the most recent error
90 4
        error_clear_last();
91
92 4
        if (!is_dir($path)) {
93 4
            if (@mkdir($path, $permissions, true) === false) {
94
                /** @var string[] $error */
95
                $error = error_get_last();
96
                throw new \Exception(
97
                    sprintf("Не удалось создать директорию: %s! Причина: %s", $path, $error['message'])
98
                );
99
            }
100 4
            $this->logger->info(sprintf('Create directory %s', $path));
101
        }
102
    }
103
}