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

NoSudo::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Dock\Installer\System\Linux;
4
5
use Dock\Installer\InstallerTask;
6
use Dock\IO\ProcessRunner;
7
use Dock\IO\UserInteraction;
8
use SRIO\ChainOfResponsibility\DependentChainProcessInterface;
9
10
class NoSudo extends InstallerTask implements DependentChainProcessInterface
11
{
12
    /**
13
     * @var ProcessRunner
14
     */
15
    private $processRunner;
16
    /**
17
     * @var UserInteraction
18
     */
19
    private $userInteraction;
20
21
    /**
22
     * @param UserInteraction $userInteraction
23
     * @param ProcessRunner   $processRunner
24
     */
25
    public function __construct(UserInteraction $userInteraction, ProcessRunner $processRunner)
26
    {
27
        $this->userInteraction = $userInteraction;
28
        $this->processRunner = $processRunner;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function dependsOn()
35
    {
36
        return ['docker'];
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getName()
43
    {
44
        return 'noSudo';
45
    }
46
47
    public function run()
48
    {
49
        if (!$this->isCurrentUserInDockerGroup()) {
50
            $this->userInteraction->writeTitle('Making docker work without sudo');
51
            $this->processRunner->run('sudo usermod -a -G docker $USER');
52
        }
53
    }
54
55
    private function isCurrentUserInDockerGroup()
56
    {
57
        return $this->processRunner->run('groups | grep docker', false)->isSuccessful();
58
    }
59
}
60