Completed
Push — master ( 139fb8...7c9eb0 )
by Thomas
10s
created

AbstractFileBackendDependantOptions   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Test Coverage

Coverage 47.17%

Importance

Changes 0
Metric Value
eloc 51
dl 0
loc 160
ccs 25
cts 53
cp 0.4717
rs 10
c 0
b 0
f 0
wmc 22

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getResourcesCollection() 0 3 1
A getDefaultModuleFileResourceMasks() 0 3 1
A getDefaultCacheFileName() 0 3 1
A getCacheFilePath() 0 3 1
A __construct() 0 4 1
A getFilteredResourcesCollection() 0 15 4
A getDefault() 0 23 5
A getDefaultThemeFileResourceMasks() 0 3 1
B getDefaultResources() 0 36 7
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 5
    public function __construct(string $cacheFilePath, ResourcesCollection $resourcesCollection)
43
    {
44 5
        $this->cacheFilePath = $cacheFilePath;
45 5
        $this->resourcesCollection = $resourcesCollection;
46 5
    }
47
48
    /**
49
     * @return string
50
     */
51
    public function getCacheFilePath(): string
52
    {
53
        return $this->cacheFilePath;
54
    }
55
56
    /**
57
     * @return ResourcesCollection
58
     */
59
    public function getResourcesCollection(): ResourcesCollection
60
    {
61
        return $this->resourcesCollection;
62
    }
63
64
    /**
65
     * @param string[] $enabledModules
66
     * @param string[] $enabledThemes
67
     *
68
     * @return ResourcesCollection
69
     */
70
    public function getFilteredResourcesCollection(array $enabledModules, array $enabledThemes): ResourcesCollection
71
    {
72
        return new ResourcesCollection(\array_filter($this->resourcesCollection->all(), function (SelfCheckingResourceInterface $resource) use ($enabledModules, $enabledThemes) {
73
            if (!$resource instanceof CustomExtensionFileResource) {
74
                return true;
75
            }
76
77
            $customExtension = $resource->getCustomExtension();
78
            switch (\get_class($customExtension)) {
79
                case CustomModule::class:
80
                    return \in_array($customExtension->getMachineName(), $enabledModules);
81
                case CustomTheme::class:
82
                    return \in_array($customExtension->getMachineName(), $enabledThemes);
83
                default:
84
                    throw new NotImplementedException(\sprintf('The behavior for the "%s" custom extension class is not implemented.', \get_class($customExtension)));
85
            }
86
        }));
87
    }
88
89
    /**
90
     * @param string                $appRoot
91
     * @param DefaultsConfiguration $defaultsConfiguration
92
     *
93
     * @return AbstractFileBackendDependantOptions
94
     */
95 2
    public static function getDefault(string $appRoot, DefaultsConfiguration $defaultsConfiguration): OptionsInterface
96
    {
97 2
        $defaultResources = array();
98
99 2
        $defaultModuleFileResourceMasks = static::getDefaultModuleFileResourceMasks();
100 2
        $defaultThemeFileResourceMasks = static::getDefaultThemeFileResourceMasks();
101 2
        if (!empty($defaultModuleFileResourceMasks) || !empty($defaultThemeFileResourceMasks)) {
102 2
            $customExtensionDiscovery = new CustomExtensionDiscovery($appRoot);
103 2
            $customModules = array();
104 2
            $customThemes = array();
105
106 2
            if (!empty($defaultModuleFileResourceMasks)) {
107 2
                $customModules = $customExtensionDiscovery->getCustomModules();
108
            }
109
110 2
            if (!empty($defaultThemeFileResourceMasks)) {
111 2
                $customThemes = $customExtensionDiscovery->getCustomThemes();
112
            }
113
114 2
            $defaultResources = static::getDefaultResources($customModules, $customThemes);
115
        }
116
117 2
        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 2
    private static function getDefaultResources(array $customModules, array $customThemes): array
151
    {
152 2
        $resources = array();
153
154 2
        if (!empty($customModules)) {
155
            /** @var CustomModule $customModule */
156
            foreach ($customModules as $customModule) {
157
                $replacePairs = array(
158
                    '%machine_name%' => $customModule->getMachineName(),
159
                    '%camel_case_machine_name%' => $customModule->getCamelCaseMachineName(),
160
                );
161
162
                foreach (static::getDefaultModuleFileResourceMasks() as $mask) {
163
                    $filePath = \sprintf('%s/%s', $customModule->getRootPath(), \strtr($mask, $replacePairs));
164
165
                    $resources[] = new CustomExtensionFileResource($filePath, $customModule);
166
                }
167
            }
168
        }
169
170 2
        if (!empty($customThemes)) {
171
            /** @var CustomTheme $customTheme */
172
            foreach ($customThemes as $customTheme) {
173
                $replacePairs = array(
174
                    '%machine_name%' => $customTheme->getMachineName(),
175
                );
176
177
                foreach (static::getDefaultThemeFileResourceMasks() as $mask) {
178
                    $filePath = \sprintf('%s/%s', $customTheme->getRootPath(), \strtr($mask, $replacePairs));
179
180
                    $resources[] = new CustomExtensionFileResource($filePath, $customTheme);
181
                }
182
            }
183
        }
184
185 2
        return $resources;
186
    }
187
}
188