Completed
Push — master ( 7c9eb0...a92430 )
by Thomas
13s
created

getDefaultCacheFileName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the ekino Drupal Debug project.
7
 *
8
 * (c) ekino
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Ekino\Drupal\Debug\Action;
15
16
use Ekino\Drupal\Debug\Configuration\Model\DefaultsConfiguration;
17
use Ekino\Drupal\Debug\Exception\NotImplementedException;
18
use Ekino\Drupal\Debug\Extension\CustomExtensionDiscovery;
19
use Ekino\Drupal\Debug\Extension\Model\CustomModule;
20
use Ekino\Drupal\Debug\Extension\Model\CustomTheme;
21
use Ekino\Drupal\Debug\Option\OptionsInterface;
22
use Ekino\Drupal\Debug\Resource\Model\CustomExtensionFileResource;
23
use Ekino\Drupal\Debug\Resource\Model\ResourcesCollection;
24
use Symfony\Component\Config\Resource\SelfCheckingResourceInterface;
25
26
abstract class AbstractFileBackendDependantOptions implements OptionsInterface
27
{
28
    /**
29
     * @var string
30
     */
31
    private $cacheFilePath;
32
33
    /**
34
     * @var ResourcesCollection
35
     */
36
    private $resourcesCollection;
37
38
    /**
39
     * @param string              $cacheFilePath
40
     * @param ResourcesCollection $resourcesCollection
41
     */
42 10
    public function __construct(string $cacheFilePath, ResourcesCollection $resourcesCollection)
43
    {
44 10
        $this->cacheFilePath = $cacheFilePath;
45 10
        $this->resourcesCollection = $resourcesCollection;
46 10
    }
47
48
    /**
49
     * @return string
50
     */
51 1
    public function getCacheFilePath(): string
52
    {
53 1
        return $this->cacheFilePath;
54
    }
55
56
    /**
57
     * @return ResourcesCollection
58
     */
59 1
    public function getResourcesCollection(): ResourcesCollection
60
    {
61 1
        return $this->resourcesCollection;
62
    }
63
64
    /**
65
     * @param string[] $enabledModules
66
     * @param string[] $enabledThemes
67
     *
68
     * @return ResourcesCollection
69
     */
70 2
    public function getFilteredResourcesCollection(array $enabledModules, array $enabledThemes): ResourcesCollection
71
    {
72
        return new ResourcesCollection(\array_filter($this->resourcesCollection->all(), function (SelfCheckingResourceInterface $resource) use ($enabledModules, $enabledThemes): bool {
73 2
            if (!$resource instanceof CustomExtensionFileResource) {
74 1
                return true;
75
            }
76
77 2
            $customExtension = $resource->getCustomExtension();
78 2
            switch (\get_class($customExtension)) {
79
                case CustomModule::class:
80 1
                    return \in_array($customExtension->getMachineName(), $enabledModules);
81
                case CustomTheme::class:
82 1
                    return \in_array($customExtension->getMachineName(), $enabledThemes);
83
                default:
84 1
                    throw new NotImplementedException(\sprintf('The behavior for the "%s" custom extension file resource class is not implemented.', \get_class($customExtension)));
85
            }
86 2
        }));
87
    }
88
89
    /**
90
     * @param string                $appRoot
91
     * @param DefaultsConfiguration $defaultsConfiguration
92
     *
93
     * @return AbstractFileBackendDependantOptions
94
     */
95 3
    public static function getDefault(string $appRoot, DefaultsConfiguration $defaultsConfiguration): OptionsInterface
96
    {
97 3
        $defaultResources = array();
98
99 3
        $defaultModuleFileResourceMasks = static::getDefaultModuleFileResourceMasks();
100 3
        $defaultThemeFileResourceMasks = static::getDefaultThemeFileResourceMasks();
101 3
        if (!empty($defaultModuleFileResourceMasks) || !empty($defaultThemeFileResourceMasks)) {
102 3
            $customExtensionDiscovery = new CustomExtensionDiscovery($appRoot);
103 3
            $customModules = array();
104 3
            $customThemes = array();
105
106 3
            if (!empty($defaultModuleFileResourceMasks)) {
107 3
                $customModules = $customExtensionDiscovery->getCustomModules();
108
            }
109
110 3
            if (!empty($defaultThemeFileResourceMasks)) {
111 3
                $customThemes = $customExtensionDiscovery->getCustomThemes();
112
            }
113
114 3
            $defaultResources = static::getDefaultResources($customModules, $customThemes);
115
        }
116
117 3
        return new static(\sprintf('%s/%s', $defaultsConfiguration->getCacheDirectory(), static::getDefaultCacheFileName()), new ResourcesCollection($defaultResources));
118
    }
119
120
    /**
121
     * @return string[]
122
     */
123
    protected static function getDefaultModuleFileResourceMasks(): array
124
    {
125
        return array();
126
    }
127
128
    /**
129
     * @return string[]
130
     */
131 2
    protected static function getDefaultThemeFileResourceMasks(): array
132
    {
133 2
        return array();
134
    }
135
136
    /**
137
     * @return string
138
     */
139
    protected static function getDefaultCacheFileName(): string
140
    {
141
        return \rtrim((new \ReflectionClass(static::class))->getShortName(), 'Action');
142
    }
143
144
    /**
145
     * @param CustomModule[] $customModules
146
     * @param CustomTheme[]  $customThemes
147
     *
148
     * @return CustomExtensionFileResource[]
149
     */
150 3
    private static function getDefaultResources(array $customModules, array $customThemes): array
151
    {
152 3
        $resources = array();
153
154 3
        if (!empty($customModules)) {
155
            /** @var CustomModule $customModule */
156 1
            foreach ($customModules as $customModule) {
157
                $replacePairs = array(
158 1
                    '%machine_name%' => $customModule->getMachineName(),
159 1
                    '%camel_case_machine_name%' => $customModule->getCamelCaseMachineName(),
160
                );
161
162 1
                foreach (static::getDefaultModuleFileResourceMasks() as $mask) {
163 1
                    $filePath = \sprintf('%s/%s', $customModule->getRootPath(), \strtr($mask, $replacePairs));
164
165 1
                    $resources[] = new CustomExtensionFileResource($filePath, $customModule);
166
                }
167
            }
168
        }
169
170 3
        if (!empty($customThemes)) {
171
            /** @var CustomTheme $customTheme */
172 1
            foreach ($customThemes as $customTheme) {
173
                $replacePairs = array(
174 1
                    '%machine_name%' => $customTheme->getMachineName(),
175
                );
176
177 1
                foreach (static::getDefaultThemeFileResourceMasks() as $mask) {
178 1
                    $filePath = \sprintf('%s/%s', $customTheme->getRootPath(), \strtr($mask, $replacePairs));
179
180 1
                    $resources[] = new CustomExtensionFileResource($filePath, $customTheme);
181
                }
182
            }
183
        }
184
185 3
        return $resources;
186
    }
187
}
188