Test Failed
Pull Request — master (#4)
by Ashoka
06:12
created

PackagesInstaller::install()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 4
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\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
66
        $mapping !== null && $this->mapping = $mapping;
67
    }
68
69
    /**
70
     * Install.
71
     *
72
     * @return void
73
     */
74
    public function install()
75
    {
76
        $type = $this->typeResolver->resolve();
77
        if (!isset($this->mapping[$type])) {
78
            return;
79
        }
80
81
        foreach ($this->mapping[$type] as $package) {
82
            if (!$this->isPackageRequired($package['name'])) {
83
                $this->io->write(
84
                    sprintf('Requiring package %s', $package['name'])
85
                );
86
87
                $this->installer->installPackage(
88
                    $package['name'],
89
                    $package['version']
90
                );
91
            }
92
        }
93
    }
94
95
    /**
96
     * Whether a package has been required.
97
     *
98
     * @param string $packageName
99
     *
100
     * @return bool
101
     */
102
    private function isPackageRequired(string $packageName): bool
103
    {
104
        foreach ($this->composer->getPackage()->getRequires() as $require) {
105
            if ($require->getTarget() === $packageName) {
106
                return true;
107
            }
108
        }
109
110
        return false;
111
    }
112
}
113