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

PackagesInstaller::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

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 5
nc 1
nop 5
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\Composer;
10
use Composer\IO\IOInterface;
11
use Mediact\Composer\DependencyInstaller\DependencyInstaller;
12
use Mediact\TestingSuite\Composer\ProjectTypeResolver;
13
14
class PackagesInstaller implements InstallerInterface
15
{
16
    /** @var DependencyInstaller */
17
    private $installer;
18
19
    /** @var Composer */
20
    private $composer;
21
22
    /** @var ProjectTypeResolver */
23
    private $typeResolver;
24
25
    /** @var IOInterface */
26
    private $io;
27
28
    /** @var array */
29
    private $mapping = [
30
        'default' => [],
31
        'magento1' => [
32
            [
33
                'name' => 'mediact/coding-standard-magento1',
34
                'version' => '@stable'
35
            ]
36
        ],
37
        'magento2' => [
38
            [
39
                'name' => 'mediact/coding-standard-magento2',
40
                'version' => '@stable'
41
            ]
42
        ]
43
    ];
44
45
    /**
46
     * Constructor.
47
     *
48
     * @param Composer                 $composer
49
     * @param ProjectTypeResolver      $typeResolver
50
     * @param IOInterface              $io
51
     * @param DependencyInstaller|null $installer
52
     * @param array                    $mapping
53
     */
54
    public function __construct(
55
        Composer $composer,
56
        ProjectTypeResolver $typeResolver,
57
        IOInterface $io,
58
        DependencyInstaller $installer = null,
59
        array $mapping = null
60
    ) {
61
        $this->composer     = $composer;
62
        $this->typeResolver = $typeResolver;
63
        $this->io           = $io;
64
        $this->installer    = $installer ?? new DependencyInstaller();
65
        $this->mapping      = $mapping ?? $this->mapping;
66
    }
67
68
    /**
69
     * Install.
70
     *
71
     * @return void
72
     */
73
    public function install()
74
    {
75
        $type = $this->typeResolver->resolve();
76
        if (!isset($this->mapping[$type])) {
77
            return;
78
        }
79
80
        foreach ($this->mapping[$type] as $package) {
81
            if (!$this->isPackageRequired($package['name'])) {
82
                $this->io->write(
83
                    sprintf('Requiring package %s', $package['name'])
84
                );
85
86
                $this->installer->installPackage(
87
                    $package['name'],
88
                    $package['version']
89
                );
90
            }
91
        }
92
    }
93
94
    /**
95
     * Whether a package has been required.
96
     *
97
     * @param string $packageName
98
     *
99
     * @return bool
100
     */
101
    private function isPackageRequired(string $packageName): bool
102
    {
103
        foreach ($this->composer->getPackage()->getRequires() as $require) {
104
            if ($require->getTarget() === $packageName) {
105
                return true;
106
            }
107
        }
108
109
        return false;
110
    }
111
}
112