Completed
Push — master ( 39c07f...aa04c1 )
by Jitendra
11s
created

BaseCommand::getCachePath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Ahc\Phint\Console;
4
5
use Ahc\Cli\Input\Command;
6
use Ahc\Cli\IO\Interactor;
7
use Ahc\Phint\Util\Composer;
8
use Ahc\Phint\Util\Git;
9
use Ahc\Phint\Util\Path;
10
11
abstract class BaseCommand extends Command
12
{
13
    public function __construct()
14
    {
15
        $this->_git      = new Git;
0 ignored issues
show
Bug Best Practice introduced by
The property _git does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
16
        $this->_pathUtil = new Path;
0 ignored issues
show
Bug Best Practice introduced by
The property _pathUtil does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
17
        $this->_composer = new Composer;
0 ignored issues
show
Bug Best Practice introduced by
The property _composer does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
18
19
        $this->defaults();
20
        $this->onConstruct();
21
    }
22
23
    protected function onConstruct()
24
    {
25
        // ;)
26
    }
27
28
    protected function promptAll(Interactor $io, array $promptConfig)
29
    {
30
        $template = ['default' => null, 'choices' => [], 'retry' => 1, 'extra' => '', 'restore' => false];
31
32
        foreach ($this->missingOptions($promptConfig) as $name => $option) {
33
            $config  = ($promptConfig[$name] ?? []) + $template;
34
            $default = $config['default'] ?? $option->default();
35
36
            if ($config['choices']) {
37
                $value = $io->choice($option->desc(), $config['choices'], $default);
38
            } else {
39
                $value = $io->prompt($option->desc() . $config['extra'], $default, null, $config['retry']);
40
            }
41
42
            if ($config['restore']) {
43
                $value = $config['choices'][$value] ?? $value;
44
            }
45
46
            $this->set($name, $value);
47
        }
48
    }
49
50
    protected function missingOptions(array $config)
51
    {
52
        return \array_filter($this->userOptions(), function ($name) use ($config) {
53
            return null === $this->$name && false !== ($config[$name] ?? null);
54
        }, \ARRAY_FILTER_USE_KEY);
55
    }
56
57
    protected function getCachePath(): string
58
    {
59
        if (!\Phar::running(false)) {
60
            return __DIR__ . '/../../.cache';
61
        }
62
63
        return $this->_pathUtil->getPhintPath('.cache');
64
    }
65
}
66