rebuildEventsSubscribersCache()   C
last analyzed

Complexity

Conditions 7
Paths 16

Size

Total Lines 24
Code Lines 16

Duplication

Lines 24
Ratio 100 %

Importance

Changes 0
Metric Value
dl 24
loc 24
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 16
nc 16
nop 2
1
<?php
2
3
namespace Taisiya\CoreBundle\Console\Command\Cache;
4
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
use Symfony\Component\Finder\Finder;
8
use Taisiya\CoreBundle\Console\Command\Command;
9
use Taisiya\CoreBundle\Console\Style\TaisiyaStyle;
10
use Taisiya\CoreBundle\Event\EventSubscriberInterface;
11
use Taisiya\CoreBundle\Exception\InvalidArgumentException;
12
use Taisiya\CoreBundle\Exception\NotReadableException;
13
use Taisiya\CoreBundle\Exception\RuntimeException;
14
use Taisiya\CoreBundle\Provider\ServiceProvider;
15
16
final class RebuildInternalCacheCommand extends Command
17
{
18
    const NAME = 'cache:rebuild-internal';
19
20
    /**
21
     * {@inheritdoc}
22
     */
23
    protected function configure()
24
    {
25
        $this->setName(self::NAME)
26
            ->setDescription('Rebuild internal application cache');
27
    }
28
29
    /**
30
     * @param InputInterface  $input
31
     * @param OutputInterface $output
32
     */
33
    protected function execute(InputInterface $input, OutputInterface $output)
34
    {
35
        $this->rebuildEventsSubscribersCache($input, $output);
36
        $this->rebuildBundlesCache($input, $output);
37
        $this->rebuildCommandsCache($input, $output);
38
    }
39
40
    /**
41
     * @param TaisiyaStyle $io
0 ignored issues
show
Bug introduced by
There is no parameter named $io. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
42
     */
43 View Code Duplication
    final protected function rebuildEventsSubscribersCache(InputInterface $input, OutputInterface $output): void
0 ignored issues
show
Unused Code introduced by
The parameter $input is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
Unnecessary FINAL modifier in FINAL class
Loading history...
44
    {
45
        $output->isVerbose() && $output->writeln('Rebuild events subscribers cache');
46
47
        $bundles = [];
48
49
        $finder = Finder::create()
50
            ->in(TAISIYA_ROOT)
51
            ->path('/(src|vendor)/')
52
            ->files()
53
            ->name('*Subscriber.php');
54
55
        foreach ($finder as $k => $file) {
56
            $bundleServiceProvider = $this->extractClassNameFromFile($file->getPathname());
57
            $reflectionClass       = new \ReflectionClass($bundleServiceProvider);
58
            if (!$reflectionClass->isAbstract() && $reflectionClass->isSubclassOf(EventSubscriberInterface::class)) {
59
                $output->isVerbose() && $output->writeln('  + '.$bundleServiceProvider);
60
                $bundles[] = $bundleServiceProvider;
61
            }
62
        }
63
64
        $this->putDataToCacheFile('events_subscribers.cache.php', $bundles);
65
        $output->isVerbose() && $output->writeln('  Subscribers saved to <info>events_subscribers.cache.php</info>');
66
    }
67
68
    /**
69
     * @param TaisiyaStyle $io
0 ignored issues
show
Bug introduced by
There is no parameter named $io. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
70
     */
71 View Code Duplication
    final protected function rebuildBundlesCache(InputInterface $input, OutputInterface $output): void
0 ignored issues
show
Unused Code introduced by
The parameter $input is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
Unnecessary FINAL modifier in FINAL class
Loading history...
72
    {
73
        $output->isVerbose() && $output->writeln('Rebuild bundles cache');
74
75
        $bundles = [];
76
77
        $finder = Finder::create()
78
            ->in(TAISIYA_ROOT)
79
            ->path('/(src|vendor)/')
80
            ->files()
81
            ->name('*ServiceProvider.php');
82
83
        foreach ($finder as $k => $file) {
84
            $bundleServiceProvider = $this->extractClassNameFromFile($file->getPathname());
85
            $reflectionClass       = new \ReflectionClass($bundleServiceProvider);
86
            if (!$reflectionClass->isAbstract() && $reflectionClass->isSubclassOf(ServiceProvider::class)) {
87
                $output->isVerbose() && $output->writeln('  + '.$bundleServiceProvider);
88
                $bundles[] = $bundleServiceProvider;
89
            }
90
        }
91
92
        $this->putDataToCacheFile('bundles.cache.php', $bundles);
93
        $output->isVerbose() && $output->writeln('  Bundles saved to <info>bundles.cache.php</info>');
94
    }
95
96
    /**
97
     * @param TaisiyaStyle $io
0 ignored issues
show
Bug introduced by
There is no parameter named $io. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
98
     */
99 View Code Duplication
    final protected function rebuildCommandsCache(InputInterface $input, OutputInterface $output): void
0 ignored issues
show
Unused Code introduced by
The parameter $input is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
Unnecessary FINAL modifier in FINAL class
Loading history...
100
    {
101
        $output->isVerbose() && $output->writeln('Rebuild commands cache');
102
103
        $commands = [];
104
105
        $finder = Finder::create()
106
            ->in(TAISIYA_ROOT)
107
            ->path('/(src|vendor)/')
108
            ->files()
109
            ->name('*Command.php');
110
111
        foreach ($finder as $k => $file) {
112
            $commandClass = $this->extractClassNameFromFile($file->getPathname());
113
            try {
114
                $reflectionClass = new \ReflectionClass($commandClass);
115
            } catch (\ReflectionException $e) {
116
                continue;
117
            }
118
            if (!$reflectionClass->isAbstract() && $reflectionClass->isSubclassOf(Command::class)) {
119
                $output->isVerbose() && $output->writeln('  + '.$commandClass);
120
                $commands[] = $commandClass;
121
            }
122
        }
123
124
        $this->putDataToCacheFile('commands.cache.php', $commands);
125
        $output->isVerbose() && $output->writeln('  Commands saved to <info>commands.cache.php</info>');
126
    }
127
128
    /**
129
     * @param string $filepath
130
     *
131
     * @throws RuntimeException
132
     *
133
     * @return string
134
     */
135
    final protected function extractClassNameFromFile(string $filepath): string
0 ignored issues
show
Coding Style introduced by
Unnecessary FINAL modifier in FINAL class
Loading history...
136
    {
137
        $contents = $this->getFileContents($filepath);
138
139
        preg_match('/namespace\s+(.+?)\;/', $contents, $namespace);
140
        if (!is_array($namespace) || !array_key_exists(1, $namespace)) {
141
            $namespace = null;
142
        } else {
143
            $namespace = $namespace[1];
144
        }
145
146
        preg_match('/class\s+(\w+)/', $contents, $class);
147
        if (!array_key_exists(1, $class)) {
148
            throw new RuntimeException('Couldn\'t extract class from file '.$filepath);
149
        }
150
        $class = $class[1];
151
152
        return trim(implode('\\', ['', $namespace, $class]), '\\');
153
    }
154
155
    /**
156
     * @param string $filepath
157
     *
158
     * @throws InvalidArgumentException
159
     * @throws NotReadableException
160
     * @throws RuntimeException
161
     *
162
     * @return string
163
     */
164
    final protected function getFileContents(string $filepath): string
0 ignored issues
show
Coding Style introduced by
Unnecessary FINAL modifier in FINAL class
Loading history...
165
    {
166
        if (!file_exists($filepath)) {
167
            throw new InvalidArgumentException('File '.$filepath.' not exists');
168
        } elseif (!is_file($filepath)) {
169
            throw new InvalidArgumentException('The '.$filepath.' is not a regular file');
170
        } elseif (!is_readable($filepath)) {
171
            throw new NotReadableException('File '.$filepath.' not readable');
172
        }
173
174
        $contents = file_get_contents($filepath);
175
176
        if ($contents === false) {
177
            throw new RuntimeException('Couldn\'t get contents from file '.$filepath);
178
        }
179
180
        return $contents;
181
    }
182
183
    /**
184
     * @param string $filename
185
     * @param array  $data
186
     *
187
     * @throws RuntimeException
188
     */
189
    final protected function putDataToCacheFile(string $filename, array $data): void
0 ignored issues
show
Coding Style introduced by
Unnecessary FINAL modifier in FINAL class
Loading history...
190
    {
191
        $cacheDir = TAISIYA_ROOT.'/var/cache/internal';
192
        if (!file_exists($cacheDir)) {
193
            if (!mkdir($cacheDir, 0777, true)) {
194
                throw new RuntimeException('Couldn\'t create directory '.$cacheDir);
195
            }
196
        }
197
        if (!file_put_contents($cacheDir.'/'.$filename, "<?php\n\nreturn ".var_export($data, true).";\n")) {
198
            throw new RuntimeException('Couldn\'t write contents to file '.$cacheDir.'/'.$filename);
199
        }
200
    }
201
}
202