Completed
Push — dev ( 812e58...1f57b5 )
by James Ekow Abaka
21:22
created

Cli   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 68
ccs 0
cts 40
cp 0
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A greet() 0 11 1
A getVersion() 0 9 2
B run() 0 29 7
1
<?php
2
3
namespace yentu;
4
5
use clearice\argparser\ArgumentParser;
6
use clearice\io\Io;
7
use yentu\commands\Command;
8
9
class Cli
10
{
11
    private $command;
12
    private $io;
13
    private $argumentParser;
14
15
    public function __construct(Io $io, ArgumentParser $argumentParser, Command $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\NonReversibleCommandException $e) {
54
                $this->io->resetOutputLevel();
55
                $this->io->error("\nError: " . $e->getMessage() . "\n");
56
            } catch (\ntentan\atiaa\exceptions\DatabaseDriverException $e) {
57
                $this->io->resetOutputLevel();
58
                $this->io->error("\nDatabase error: " . $e->getMessage() . "\n");
59
                $this->command->reverse();
60
            } catch (\yentu\exceptions\YentuException $e) {
61
                $this->io->resetOutputLevel();
62
                $this->io->error("\nError: " . $e->getMessage() . "\n");
63
                $this->command->reverse();
64
            } catch (\PDOException $e) {
65
                $this->io->resetOutputLevel();
66
                $this->io->error("\nFailed to connect to database: {$e->getMessage()}\n");
67
            } catch (\ntentan\utils\exceptions\FileNotFoundException $e) {
68
                $this->io->resetOutputLevel();
69
                $this->io->error($e->getMessage() . "\n");        
70
            }
71
    
72
        } else {
73
            $this->io->error($this->argumentParser->getHelpMessage());
74
        }
75
    }
76
}
77