Installer   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
dl 0
loc 94
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A debug() 0 3 1
A __construct() 0 10 1
B run() 0 36 6
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the dotfiles project.
7
 *
8
 *     (c) Anthonius Munthi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Dotfiles\Plugins\PHPBrew;
15
16
use Dotfiles\Core\Config\Config;
17
use Dotfiles\Core\Util\Downloader;
18
use Dotfiles\Core\Util\Filesystem;
19
use Dotfiles\Core\Util\Toolkit;
20
use Psr\Log\LoggerInterface;
21
use Symfony\Component\Console\Output\OutputInterface;
22
use Symfony\Component\Process\Process;
23
24
class Installer
25
{
26
    public const DOWNLOAD_URL = 'https://github.com/phpbrew/phpbrew/raw/master/phpbrew';
27
28
    /**
29
     * @var Config
30
     */
31
    private $config;
32
33
    /**
34
     * @var Downloader
35
     */
36
    private $downloader;
37
38
    /**
39
     * @var LoggerInterface
40
     */
41
    private $logger;
42
43
    /**
44
     * @var OutputInterface
45
     */
46
    private $output;
47
48
    /**
49
     * Installer constructor.
50
     *
51
     * @param Config          $config
52
     * @param Downloader      $downloader
53
     * @param LoggerInterface $logger
54
     * @param OutputInterface $output
55
     */
56
    public function __construct(
57
        Config $config,
58
        Downloader $downloader,
59
        LoggerInterface $logger,
60
        OutputInterface $output
61
    ) {
62
        $this->config = $config;
63
        $this->downloader = $downloader;
64
        $this->logger = $logger;
65
        $this->output = $output;
66
    }
67
68
    /**
69
     * @param $message
70
     * @param array $context
71
     */
72
    public function debug($message, array $context = array()): void
73
    {
74
        $this->logger->debug('phpbrew: '.$message, $context);
75
    }
76
77
    /**
78
     * Run PHPBrew installation.
79
     *
80
     * @param bool $force
81
     */
82
    public function run(bool $force = false): void
83
    {
84
        $config = $this->config;
85
        $toFile = $config->get('dotfiles.temp_dir').DIRECTORY_SEPARATOR.'phpbrew';
86
        $installToFile = $config->get('dotfiles.bin_dir').DIRECTORY_SEPARATOR.'phpbrew';
87
        Toolkit::ensureFileDir($toFile);
88
89
        if (is_file($installToFile) && !$force) {
90
            $this->output->writeln('<comment>PHPBrew</comment> already installed, skipping');
91
92
            return;
93
        }
94
95
        if (!is_file($toFile)) {
96
            $downloader = $this->downloader;
97
            $downloader->run(static::DOWNLOAD_URL, $toFile);
98
        } else {
99
            $this->debug('file already downloaded, skipping');
100
        }
101
102
        $dryRun = $config->get('dotfiles.dry_run');
103
        if (!$dryRun) {
104
            $fs = new Filesystem();
105
            $fs->chmod($toFile, 0755);
106
            $fs->copy($toFile, $installToFile, false);
107
            $cmd = array(
108
                $installToFile,
109
                'init',
110
            );
111
            $process = new Process($cmd);
112
            $process->run(function ($type, $buffer): void {
113
                //@codeCoverageIgnoreStart
114
                if (Process::ERR === $type) {
115
                    $this->logger->error('phpbrew: '.$buffer);
116
                } else {
117
                    $this->debug($buffer);
118
                }
119
                //@codeCoverageIgnoreEnd
120
            });
121
        }
122
    }
123
}
124