Completed
Push — master ( 76c18a...59b186 )
by Thomas
24s
created

LoadNewModuleFile   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 52
ccs 0
cts 32
cp 0
rs 10
c 0
b 0
f 0
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B __invoke() 0 34 8
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\WatchModulesHooksImplementations;
15
16
use Drupal\Core\DrupalKernelInterface;
17
use Drupal\Core\Extension\ModuleHandler;
18
use Ekino\Drupal\Debug\Cache\Event\CacheNotFreshEvent;
19
use Ekino\Drupal\Debug\Extension\Model\CustomModule;
20
use Ekino\Drupal\Debug\Resource\Model\CustomExtensionFileResource;
21
use Symfony\Component\HttpKernel\HttpKernelInterface;
22
23
class LoadNewModuleFile
24
{
25
    /**
26
     * @var ModuleHandler
27
     */
28
    private $moduleHandler;
29
30
    /**
31
     * @var HttpKernelInterface
32
     */
33
    private $kernel;
34
35
    public function __construct(ModuleHandler $moduleHandler, HttpKernelInterface $kernel)
36
    {
37
        $this->moduleHandler = $moduleHandler;
38
        $this->kernel = $kernel;
39
    }
40
41
    public function __invoke(CacheNotFreshEvent $event): void
42
    {
43
        foreach ($event->getFileCache()->getCurrentResourcesCollection()->all() as $resource) {
44
            if (!$resource instanceof CustomExtensionFileResource) {
45
                continue;
46
            }
47
48
            if (!$resource->isNew()) {
49
                continue;
50
            }
51
52
            $customExtension = $resource->getCustomExtension();
53
            if (!$customExtension instanceof CustomModule) {
54
                continue;
55
            }
56
57
            $machineName = $customExtension->getMachineName();
58
            $extensionFilename = \sprintf('%s.module', $machineName);
59
            if ($extensionFilename !== \substr($resource->getFilePath(), -\strlen($extensionFilename))) {
60
                continue;
61
            }
62
63
            $module = $this->moduleHandler->getModule($machineName);
64
            if (\is_string($module->getExtensionFilename())) {
65
                continue;
66
            }
67
68
            $name = $module->getName();
69
70
            $this->moduleHandler->addModule($name, $module->getPath());
71
            $this->moduleHandler->getModule($name)->load();
72
73
            if ($this->kernel instanceof DrupalKernelInterface) {
74
                $this->kernel->invalidateContainer();
75
            }
76
        }
77
    }
78
}
79