Passed
Pull Request — master (#86)
by Dmitriy
13:43
created

GitController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 38
c 1
b 0
f 0
dl 0
loc 67
ccs 0
cts 44
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A checkout() 0 12 2
A __construct() 0 4 1
A summary() 0 25 1
A getGit() 0 18 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Api\Controller;
6
7
use GitElephant\Objects\Remote;
8
use GitElephant\Repository;
9
use InvalidArgumentException;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Http\Message\ServerRequestInterface;
12
use Throwable;
13
use Yiisoft\Aliases\Aliases;
14
use Yiisoft\DataResponse\DataResponseFactoryInterface;
15
use Yiisoft\VarDumper\VarDumper;
16
17
final class GitController
18
{
19
    public function __construct(
20
        private DataResponseFactoryInterface $responseFactory,
21
        private Aliases $aliases,
22
    ) {
23
    }
24
25
    public function summary(): ResponseInterface
26
    {
27
        $git = $this->getGit();
28
29
        $branch = $git->getMainBranch();
30
        $result = [
31
            'currentBranch' => $branch->getName(),
32
            'remotes' => array_map(fn (Remote $repo) => [
33
                'name' => $repo->getName(),
34
                'branches' => array_keys($repo->getBranches()),
35
                'url' => $repo->getFetchURL(),
36
            ], $git->getRemotes(false)),
37
            'branches' => $git->getBranches(true),
38
            'lastCommit' => [
39
                'ref' => $branch->getLastCommit()->getSha(true),
40
                'message' => $branch->getLastCommit()->getMessage()->getShortMessage(),
41
                'author' => [
42
                    'name' => $branch->getLastCommit()->getAuthor()->getName(),
43
                    'email' => $branch->getLastCommit()->getAuthor()->getEmail(),
44
                ],
45
            ],
46
            'status' => $git->getStatusOutput(),
47
        ];
48
        $response = VarDumper::create($result)->asJson(false, 255);
49
        return $this->responseFactory->createResponse(json_decode($response, null, 512, JSON_THROW_ON_ERROR));
50
    }
51
52
    public function checkout(ServerRequestInterface $request): ResponseInterface
53
    {
54
        $git = $this->getGit();
55
56
        $branch = $request->getParsedBody()['branch'] ?? null;
57
58
        if ($branch === null) {
59
            throw new InvalidArgumentException('Branch should not be empty.');
60
        }
61
62
        $git->checkout($branch);
63
        return $this->responseFactory->createResponse([]);
64
    }
65
66
    private function getGit(): Repository
67
    {
68
        $projectPath = $this->aliases->get('@root');
69
70
        while ($projectPath !== '/') {
71
            try {
72
                $git = new Repository($projectPath);
73
                $git->getStatus();
74
                return $git;
75
            } catch (Throwable) {
76
                $projectPath = dirname($projectPath);
77
            }
78
        }
79
80
        throw new InvalidArgumentException(
81
            sprintf(
82
                'Could find any repositories up from "%s" directory.',
83
                $projectPath,
84
            )
85
        );
86
    }
87
}
88