Passed
Pull Request — master (#128)
by Dmitriy
04:21 queued 01:20
created

ConfigController::read()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
c 2
b 0
f 0
dl 0
loc 27
ccs 0
cts 21
cp 0
rs 9.7333
cc 3
nc 2
nop 1
crap 12
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