Test Failed
Pull Request — master (#4)
by Ashoka
05:38
created

PipelinesInstaller::chooseMapping()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
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 null|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
65
        $pattern  !== null && $this->pattern  = $pattern;
66
        $filename !== null && $this->filename = $filename;
67
        $types    !== null && $this->types    = $types;
68
    }
69
70
    /**
71
     * Install.
72
     *
73
     * @return void
74
     */
75
    public function install()
76
    {
77
        if (file_exists($this->destination . '/' . $this->filename)
78
            || !$this->isBitbucket()
79
        ) {
80
            return;
81
        }
82
83
84
        $mapping = new UnixFileMapping(
85
            sprintf(
86
                __DIR__ . '/../../templates/files/pipelines-%s',
87
                $this->chooseMapping()
88
            ),
89
            $this->destination,
0 ignored issues
show
Bug introduced by
It seems like $this->destination can also be of type null; however, parameter $destinationDirectory of Mediact\FileMapping\UnixFileMapping::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

89
            /** @scrutinizer ignore-type */ $this->destination,
Loading history...
90
            $this->filename
91
        );
92
93
        $this->fileInstaller->installFile($mapping);
94
        $this->io->write(
95
            sprintf(
96
                '<info>Installed:</info> %s',
97
                $mapping->getRelativeDestination()
98
            )
99
        );
100
    }
101
102
    /**
103
     * Choose the mapping to install.
104
     *
105
     * @return mixed
106
     */
107
    private function chooseMapping()
108
    {
109
        $labels = array_values($this->types);
110
        $keys   = array_keys($this->types);
111
112
        $selected = $this->io->select(
113
            'Bitbucket has been detected. Which pipelines script do you want to install?',
114
            $labels,
115
            0
116
        );
117
118
        return $keys[$selected];
119
    }
120
121
    /**
122
     * Check whether the project is on Bitbucket.
123
     *
124
     * @return bool
125
     */
126
    private function isBitbucket(): bool
127
    {
128
        $process = $this->processBuilder
129
            ->setPrefix('/usr/bin/env')
130
            ->setArguments(['git', 'remote', '-v'])
131
            ->getProcess();
132
133
        $process->run();
134
135
        return strpos($process->getOutput(), $this->pattern) !== false;
136
    }
137
}
138