Passed
Push — feature/create-console-bootstr... ( abaeda )
by Chema
04:15
created

ConsoleConfig::getComposerJsonContentAsArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
ccs 0
cts 6
cp 0
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Console;
6
7
use Gacela\Framework\AbstractConfig;
8
use JsonException;
9
use LogicException;
10
11
final class ConsoleConfig extends AbstractConfig
12
{
13
    public function getFacadeMakerTemplate(): string
14
    {
15
        return $this->getCommandTemplateContent('facade-maker.txt');
16
    }
17
18
    public function getFactoryMakerTemplate(): string
19
    {
20
        return $this->getCommandTemplateContent('factory-maker.txt');
21
    }
22
23
    public function getConfigMakerTemplate(): string
24
    {
25
        return $this->getCommandTemplateContent('config-maker.txt');
26
    }
27
28
    public function getDependencyProviderMakerTemplate(): string
29
    {
30
        return $this->getCommandTemplateContent('dependency-provider-maker.txt');
31
    }
32
33
    /**
34
     * @throws JsonException
35
     *
36
     * @return array{autoload: array{psr-4: array<string,string>}}
37
     */
38
    public function getComposerJsonContentAsArray(): array
39
    {
40
        $filename = $this->getAppRootDir() . '/composer.json';
41
        if (!file_exists($filename)) {
42
            throw new LogicException('composer.json file not found but it is required');
43
        }
44
45
        /** @var array{autoload: array{psr-4: array<string,string>}} $json */
46
        $json = (array)json_decode((string)file_get_contents($filename), true, 512, JSON_THROW_ON_ERROR);
47
48
        return $json;
49
    }
50
51
    private function getCommandTemplateContent(string $filename): string
52
    {
53
        return (string)file_get_contents(__DIR__ . '/Infrastructure/Template/Command/' . $filename);
54
    }
55
}
56