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

GrumPhpInstaller::install()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 8.439
c 0
b 0
f 0
cc 5
eloc 19
nc 10
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\Factory;
10
use Composer\IO\IOInterface;
11
use Composer\Json\JsonFile;
12
13
class GrumPhpInstaller implements InstallerInterface
14
{
15
    /** @var JsonFile */
16
    private $file;
17
18
    /** @var IOInterface */
19
    private $io;
20
21
    /** @var string */
22
    private $destination;
23
24
    /**
25
     * Constructor.
26
     *
27
     * @param IOInterface|null $io
28
     * @param JsonFile         $file
29
     * @param string|null      $destination
30
     */
31
    public function __construct(
32
        IOInterface $io,
33
        JsonFile $file = null,
34
        string $destination = null
35
    ) {
36
        $this->file        = $file;
37
        $this->io          = $io;
38
        $this->file        = $file ?: new JsonFile(Factory::getComposerFile());
39
        $this->destination = $destination ?: getcwd();
40
    }
41
42
    /**
43
     * Install.
44
     *
45
     * @return void
46
     */
47
    public function install()
48
    {
49
        $grumPhpFile = $this->destination . '/grumphp.yml';
50
51
        if (file_exists($grumPhpFile)) {
52
            unlink($grumPhpFile);
53
            $this->io->write(
54
                sprintf(
55
                    '<comment>Removed:</comment> existing GrumPHP config file %s',
56
                    $grumPhpFile
57
                )
58
            );
59
        }
60
61
        $definition = $this->file->read();
62
        if (!empty($definition['extra']['grumphp']['config-default-path'])) {
63
            return;
64
        }
65
66
        if (!array_key_exists('extra', $definition)) {
67
            $definition['extra'] = [];
68
        }
69
70
        if (!array_key_exists('grumphp', $definition['extra'])) {
71
            $definition['extra']['grumphp'] = [];
72
        }
73
74
        $definition['extra']['grumphp']['config-default-path'] =
75
            'vendor/mediact/testing-suite/config/default/grumphp.yml';
76
77
        $this->file->write($definition);
78
        $this->io->write(
79
            '<info>Added:</info> GrumPHP config to composer.json'
80
        );
81
    }
82
}
83