Passed
Push — feature/issue-250 ( 5a78c9 )
by Mikaël
07:02
created

Composer::getComposerFileContent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WsdlToPhp\PackageGenerator\File;
6
7
use Composer\Console\Application;
8
use Symfony\Component\Console\Input\ArrayInput;
9
use WsdlToPhp\PackageGenerator\Model\EmptyModel;
10
11
final class Composer extends AbstractFile
12
{
13
    public const JSON_FILE_EXTENSION = 'json';
14
15
    /**
16
     * Tests purpose: do not run composer update command.
17
     */
18
    protected bool $runComposerUpdate = true;
19
20 12
    public function setRunComposerUpdate(bool $runComposerUpdate): Composer
21
    {
22 12
        $this->runComposerUpdate = $runComposerUpdate;
23
24 12
        return $this;
25
    }
26
27 22
    public function getRunComposerUpdate(): bool
28
    {
29 22
        return $this->runComposerUpdate;
30
    }
31
32 12
    public function getFileExtension(): string
33
    {
34 12
        return self::JSON_FILE_EXTENSION;
35
    }
36
37 20
    protected function writeFile(): void
38
    {
39 20
        $composer = new Application();
40 20
        $composer->setAutoExit(false);
41 20
        $composer->run(new ArrayInput([
42 20
            'command' => 'init',
43
            '--verbose' => true,
44
            '--no-interaction' => true,
45 20
            '--name' => $this->getGenerator()->getOptionComposerName(),
46 20
            '--description' => sprintf('Package generated from %s using wsdltophp/packagegenerator', $this->getGenerator()->getWsdl()->getName()),
47
            '--require' => [
48
                'php:>=7.4',
49
                'ext-dom:*',
50
                'ext-mbstring:*',
51
                'ext-soap:*',
52
                'wsdltophp/packagebase:~5.0',
53
            ],
54 20
            '--working-dir' => $this->getGenerator()->getOptionDestination(),
55
        ]));
56
57 20
        $this->completeComposerJson();
58
59 20
        if ($this->getRunComposerUpdate()) {
60 10
            $composer->run(new ArrayInput([
61 10
                'command' => 'update',
62
                '--verbose' => true,
63
                '--optimize-autoloader' => true,
64
                '--no-dev' => true,
65 10
                '--working-dir' => $this->getGenerator()->getOptionDestination(),
66
            ]));
67
        }
68 20
    }
69
70 20
    protected function completeComposerJson(): Composer
71
    {
72 20
        $content = $this->getComposerFileContent();
73 20
        if (is_array($content) && !empty($content)) {
74
            $this
75 20
                ->addAutoloadToComposerJson($content)
76 20
                ->addComposerSettings($content)
77
            ;
78
        }
79
80 20
        return $this->setComposerFileContent($content);
81
    }
82
83 20
    protected function addAutoloadToComposerJson(array &$content): Composer
84
    {
85 20
        $content['autoload'] = [
86 20
            'psr-4' => $this->getPsr4Autoload(),
87
        ];
88
89 20
        return $this;
90
    }
91
92 20
    protected function addComposerSettings(array &$content): Composer
93
    {
94 20
        $content = array_merge_recursive($content, $this->getGenerator()->getOptionComposerSettings());
95
96 20
        return $this;
97
    }
98
99 20
    protected function getPsr4Autoload(): array
100
    {
101 20
        $namespace = new EmptyModel($this->getGenerator(), '');
102 20
        if (!empty($namespace->getNamespace())) {
103
            $namespaceKey = sprintf('%s\\', $namespace->getNamespace());
104
        } else {
105 20
            $namespaceKey = '';
106
        }
107 20
        $src = rtrim($this->generator->getOptionSrcDirname(), DIRECTORY_SEPARATOR);
108
109
        return [
110 20
            $namespaceKey => sprintf(
111 20
                './%s%s',
112 20
                empty($src) ? '' : $src.DIRECTORY_SEPARATOR,
113 20
                str_replace('\\', DIRECTORY_SEPARATOR, $namespace->getNamespace())
114
            ),
115
        ];
116
    }
117
118 20
    protected function getComposerFileContent(): array
119
    {
120 20
        $content = [];
121 20
        $composerFilePath = $this->getComposerFilePath();
122 20
        if (!empty($composerFilePath)) {
123 20
            $content = json_decode(file_get_contents($composerFilePath), true);
124
        }
125
126 20
        return $content;
127
    }
128
129 20
    protected function setComposerFileContent(array $content): Composer
130
    {
131 20
        $composerFilePath = $this->getComposerFilePath();
132 20
        if (!empty($composerFilePath)) {
133 20
            file_put_contents($composerFilePath, self::encodeToJson($content));
134
        }
135
136 20
        return $this;
137
    }
138
139 20
    protected static function encodeToJson(array $content): string
140
    {
141 20
        return json_encode($content, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
142
    }
143
144 20
    protected function getComposerFilePath(): string
145
    {
146 20
        $path = realpath(sprintf('%s/composer.json', $this->getGenerator()->getOptionDestination()));
147 20
        return false === $path ? '' : $path;
148
    }
149
}
150