PipelinesInstaller   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 6
eloc 37
c 6
b 0
f 0
dl 0
loc 103
ccs 32
cts 32
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A isBitbucket() 0 6 1
A install() 0 30 4
1
<?php
2
3
/**
4
 * Copyright MediaCT. All rights reserved.
5
 * https://www.mediact.nl
6
 */
7
8
namespace Mediact\TestingSuite\Composer\Installer;
9
10
use Composer\IO\IOInterface;
11
use Mediact\Composer\FileInstaller;
12
use Mediact\FileMapping\UnixFileMapping;
13
use Mediact\FileMapping\UnixFileMappingReader;
14
use Mediact\TestingSuite\Composer\ProjectTypeResolver;
15
use Mediact\TestingSuite\Composer\Factory\ProcessFactoryInterface;
16
17
/**
18
 * @SuppressWarnings(PHPMD.ShortVariable)
19
 */
20
class PipelinesInstaller implements InstallerInterface
21
{
22
    /** @var FileInstaller */
23
    private $fileInstaller;
24
25
    /** @var IOInterface */
26
    private $io;
27
28
    /** @var ProcessFactoryInterface */
29
    private $processFactory;
30
31
    /** @var ProjectTypeResolver */
32
    private $typeResolver;
33
34
    /** @var string */
35
    private $destination;
36
37
    /** @var string */
38
    private $pattern = 'bitbucket.org';
39
40
    /** @var string */
41
    private $filename = 'bitbucket-pipelines.yml';
42
43
    /**
44
     * Constructor.
45
     *
46
     * @param FileInstaller           $fileInstaller
47
     * @param IOInterface             $io
48
     * @param ProcessFactoryInterface $processFactory
49
     * @param ProjectTypeResolver     $typeResolver
50
     * @param string|null             $destination
51
     * @param string|null             $pattern
52
     * @param string|null             $filename
53
     */
54 4
    public function __construct(
55
        FileInstaller $fileInstaller,
56
        IOInterface $io,
57
        ProcessFactoryInterface $processFactory,
58
        ProjectTypeResolver $typeResolver,
59
        string $destination = null,
60
        string $pattern = null,
61
        string $filename = null
62
    ) {
63 4
        $this->fileInstaller  = $fileInstaller;
64 4
        $this->io             = $io;
65 4
        $this->processFactory = $processFactory;
66 4
        $this->typeResolver   = $typeResolver;
67 4
        $this->destination    = $destination ?? getcwd();
68 4
        $this->pattern        = $pattern ?? $this->pattern;
69 4
        $this->filename       = $filename ?? $this->filename;
70 4
    }
71
72
    /**
73
     * Install.
74
     *
75
     * @return void
76
     */
77 4
    public function install()
78
    {
79
        if (
80 4
            file_exists($this->destination . '/' . $this->filename)
81 4
            || !$this->isBitbucket()
82
        ) {
83 2
            return;
84
        }
85
86
87
        $files = [
88 2
            __DIR__ . '/../../templates/mapping/files',
89 2
            sprintf(
90 2
                __DIR__ . '/../../templates/mapping/pipelines/%s',
91 2
                $this->typeResolver->resolve()
92
            )
93
        ];
94
95 2
        $reader = new UnixFileMappingReader(
96 2
            __DIR__ . '/../../templates/files',
97 2
            getcwd(),
98 2
            ...$files
99
        );
100
101 2
        foreach ($reader as $mapping) {
102 2
            $this->fileInstaller->installFile($mapping);
103 2
            $this->io->write(
104 2
                sprintf(
105 2
                    '<info>Installed:</info> %s',
106 2
                    $mapping->getRelativeDestination()
107
                )
108
            );
109
        }
110 2
    }
111
112
    /**
113
     * Check whether the project is on Bitbucket.
114
     *
115
     * @return bool
116
     */
117 3
    private function isBitbucket(): bool
118
    {
119 3
        $process = $this->processFactory->create('git remote -v');
120 3
        $process->run();
121
122 3
        return strpos($process->getOutput(), $this->pattern) !== false;
123
    }
124
}
125