Completed
Push — master ( c398b8...7a98a5 )
by Jitendra
10s
created

BaseCommand   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 71
rs 10
c 0
b 0
f 0
wmc 15

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A onConstruct() 0 2 1
A getCachePath() 0 7 2
A logging() 0 10 3
A missingOptions() 0 5 2
A promptAll() 0 19 5
1
<?php
2
3
/*
4
 * This file is part of the PHINT package.
5
 *
6
 * (c) Jitendra Adhikari <[email protected]>
7
 *     <https://github.com/adhocore>
8
 *
9
 * Licensed under MIT license.
10
 */
11
12
namespace Ahc\Phint\Console;
13
14
use Ahc\Cli\Input\Command;
15
use Ahc\Cli\IO\Interactor;
16
use Ahc\Phint\Util\Composer;
17
use Ahc\Phint\Util\Git;
18
use Ahc\Phint\Util\Path;
19
20
abstract class BaseCommand extends Command
21
{
22
    /** @var string Full path of log file */
23
    protected $_logFile;
24
25
    public function __construct()
26
    {
27
        $logFile = @\tempnam(\sys_get_temp_dir(), 'PHT') ?: '';
28
29
        $this->_logFile  = $logFile;
30
        $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...
31
        $this->_git      = new Git(null, $logFile);
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...
32
        $this->_composer = new Composer(null, $logFile);
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...
33
34
        $this->defaults();
35
        $this->onConstruct();
36
    }
37
38
    protected function onConstruct()
39
    {
40
        // ;)
41
    }
42
43
    protected function promptAll(Interactor $io, array $promptConfig)
44
    {
45
        $template = ['default' => null, 'choices' => [], 'retry' => 1, 'extra' => '', 'restore' => false];
46
47
        foreach ($this->missingOptions($promptConfig) as $name => $option) {
48
            $config  = ($promptConfig[$name] ?? []) + $template;
49
            $default = ($config['default'] ?? $option->default()) ?: null;
50
51
            if ($config['choices']) {
52
                $value = $io->choice($option->desc(), $config['choices'], $default);
53
            } else {
54
                $value = $io->prompt($option->desc() . $config['extra'], $default, null, $config['retry']);
55
            }
56
57
            if ($config['restore']) {
58
                $value = $config['choices'][$value] ?? $value;
59
            }
60
61
            $this->set($name, $value);
62
        }
63
    }
64
65
    protected function missingOptions(array $config)
66
    {
67
        return \array_filter($this->userOptions(), function ($name) use ($config) {
68
            return false !== ($config[$name] ?? null) && \in_array($this->$name, [null, []], true);
69
        }, \ARRAY_FILTER_USE_KEY);
70
    }
71
72
    protected function getCachePath(): string
73
    {
74
        if (!\Phar::running(false)) {
75
            return __DIR__ . '/../../.cache';
76
        }
77
78
        return $this->_pathUtil->getPhintPath('.cache');
79
    }
80
81
    protected function logging(string $mode = 'start')
82
    {
83
        if (!$logFile = $this->_logFile) {
84
            return;
85
        }
86
87
        if ('end' === $mode) {
88
            $this->app()->io()->comment("Check detailed logs at $logFile", true);
89
        } else {
90
            $this->app()->io()->comment("Logging to $logFile", true);
91
        }
92
    }
93
}
94