Passed
Pull Request — master (#126)
by Dmitriy
08:16 queued 05:28
created

ConfigController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 1
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Api\Controller;
6
7
use Exception;
8
use Psr\Http\Message\ResponseInterface;
9
use Yiisoft\Aliases\Aliases;
10
use Yiisoft\DataResponse\DataResponseFactoryInterface;
11
use Yiisoft\Yii\Debug\Api\Inspector\Command\BashCommand;
12
13
final class ConfigController
14
{
15
    public function __construct(
16
        private DataResponseFactoryInterface $responseFactory,
17
    ) {
18
    }
19
20
    public function read(Aliases $aliases): ResponseInterface
21
    {
22
        $command = new BashCommand($aliases, [
23
            'composer',
24
            'yii-config-merge-plan',
25
        ]);
26
        $output = $command->run()->getResult();
27
        $mergePlanPath = substr($output, 0, strpos($output, 'Xdebug: [Step Debug]') ?: -1);
28
29
        if (!file_exists($mergePlanPath)) {
30
            throw new Exception(
31
                sprintf(
32
                    'Could not find composer.json by the path "%s".',
33
                    $mergePlanPath,
34
                )
35
            );
36
        }
37
38
        $content = require $mergePlanPath;
39
        $rootAlias = $aliases->get('@root');
40
41
        $result = [
42
            'path' => substr($mergePlanPath, strlen($rootAlias) + 1),
43
            'data' => $content,
44
        ];
45
46
        return $this->responseFactory->createResponse($result);
47
    }
48
}
49