Passed
Branch 1.0.x (7d3940)
by Koldo
02:45
created

ComposerJson::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Antidot\Installer\Template;
6
7
use Antidot\Installer\RunInstall;
8
use Composer\Factory;
9
use Composer\IO\IOInterface;
10
use Composer\Json\JsonFile;
11
use Composer\Json\JsonManipulator;
12
13
use function array_merge;
14
use function file_get_contents;
15
use function file_put_contents;
16
use function preg_replace;
17
use function sprintf;
18
19
class ComposerJson
20
{
21
    private IOInterface $io;
22
    private RunInstall $runInstall;
23
24 6
    public function __construct(IOInterface $io, RunInstall $runInstall)
25
    {
26 6
        $this->io = $io;
27 6
        $this->runInstall = $runInstall;
28 6
    }
29
30
    /**
31
     * @param string $installationPath
32
     * @param array<string, string> $dependencies
33
     * @param array<string> $removePatterns
34
     */
35 1
    public function prepare(string $installationPath, array $dependencies, array $removePatterns): void
36
    {
37
38
        // Update composer.json (project is proprietary by default)
39
        /** @psalm-suppress MixedArgument */
40 1
        $json = new JsonFile(Factory::getComposerFile());
41 1
        $contents = file_get_contents($json->getPath());
42 1
        $manipulator = new JsonManipulator($contents);
43
        // new projects are most of the time proprietary
44 1
        $manipulator->addMainKey('license', 'proprietary');
45
46 1
        foreach ($dependencies as $package => $version) {
47 1
            $manipulator->addLink('require', $package, $version);
48
        }
49
50 1
        $matchPatterns = array_merge([
51 1
            '{^\s*+"name":.*,$\n}m',
52
            '{^\s*+"description":.*,$\n}m',
53
            '{^\s*+"antidot-fw\/installer":.*,$\n}m',
54
            '{^\s*+"repositories":.*$\n^\s*+\{$\n^\s*+.*,$\n^\s*+.*$\n^\s*+\}$\n^\s*+\],$\n}m' // only for development
55 1
        ], $removePatterns);
56
57
        /** @psalm-suppress MixedArgument */
58 1
        $contents = preg_replace($matchPatterns, '', $manipulator->getContents(), 1);
59
60 1
        $namespace = $this->io->ask('Select the primary namespace for your application [<info>App</info>]: ', 'App');
61 1
        $contents = preg_replace(
62
            [
63 1
                '{(^\s*+")App(\\\\\\\\":\s.*$\n)}m',
64
                '{(^\s*+")App(\\\\\\\\Test\\\\\\\\":\s.*$\n)}m',
65
            ],
66 1
            '$1' . $namespace . '$2',
67 1
            $contents,
68 1
            1
69
        );
70
71 1
        file_put_contents($installationPath . '/composer.json', $contents);
72
73 1
        $this->runInstall->exec(sprintf(
74 1
            'cd %s && rm -rf vendor/ composer.lock && composer install  --ansi',
75 1
            $installationPath
76
        ));
77 1
    }
78
}
79