ArchiveExcludeInstaller::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 10
cc 1
nc 1
nop 5
crap 1
1
<?php
2
3
/**
4
 * Copyright MediaCT. All rights reserved.
5
 * https://www.mediact.nl
6
 */
7
8
namespace Mediact\TestingSuite\Composer\Installer;
9
10
use Composer\Factory;
11
use Composer\IO\IOInterface;
12
use Composer\Json\JsonFile;
13
use Mediact\FileMapping\FileMappingInterface;
14
use Mediact\TestingSuite\Composer\MappingResolver;
15
16
/**
17
 * @SuppressWarnings(PHPMD.ShortVariable)
18
 * @SuppressWarnings(PHPMD.StaticAccess)
19
 */
20
class ArchiveExcludeInstaller implements InstallerInterface
21
{
22
    /** @var JsonFile */
23
    private $file;
24
25
    /** @var MappingResolver */
26
    private $resolver;
27
28
    /** @var IOInterface */
29
    private $io;
30
31
    /** @var string */
32
    private $destination;
33
34
    /** @var array */
35
    private $defaults = [
36
        '/bitbucket-pipelines.yml',
37
        '/docker-compose.yml',
38
        '/examples',
39
        '/example',
40
        '/.env.dev',
41
        '/.gitattributes',
42
        '/.gitignore',
43
        '/tests'
44
    ];
45
46
    /**
47
     * Constructor.
48
     *
49
     * @param MappingResolver $resolver
50
     * @param IOInterface     $io
51
     * @param JsonFile|null   $file
52
     * @param string          $destination
53
     * @param array|null      $defaults
54
     */
55 1
    public function __construct(
56
        MappingResolver $resolver,
57
        IOInterface $io,
58
        JsonFile $file = null,
59
        string $destination = null,
60
        array $defaults = null
61
    ) {
62 1
        $this->resolver    = $resolver;
63 1
        $this->io          = $io;
64 1
        $this->file        = $file ?? new JsonFile(Factory::getComposerFile());
65 1
        $this->destination = $destination ?? getcwd();
66 1
        $this->defaults    = $defaults ?? $this->defaults;
67 1
    }
68
69
    /**
70
     * Install.
71
     *
72
     * @return void
73
     */
74 1
    public function install()
75
    {
76 1
        $definition = $this->file->read();
77 1
        $excluded   = $definition['archive']['exclude'] ?? [];
78
79 1
        $excluded = array_map(
80
            function (string $exclude): string {
81 1
                return substr($exclude, 0, 1) !== '/'
82 1
                    ? '/' . $exclude
83 1
                    : $exclude;
84 1
            },
85 1
            $excluded
86
        );
87
88 1
        $files = array_merge(
89 1
            $this->defaults,
90 1
            array_map(
91
                function (FileMappingInterface $mapping): string {
92 1
                    return '/' . $mapping->getRelativeDestination();
93 1
                },
94 1
                iterator_to_array(
95 1
                    $this->resolver->resolve()
96
                )
97
            )
98
        );
99
100 1
        foreach ($files as $file) {
101
            if (
102 1
                !in_array($file, $excluded)
103 1
                && file_exists($this->destination . $file)
104
            ) {
105 1
                $excluded[] = $file;
106 1
                $this->io->write(
107 1
                    sprintf(
108 1
                        '<info>Added:</info> %s to archive exclude in composer.json',
109 1
                        $file
110
                    )
111
                );
112
            }
113
        }
114
115 1
        $definition['archive']['exclude'] = $excluded;
116 1
        $this->file->write($definition);
117 1
    }
118
}
119