Completed
Pull Request — master (#333)
by Nikola
02:33
created

CacheWarmer::warmUp()   B

Complexity

Conditions 7
Paths 13

Size

Total Lines 55
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 55
ccs 0
cts 42
cp 0
rs 7.8235
cc 7
eloc 34
nc 13
nop 0
crap 56

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * Go! AOP framework
4
 *
5
 * @copyright Copyright 2013, Lisachenko Alexander <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Go\Instrument\ClassLoading;
12
13
use Go\Core\AspectKernel;
14
use Go\Instrument\FileSystem\Enumerator;
15
use Go\Instrument\Transformer\FilterInjectorTransformer;
16
use Symfony\Component\Console\Output\NullOutput;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
/**
20
 * Warms up the cache
21
 */
22
class CacheWarmer
23
{
24
    /**
25
     * @var AspectKernel
26
     */
27
    protected $aspectKernel;
28
29
    /**
30
     * @var OutputInterface
31
     */
32
    protected $output;
33
34
    /**
35
     * CacheWarmer constructor.
36
     *
37
     * @param AspectKernel $aspectKernel A kernel
38
     * @param OutputInterface $output Optional output to log current status.
39
     */
40
    public function __construct(AspectKernel $aspectKernel, OutputInterface $output = null)
41
    {
42
        $this->aspectKernel = $aspectKernel;
43
        $this->output       = $output !== null ? $output : new NullOutput();
44
    }
45
46
    /**
47
     * Warms up cache
48
     */
49
    public function warmUp()
50
    {
51
        $options = $this->aspectKernel->getOptions();
52
53
        if (empty($options['cacheDir'])) {
54
            throw new \InvalidArgumentException('Cache warmer require the `cacheDir` options to be configured');
55
        }
56
57
        $enumerator = new Enumerator($options['appDir'], $options['includePaths'], $options['excludePaths']);
58
        $iterator   = $enumerator->enumerate();
59
        $total      = iterator_count($iterator);
60
61
        $this->output->writeln(sprintf('Total <info>%s</info> files to process.', $total));
62
        $this->output->writeln('');
63
        $iterator->rewind();
64
65
        set_error_handler(function ($errno, $errstr, $errfile, $errline) {
66
            throw new \ErrorException($errstr, $errno, 0, $errfile, $errline);
67
        });
68
69
        $errors       = [];
70
        $displayException = \Closure::bind(function ($exception, $path) use (&$errors) {
71
            $this->output->writeln(sprintf('<fg=white;bg=red;options=bold>[ERR]</>: %s', $path));
72
            $errors[$path] = $exception->getMessage();
73
        }, $this);
74
75
        foreach ($iterator as $file) {
76
            $path = $file->getRealPath();
77
78
            try {
79
                // This will trigger creation of cache
80
                file_get_contents(FilterInjectorTransformer::PHP_FILTER_READ .
81
                    SourceTransformingLoader::FILTER_IDENTIFIER .
82
                    '/resource=' . $path
83
                );
84
85
                $this->output->writeln(sprintf('<fg=green;options=bold>[OK]</>: <comment>%s</comment>', $path));
86
            } catch (\Throwable $e) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
87
                $displayException($e, $path);
88
            } catch (\Exception $e) {
89
                $displayException($e, $path);
90
            }
91
        }
92
93
        restore_error_handler();
94
95
        if ($this->output->isVerbose()) {
96
            foreach ($errors as $path => $error) {
97
                $this->output->writeln(sprintf('<fg=white;bg=red;options=bold>[ERR]</>: File "%s" is not processed correctly due to exception: "%s".', $path, $error));
98
            }
99
        }
100
101
        $this->output->writeln('');
102
        $this->output->writeln(sprintf('<fg=green;>[DONE]</>: Total processed %s, %s errors.', $total, count($errors)));
103
    }
104
}
105