Test Failed
Pull Request — master (#4)
by Ashoka
09:01 queued 03:06
created

PipelinesInstaller::isBitbucket()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
/**
3
 * Copyright MediaCT. All rights reserved.
4
 * https://www.mediact.nl
5
 */
6
7
namespace Mediact\TestingSuite\Composer\Installer;
8
9
use Composer\IO\IOInterface;
10
use Mediact\Composer\FileInstaller;
11
use Mediact\FileMapping\UnixFileMapping;
12
use Symfony\Component\Process\ProcessBuilder;
13
14
class PipelinesInstaller implements InstallerInterface
15
{
16
    /** @var FileInstaller */
17
    private $fileInstaller;
18
19
    /** @var IOInterface */
20
    private $io;
21
22
    /** @var ProcessBuilder */
23
    private $processBuilder;
24
25
    /** @var string */
26
    private $destination;
27
28
    /** @var string */
29
    private $pattern = 'bitbucket.org';
30
31
    /** @var string */
32
    private $filename = 'bitbucket-pipelines.yml';
33
34
    /** @var array */
35
    private $types = [
36
        'mediact' => 'MediaCT pipelines script',
37
        'basic'   => 'Basic pipelines script'
38
    ];
39
40
    /**
41
     * Constructor.
42
     *
43
     * @param FileInstaller  $fileInstaller
44
     * @param IOInterface    $io
45
     * @param ProcessBuilder $processBuilder
46
     * @param string|null    $destination
47
     * @param string|null    $pattern
48
     * @param string|null    $filename
49
     * @param array|null     $types
50
     */
51
    public function __construct(
52
        FileInstaller $fileInstaller,
53
        IOInterface $io,
54
        ProcessBuilder $processBuilder = null,
55
        string $destination = null,
56
        string $pattern = null,
57
        string $filename = null,
58
        array $types = null
59
    ) {
60
        $this->fileInstaller  = $fileInstaller;
61
        $this->io             = $io;
62
        $this->processBuilder = $processBuilder ?? new ProcessBuilder();
63
        $this->destination    = $destination ?? getcwd();
64
        $this->pattern        = $pattern ?? $this->pattern;
65
        $this->filename       = $filename ?? $this->filename;
66
        $this->types          = $types ?? $this->types;
67
    }
68
69
    /**
70
     * Install.
71
     *
72
     * @return void
73
     */
74
    public function install()
75
    {
76
        if (file_exists($this->destination . '/' . $this->filename)
77
            || !$this->isBitbucket()
78
        ) {
79
            return;
80
        }
81
82
83
        $mapping = new UnixFileMapping(
84
            sprintf(
85
                __DIR__ . '/../../templates/files/pipelines-%s',
86
                $this->chooseMapping()
87
            ),
88
            $this->destination,
89
            $this->filename
90
        );
91
92
        $this->fileInstaller->installFile($mapping);
93
        $this->io->write(
94
            sprintf(
95
                '<info>Installed:</info> %s',
96
                $mapping->getRelativeDestination()
97
            )
98
        );
99
    }
100
101
    /**
102
     * Choose the mapping to install.
103
     *
104
     * @return string
105
     */
106
    private function chooseMapping():string
107
    {
108
        $labels = array_values($this->types);
109
        $keys   = array_keys($this->types);
110
111
        $selected = $this->io->select(
112
            'Bitbucket has been detected. Which pipelines script do you want to install?',
113
            $labels,
114
            key($labels)
115
        );
116
117
        return $keys[$selected];
118
    }
119
120
    /**
121
     * Check whether the project is on Bitbucket.
122
     *
123
     * @return bool
124
     */
125
    private function isBitbucket(): bool
126
    {
127
        $process = $this->processBuilder
128
            ->setPrefix('/usr/bin/env')
129
            ->setArguments(['git', 'remote', '-v'])
130
            ->getProcess();
131
132
        $process->run();
133
134
        return strpos($process->getOutput(), $this->pattern) !== false;
135
    }
136
}
137