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

ArchiveExcludeInstaller::install()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 44
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 8.439
c 0
b 0
f 0
cc 6
eloc 26
nc 6
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
use Mediact\FileMapping\FileMappingInterface;
13
use Mediact\TestingSuite\Composer\MappingResolver;
14
15
class ArchiveExcludeInstaller implements InstallerInterface
16
{
17
    /** @var JsonFile */
18
    private $file;
19
20
    /** @var MappingResolver */
21
    private $resolver;
22
23
    /** @var IOInterface */
24
    private $io;
25
26
    /** @var string */
27
    private $destination;
28
29
    /** @var array */
30
    private $defaults = [
31
        '/bitbucket-pipelines.yml',
32
        '/.gitignore',
33
        '/tests'
34
    ];
35
36
    /**
37
     * Constructor.
38
     *
39
     * @param MappingResolver $resolver
40
     * @param IOInterface     $io
41
     * @param JsonFile|null   $file
42
     * @param string          $destination
43
     * @param array|null      $defaults
44
     */
45
    public function __construct(
46
        MappingResolver $resolver,
47
        IOInterface $io,
48
        JsonFile $file = null,
49
        string $destination = null,
50
        array $defaults = null
51
    ) {
52
        $this->resolver    = $resolver;
53
        $this->io          = $io;
54
        $this->file        = $file ?? new JsonFile(Factory::getComposerFile());
55
        $this->destination = $destination ?? getcwd();
56
        $this->defaults    = $defaults ?? $this->defaults;
57
    }
58
59
    /**
60
     * Install.
61
     *
62
     * @return void
63
     */
64
    public function install()
65
    {
66
        $definition = $this->file->read();
67
        $excluded   = isset($definition['archive']['exclude'])
68
            ? $definition['archive']['exclude']
69
            : [];
70
71
        $excluded = array_map(
72
            function (string $exclude): string {
73
                return substr($exclude, 0, 1) !== '/'
74
                    ? '/' . $exclude
75
                    : $exclude;
76
            },
77
            $excluded
78
        );
79
80
        $files = array_merge(
81
            $this->defaults,
82
            array_map(
83
                function (FileMappingInterface $mapping): string {
84
                    return '/' . $mapping->getRelativeDestination();
85
                },
86
                iterator_to_array(
87
                    $this->resolver->resolve()
88
                )
89
            )
90
        );
91
92
        foreach ($files as $file) {
93
            if (!in_array($file, $excluded)
94
                && file_exists($this->destination . $file)
95
            ) {
96
                $excluded[] = $file;
97
                $this->io->write(
98
                    sprintf(
99
                        '<info>Added:</info> %s to archive exclude in composer.json',
100
                        $file
101
                    )
102
                );
103
            }
104
        }
105
106
        $definition['archive']['exclude'] = $excluded;
107
        $this->file->write($definition);
108
    }
109
}
110