ClearCacheCommand::removeFiles()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
6
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
8
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
9
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
10
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
11
 * THE SOFTWARE.
12
 *
13
 * This software consists of voluntary contributions made by many individuals
14
 * and is licensed under the MIT license.
15
 *
16
 * Copyright (c) 2015-2020 Yuuki Takezawa
17
 *
18
 */
19
20
namespace Ytake\LaravelAspect\Console;
21
22
use Illuminate\Console\Command;
23
use Illuminate\Filesystem\Filesystem;
24
use Illuminate\Contracts\Config\Repository as ConfigRepository;
25
26
/**
27
 * Class ClearCacheCommand
28
 */
29
class ClearCacheCommand extends Command
30
{
31
    /** @var string */
32
    protected $name = 'ytake:aspect-clear-cache';
33
34
    /** @var string */
35
    protected $description = 'Flush the application aspect code cache';
36
37
    /** @var ConfigRepository */
38
    protected $config;
39
40
    /** @var Filesystem */
41
    protected $filesystem;
42
43
    /**
44
     * @param ConfigRepository $config
45
     * @param Filesystem       $filesystem
46
     */
47
    public function __construct(ConfigRepository $config, Filesystem $filesystem)
48
    {
49
        parent::__construct();
50
        $this->config = $config;
51
        $this->filesystem = $filesystem;
52
    }
53
54
    /**
55
     * @return void
56
     */
57
    public function handle(): void
58
    {
59
        $configure = $this->config->get('ytake-laravel-aop.aspect');
60
        $driverConfig = $configure['drivers'][$configure['default']];
61
        if (isset($driverConfig['cache_dir'])) {
62
            $this->removeFiles($driverConfig['cache_dir']);
63
        }
64
        if (isset($driverConfig['compile_dir'])) {
65
            $this->removeFiles($driverConfig['compile_dir']);
66
        }
67
        $this->info('aspect code cache clear!');
68
    }
69
70
    /**
71
     * @param string $dir
72
     */
73
    protected function removeFiles(string $dir): void
74
    {
75
        $files = $this->filesystem->glob($dir . '/*');
76
        foreach ($files as $file) {
77
            $this->filesystem->delete($file);
78
        }
79
    }
80
}
81