Passed
Pull Request — master (#10)
by Ashoka
06:52
created

PipelinesInstaller::chooseMapping()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 0
crap 2
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 Mediact\TestingSuite\Composer\Factory\ProcessFactoryInterface;
13
use Symfony\Component\Process\Process;
14
15
class PipelinesInstaller implements InstallerInterface
16
{
17
    /** @var FileInstaller */
18
    private $fileInstaller;
19
20
    /** @var IOInterface */
21
    private $io;
22
23
    /** @var ProcessFactoryInterface */
24
    private $processFactory;
25
26
    /** @var string */
27
    private $destination;
28
29
    /** @var string */
30
    private $pattern = 'bitbucket.org';
31
32
    /** @var string */
33
    private $filename = 'bitbucket-pipelines.yml';
34
35
    /** @var array */
36
    private $types = [
37
        'mediact' => 'MediaCT pipelines script',
38
        'basic'   => 'Basic pipelines script'
39
    ];
40
41
    /**
42
     * Constructor.
43
     *
44
     * @param FileInstaller           $fileInstaller
45
     * @param IOInterface             $io
46
     * @param ProcessFactoryInterface $processFactory
47
     * @param string|null             $destination
48
     * @param string|null             $pattern
49
     * @param string|null             $filename
50
     * @param array|null              $types
51
     */
52 4
    public function __construct(
53
        FileInstaller $fileInstaller,
54
        IOInterface $io,
55
        ProcessFactoryInterface $processFactory,
56
        string $destination = null,
57
        string $pattern = null,
58
        string $filename = null,
59
        array $types = null
60
    ) {
61 4
        $this->fileInstaller  = $fileInstaller;
62 4
        $this->io             = $io;
63 4
        $this->processFactory = $processFactory;
64 4
        $this->destination    = $destination ?? getcwd();
65 4
        $this->pattern        = $pattern ?? $this->pattern;
66 4
        $this->filename       = $filename ?? $this->filename;
67 4
        $this->types          = $types ?? $this->types;
68 4
    }
69
70
    /**
71
     * Install.
72
     *
73
     * @return void
74
     */
75 4
    public function install()
76
    {
77 4
        if (file_exists($this->destination . '/' . $this->filename)
78 3
            || !$this->isBitbucket()
79
        ) {
80 2
            return;
81
        }
82
83 2
        $mapping = new UnixFileMapping(
84 2
            __DIR__ . '/../../templates/files/pipelines',
85 2
            $this->destination,
86 2
            $this->filename
87
        );
88
89 2
        $this->fileInstaller->installFile($mapping);
90 2
        $this->io->write(
91 2
            sprintf(
92 2
                '<info>Installed:</info> %s',
93 2
                $mapping->getRelativeDestination()
94
            )
95
        );
96 2
    }
97
98
    /**
99
     * Check whether the project is on Bitbucket.
100
     *
101
     * @return bool
102
     */
103 3
    private function isBitbucket(): bool
104
    {
105 3
        $process = $this->processFactory->create('git remote -v');
106 3
        $process->run();
107
108 3
        return strpos($process->getOutput(), $this->pattern) !== false;
109
    }
110
}
111