Completed
Push — master ( 95a54c...7178fa )
by Samuel
10:58
created

PhpSsh   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 15
c 3
b 0
f 0
lcom 1
cbo 4
dl 0
loc 132
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A dependsOn() 0 4 1
A guessSsh2PhpPackage() 0 14 4
A hasHomebrewPackage() 0 4 1
A sshExtensionInstalled() 0 4 1
A __construct() 0 5 1
A run() 0 20 3
A installHomebrewPackage() 0 13 2
A promptForPackageName() 0 14 1
1
<?php
2
3
namespace Dock\Installer\System\Mac;
4
5
use Dock\Installer\InstallerTask;
6
use Dock\IO\ProcessRunner;
7
use Dock\IO\UserInteraction;
8
use SRIO\ChainOfResponsibility\DependentChainProcessInterface;
9
use Symfony\Component\Console\Question\Question;
10
11
class PhpSsh extends InstallerTask implements DependentChainProcessInterface
12
{
13
    /**
14
     * @var ProcessRunner
15
     */
16
    private $processRunner;
17
    /**
18
     * @var UserInteraction
19
     */
20
    private $userInteraction;
21
22
    /**
23
     * @param UserInteraction        $userInteraction
24
     * @param \Dock\IO\ProcessRunner $processRunner
25
     */
26
    public function __construct(UserInteraction $userInteraction, ProcessRunner $processRunner)
27
    {
28
        $this->userInteraction = $userInteraction;
29
        $this->processRunner = $processRunner;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function getName()
36
    {
37
        return 'phpSsh';
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function dependsOn()
44
    {
45
        return ['homebrew'];
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function run()
52
    {
53
        $this->userInteraction->writeTitle('Checking PHP SSH2 extension');
54
55
        if ($this->sshExtensionInstalled()) {
56
            return;
57
        }
58
59
        $this->userInteraction->write('PHP SSH2 extension is required.');
60
61
        $package = $this->promptForPackageName();
62
63
        if ($package == 'n') {
64
            $this->userInteraction->write('Skipping PHP SSH2 extension installation, do it yourself.');
65
        } else {
66
            $this->installHomebrewPackage($package);
67
        }
68
69
        throw new \RuntimeException('Please re-run this installation script to have enabled PHP-SSH2 extension');
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    private function guessSsh2PhpPackage()
76
    {
77
        if (PHP_VERSION_ID > 50600) {
78
            $package = 'php56-ssh2';
79
        } elseif (PHP_VERSION_ID > 50500) {
80
            $package = 'php55-ssh2';
81
        } elseif (PHP_VERSION_ID > 50400) {
82
            $package = 'php54-ssh2';
83
        } else {
84
            $package = 'php53-ssh2';
85
        }
86
87
        return $package;
88
    }
89
90
    /**
91
     * @param string $package
92
     *
93
     * @return bool
94
     */
95
    private function hasHomebrewPackage($package)
96
    {
97
        return $this->processRunner->run('brew install --dry-run '.$package, false)->isSuccessful();
98
    }
99
100
    /**
101
     * @return bool
102
     */
103
    private function sshExtensionInstalled()
104
    {
105
        return function_exists('ssh2_exec');
106
    }
107
108
    /**
109
     * @param $package
110
     */
111
    protected function installHomebrewPackage($package)
112
    {
113
        // Check if the package is known
114
        if (!$this->hasHomebrewPackage($package)) {
115
            $this->userInteraction->write(sprintf('Package "%s" not found, tapping default PHP brews', $package));
116
117
            $this->processRunner->run('brew tap homebrew/dupes');
118
            $this->processRunner->run('brew tap homebrew/versions');
119
            $this->processRunner->run('brew tap homebrew/homebrew-php');
120
        }
121
122
        $this->processRunner->run('brew install '.$package);
123
    }
124
125
    /**
126
     * @return string
127
     */
128
    protected function promptForPackageName()
129
    {
130
        $defaultPhpVersion = $this->guessSsh2PhpPackage();
131
        $question = new Question(
132
            sprintf(
133
                'Which homebrew package do you want to install (default "%s") ? ("n" for nothing)',
134
                $defaultPhpVersion
135
            ),
136
            $defaultPhpVersion
137
        );
138
        $package = $this->userInteraction->ask($question);
139
140
        return $package;
141
    }
142
}
143