Completed
Push — dev ( b3004a...6293b4 )
by James Ekow Abaka
01:37
created

Cli::getVersion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 9
ccs 0
cts 5
cp 0
crap 6
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace yentu;
4
5
use clearice\argparser\ArgumentParser;
6
use clearice\io\Io;
7
use yentu\commands\CommandInterface;
8
9
class Cli
10
{
11
    private $command;
12
    private $io;
13
    private $argumentParser;
14
15
    public function __construct(Io $io, ArgumentParser $argumentParser, CommandInterface $command = null)
16
    {
17
        $this->command = $command;
18
        $this->io = $io;
19
        $this->argumentParser = $argumentParser;
20
    }
21
22
    /**
23
     * Display the greeting for the CLI user interface.
24
     */
25
    private function greet()
26
    {
27
        $version = $this->getVersion();
28
        $welcome = <<<WELCOME
29
Yentu Database Migration Tool
30
Version $version
31
32
33
WELCOME;
34
        $this->io->output($welcome);
35
    }
36
37
    private function getVersion()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
38
    {
39
        if (defined('PHING_BUILD_VERSION')) {
40
            return PHING_BUILD_VERSION;
41
        } else {
42
            $version = new \SebastianBergmann\Version(Yentu::VERSION, dirname(__DIR__));
43
            return $version->getVersion();
44
        }
45
    }
46
47
    public function run()
48
    {
49
        $this->greet();
50
        if($this->command) {
51
            try {
52
                $this->command->run();
53
            } catch (\yentu\exceptions\CommandException $e) {
54
                $this->io->resetOutputLevel();
55
                $this->io->error("Error! " . $e->getMessage() . "\n");
56
            } catch (\ntentan\atiaa\exceptions\DatabaseDriverException $e) {
57
                $this->io->resetOutputLevel();
58
                $this->io->error("Database driver failed: " . $e->getMessage() . "\n");
59
                if (isset($command)) {
0 ignored issues
show
Bug introduced by
The variable $command seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
60
                    $yentu->reverseCommand($command);
0 ignored issues
show
Bug introduced by
The variable $yentu does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
61
                }
62
            } catch (\yentu\exceptions\DatabaseManipulatorException $e) {
63
                $this->io->resetOutputLevel();
64
                $this->io->error("Failed to perform database action: " . $e->getMessage() . "\n");
65
                if (isset($command)) {
66
                    $yentu->reverseCommand($command);
67
                }
68
            } catch (\ntentan\atiaa\DescriptionException $e) {
0 ignored issues
show
Bug introduced by
The class ntentan\atiaa\DescriptionException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
69
                $this->io->resetOutputLevel();
70
                $this->io->error("Failed to perform database action: " . $e->getMessage() . "\n");
71
                if (isset($command)) {
72
                    $yentu->reverseCommand($command);
73
                }
74
            } catch (\yentu\exceptions\SyntaxErrorException $e) {
75
                $this->io->resetOutputLevel();
76
                $this->io->error("Error found in syntax: {$e->getMessage()}\n");
77
                if (isset($command)) {
78
                    $yentu->reverseCommand($command);
79
                }
80
            } catch (\PDOException $e) {
81
                $this->io->resetOutputLevel();
82
                $this->io->error("Failed to connect to database: {$e->getMessage()}\n");
83
            } catch (\ntentan\utils\exceptions\FileNotFoundException $e) {
84
                $this->io->resetOutputLevel();
85
                $this->io->error($e->getMessage() . "\n");        
86
            }
87
    
88
        } else {
89
            $this->io->error($this->argumentParser->getHelpMessage());
90
        }
91
    }
92
}
93