Passed
Push — master ( cd1392...a15adc )
by SignpostMarv
07:33
created

FastRouteCacheCommand::execute()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0416

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 2
dl 0
loc 26
ccs 10
cts 12
cp 0.8333
crap 3.0416
rs 9.504
c 0
b 0
f 0
1
<?php
2
/**
3
* @author SignpostMarv
4
*/
5
declare(strict_types=1);
6
7
namespace SignpostMarv\DaftFramework\Symfony\Console\Command;
8
9
use SignpostMarv\DaftRouter\Router\Compiler;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
class FastRouteCacheCommand extends Command
15
{
16
    /**
17
    * @var string
18
    */
19
    protected static $defaultName = 'daft-framework:router:update-cache';
20
21 2
    public function execute(InputInterface $input, OutputInterface $output) : int
22
    {
23 2
        $cacheFilename = tempnam(sys_get_temp_dir(), static::class);
24
25 2
        if ( ! is_string($cacheFilename)) {
0 ignored issues
show
introduced by
The condition is_string($cacheFilename) is always true.
Loading history...
26
            $output->writeln('could not get temporary filename!');
27
28
            return 1;
29
        }
30
31 2
        unlink($cacheFilename);
32
33 2
        $cacheFilename .= '.cache';
34
35
        /**
36
        * @var string[] $sources
37
        */
38 2
        $sources = $input->getArgument('sources');
39
40 2
        Compiler::ObtainDispatcher(['cacheFile' => $cacheFilename], ...$sources);
41
42 2
        $output->write(file_get_contents($cacheFilename) ?: 'false');
43
44 2
        unlink($cacheFilename);
45
46 2
        return 0;
47
    }
48
49 4
    protected function configure() : void
50
    {
51 4
        $this->setDescription(
52 4
            'Update the cache used by the daft framework router'
53 4
        )->addArgument(
54 4
            'sources',
55 4
            InputArgument::REQUIRED | InputArgument::IS_ARRAY,
56 4
            'class names for sources'
57
        );
58 4
    }
59
}
60