Completed
Push — master ( 52b242...112fdc )
by Thomas
17s queued 11s
created

getThemeResources()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 12
c 0
b 0
f 0
nc 7
nop 3
dl 0
loc 26
ccs 0
cts 12
cp 0
crap 56
rs 8.8333
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\CacheDirectoryPathConfigurationTrait;
17
use Ekino\Drupal\Debug\Configuration\Model\ActionConfiguration;
18
use Ekino\Drupal\Debug\Configuration\Model\DefaultsConfiguration;
19
use Ekino\Drupal\Debug\Exception\NotImplementedException;
20
use Ekino\Drupal\Debug\Extension\CustomExtensionDiscovery;
21
use Ekino\Drupal\Debug\Extension\Model\CustomModule;
22
use Ekino\Drupal\Debug\Extension\Model\CustomTheme;
23
use Ekino\Drupal\Debug\Option\OptionsInterface;
24
use Ekino\Drupal\Debug\Resource\Model\CustomExtensionFileResource;
25
use Ekino\Drupal\Debug\Resource\Model\ResourcesCollection;
26
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
27
use Symfony\Component\Config\Resource\SelfCheckingResourceInterface;
28
29
abstract class AbstractFileBackendDependantOptions implements OptionsInterface
30
{
31
    use CacheDirectoryPathConfigurationTrait;
32
33
    /**
34
     * @var string
35
     */
36
    private $cacheFilePath;
37
38
    /**
39
     * @var ResourcesCollection
40
     */
41
    private $resourcesCollection;
42
43
    /**
44
     * @var bool|null
45
     */
46
    private static $canHaveBothExtensionTypeFileResourceMasks;
47
48
    /**
49
     * @param string              $cacheFilePath
50
     * @param ResourcesCollection $resourcesCollection
51
     */
52 9
    public function __construct(string $cacheFilePath, ResourcesCollection $resourcesCollection)
53
    {
54 9
        $this->cacheFilePath = $cacheFilePath;
55 9
        $this->resourcesCollection = $resourcesCollection;
56 9
    }
57
58
    /**
59
     * @return string
60
     */
61 1
    public function getCacheFilePath(): string
62
    {
63 1
        return $this->cacheFilePath;
64
    }
65
66
    /**
67
     * @return ResourcesCollection
68
     */
69 1
    public function getResourcesCollection(): ResourcesCollection
70
    {
71 1
        return $this->resourcesCollection;
72
    }
73
74
    /**
75
     * @param string[] $enabledModules
76
     * @param string[] $enabledThemes
77
     *
78
     * @return ResourcesCollection
79
     */
80 2
    public function getFilteredResourcesCollection(array $enabledModules, array $enabledThemes): ResourcesCollection
81
    {
82
        return new ResourcesCollection(\array_filter($this->resourcesCollection->all(), static function (SelfCheckingResourceInterface $resource) use ($enabledModules, $enabledThemes): bool {
83 2
            if (!$resource instanceof CustomExtensionFileResource) {
84 1
                return true;
85
            }
86
87 2
            $customExtension = $resource->getCustomExtension();
88 2
            switch (\get_class($customExtension)) {
89
                case CustomModule::class:
90 1
                    return \in_array($customExtension->getMachineName(), $enabledModules);
91
                case CustomTheme::class:
92 1
                    return \in_array($customExtension->getMachineName(), $enabledThemes);
93
                default:
94 1
                    throw new NotImplementedException(\sprintf('The behavior for the "%s" custom extension file resource class is not implemented.', \get_class($customExtension)));
95
            }
96 2
        }));
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 19
    public static function addConfiguration(NodeBuilder $nodeBuilder, DefaultsConfiguration $defaultsConfiguration): void
103
    {
104 19
        $childrenNodeBuilders = array($nodeBuilder);
105
106 19
        if ($canHaveBothExtensionTypeFileResourceMasks = self::canHaveBothExtensionTypeFileResourceMasks()) {
107
            $childrenNodeBuilders = array();
108
            foreach (array('module', 'theme') as $extensionType) {
109
                $childrenNodeBuilders[] = $nodeBuilder
110
                    ->arrayNode($extensionType)
111
                        ->children();
112
            }
113
        }
114
115
        /** @var NodeBuilder $childrenNodeBuilder */
116 19
        foreach ($childrenNodeBuilders as $childrenNodeBuilder) {
117
            $childrenNodeBuilder
118 19
                ->booleanNode('include_defaults')
119 19
                    ->defaultTrue()
120 19
                ->end()
121 19
                ->arrayNode('custom_file_resource_masks')
122 19
                    ->scalarPrototype()
123 19
                ->end();
124
125 19
            if ($canHaveBothExtensionTypeFileResourceMasks) {
126
                $childrenNodeBuilder->end();
127
            }
128
        }
129
130 19
        self::addCacheDirectoryPathConfigurationNodeFromDefaultsConfiguration($nodeBuilder, $defaultsConfiguration);
131 19
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136 2
    public static function getOptions(string $appRoot, ActionConfiguration $actionConfiguration): OptionsInterface
137
    {
138 2
        $processedConfiguration = $actionConfiguration->getProcessedConfiguration();
139
140 2
        $resources = array();
141
142 2
        $customExtensionDiscovery = new CustomExtensionDiscovery($appRoot);
143
144 2
        if (static::canHaveModuleFileResourceMasks()) {
145 2
            $includeDefaults = ($canHaveBothExtensionTypeFileResourceMasks = self::canHaveBothExtensionTypeFileResourceMasks()) ? $processedConfiguration['module']['include_defaults'] : $processedConfiguration['include_defaults'];
146 2
            $customFileResourceMasks = $canHaveBothExtensionTypeFileResourceMasks ? $processedConfiguration['module']['custom_file_resource_masks'] : $processedConfiguration['custom_file_resource_masks'];
147
148 2
            $resources = self::getModuleResources($customExtensionDiscovery->getCustomModules(), $includeDefaults, $customFileResourceMasks);
149
        }
150
151 2
        if (static::canHaveThemeFileResourceMasks()) {
152
            $includeDefaults = ($canHaveBothExtensionTypeFileResourceMasks ?? ($canHaveBothExtensionTypeFileResourceMasks = self::canHaveBothExtensionTypeFileResourceMasks()) ? $processedConfiguration['theme']['include_defaults'] : $processedConfiguration['include_defaults']);
153
            $customFileResourceMasks = $canHaveBothExtensionTypeFileResourceMasks ? $processedConfiguration['theme']['custom_file_resource_masks'] : $processedConfiguration['custom_file_resource_masks'];
154
155
            $resources = \array_merge($resources, self::getThemeResources($customExtensionDiscovery->getCustomThemes(), $includeDefaults, $customFileResourceMasks));
156
        }
157
158 2
        return new static(
159 2
            \sprintf('%s/%s', self::getConfiguredCacheDirectoryPath($actionConfiguration), static::getCacheFileName()),
160 2
            new ResourcesCollection($resources)
161
        );
162
    }
163
164
    protected static function canHaveModuleFileResourceMasks(): bool
165
    {
166
        return false;
167
    }
168
169
    /**
170
     * @return string[]
171
     */
172
    protected static function getDefaultModuleFileResourceMasks(): array
173
    {
174
        return array();
175
    }
176
177 19
    protected static function canHaveThemeFileResourceMasks(): bool
178
    {
179 19
        return false;
180
    }
181
182
    /**
183
     * @return string[]
184
     */
185
    protected static function getDefaultThemeFileResourceMasks(): array
186
    {
187
        return array();
188
    }
189
190
    protected static function getCacheFileName(): string
191
    {
192
        return \rtrim((new \ReflectionClass(static::class))->getShortName(), 'Action');
193
    }
194
195
    /**
196
     * @param CustomModule[] $customModules
197
     * @param bool           $includeDefaults
198
     * @param string[]       $fileResourceMasks
199
     *
200
     * @return CustomExtensionFileResource[]
201
     */
202 2
    private static function getModuleResources(array $customModules, bool $includeDefaults, array $fileResourceMasks): array
203
    {
204 2
        if (empty($customModules) || (!$includeDefaults && empty($fileResourceMasks))) {
205 2
            return array();
206
        }
207
208
        $resources = array();
209
210
        if ($includeDefaults) {
211
            $fileResourceMasks = \array_merge($fileResourceMasks, static::getDefaultModuleFileResourceMasks());
212
        }
213
214
        /** @var CustomModule $customModule */
215
        foreach ($customModules as $customModule) {
216
            $replacePairs = array(
217
                '%machine_name%' => $customModule->getMachineName(),
218
                '%camel_case_machine_name%' => $customModule->getCamelCaseMachineName(),
219
            );
220
221
            foreach ($fileResourceMasks as $fileResourceMask) {
222
                $filePath = \sprintf('%s/%s', $customModule->getRootPath(), \strtr($fileResourceMask, $replacePairs));
223
224
                $resources[] = new CustomExtensionFileResource($filePath, $customModule);
225
            }
226
        }
227
228
        return $resources;
229
    }
230
231
    /**
232
     * @param CustomTheme[] $customThemes
233
     * @param bool          $includeDefaults
234
     * @param string[]      $fileResourceMasks
235
     *
236
     * @return CustomExtensionFileResource[]
237
     */
238
    private static function getThemeResources(array $customThemes, bool $includeDefaults, array $fileResourceMasks): array
239
    {
240
        if (empty($customThemes) || (!$includeDefaults && empty($fileResourceMasks))) {
241
            return array();
242
        }
243
244
        $resources = array();
245
246
        if ($includeDefaults) {
247
            $fileResourceMasks = \array_merge($fileResourceMasks, static::getDefaultThemeFileResourceMasks());
248
        }
249
250
        /** @var CustomTheme $customTheme */
251
        foreach ($customThemes as $customTheme) {
252
            $replacePairs = array(
253
                '%machine_name%' => $customTheme->getMachineName(),
254
            );
255
256
            foreach ($fileResourceMasks as $fileResourceMask) {
257
                $filePath = \sprintf('%s/%s', $customTheme->getRootPath(), \strtr($fileResourceMask, $replacePairs));
258
259
                $resources[] = new CustomExtensionFileResource($filePath, $customTheme);
260
            }
261
        }
262
263
        return $resources;
264
    }
265
266 19
    private static function canHaveBothExtensionTypeFileResourceMasks(): bool
267
    {
268 19
        if (!\is_bool(self::$canHaveBothExtensionTypeFileResourceMasks)) {
269 19
            self::$canHaveBothExtensionTypeFileResourceMasks = static::canHaveModuleFileResourceMasks() && static::canHaveThemeFileResourceMasks();
270
        }
271
272 19
        return self::$canHaveBothExtensionTypeFileResourceMasks;
273
    }
274
}
275