Passed
Push — master ( 91f02d...65071d )
by Melech
21:33 queued 07:21
created

ClearCacheCommand   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 13
dl 0
loc 22
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 0 20 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
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 Valkyrja\Application\Cli\Command;
15
16
use Valkyrja\Application\Env;
0 ignored issues
show
Bug introduced by
The type Valkyrja\Application\Env was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Valkyrja\Cli\Interaction\Factory\Contract\OutputFactory;
18
use Valkyrja\Cli\Interaction\Message\Banner;
19
use Valkyrja\Cli\Interaction\Message\Message;
20
use Valkyrja\Cli\Interaction\Message\NewLine;
21
use Valkyrja\Cli\Interaction\Message\SuccessMessage;
22
use Valkyrja\Cli\Interaction\Output\Contract\Output;
23
use Valkyrja\Cli\Routing\Attribute\Command as CommandAttribute;
24
25
/**
26
 * Class ClearCacheCommand.
27
 *
28
 * @author Melech Mizrachi
29
 */
30
class ClearCacheCommand
31
{
32
    #[CommandAttribute(
33
        name: 'config:clear-cache',
34
        description: 'Clear config cache',
35
        helpText: new Message('A command to clear the config cache.'),
36
    )]
37
    public function run(Env $env, OutputFactory $outputFactory): Output
38
    {
39
        /** @var non-empty-string $cacheFilePath */
40
        $cacheFilePath = $env::APP_CACHE_FILE_PATH;
41
42
        // If the cache file already exists, delete it
43
        if (is_file($cacheFilePath)) {
44
            @unlink($cacheFilePath);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

44
            /** @scrutinizer ignore-unhandled */ @unlink($cacheFilePath);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
45
        }
46
47
        return $outputFactory
48
            ->createOutput()
49
            ->withMessages(
50
                new Banner(new SuccessMessage('Application config cache cleared successfully.')),
51
                new NewLine(),
52
            );
53
    }
54
}
55