Test Failed
Pull Request — master (#4)
by Ashoka
05:46
created

ArchiveExcludeInstaller::__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\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   = $definition['archive']['exclude'] ?? [];
68
69
        $excluded = array_map(
70
            function (string $exclude): string {
71
                return substr($exclude, 0, 1) !== '/'
72
                    ? '/' . $exclude
73
                    : $exclude;
74
            },
75
            $excluded
76
        );
77
78
        $files = array_merge(
79
            $this->defaults,
80
            array_map(
81
                function (FileMappingInterface $mapping): string {
82
                    return '/' . $mapping->getRelativeDestination();
83
                },
84
                iterator_to_array(
85
                    $this->resolver->resolve()
86
                )
87
            )
88
        );
89
90
        foreach ($files as $file) {
91
            if (!in_array($file, $excluded)
92
                && file_exists($this->destination . $file)
93
            ) {
94
                $excluded[] = $file;
95
                $this->io->write(
96
                    sprintf(
97
                        '<info>Added:</info> %s to archive exclude in composer.json',
98
                        $file
99
                    )
100
                );
101
            }
102
        }
103
104
        $definition['archive']['exclude'] = $excluded;
105
        $this->file->write($definition);
106
    }
107
}
108