GitlabController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace CompoLab\Application\Http\Controller;
4
5
use CompoLab\Application\GitlabRepositoryManager;
6
use Gitlab\Client as Gitlab;
7
use Gitlab\Model\Project;
8
use Symfony\Component\HttpFoundation\JsonResponse;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
12
13
final class GitlabController
14
{
15
    /** @var Gitlab */
16
    private $gitlab;
17
18
    /** @var GitlabRepositoryManager */
19
    private $repositoryManager;
20
21
    public function __construct(Gitlab $gitlab, GitlabRepositoryManager $repositoryManager)
22
    {
23
        $this->gitlab = $gitlab;
24
        $this->repositoryManager = $repositoryManager;
25
    }
26
27
    public function handle(Request $request): Response
28
    {
29
        $event = json_decode($request->getContent(), true);
30
31
        if (!isset($event['project_id'])) {
32
            throw new BadRequestHttpException('Missing project_id from body');
33
        }
34
35
        if (!isset($event['event_name'])) {
36
            throw new BadRequestHttpException('Missing event_name from body');
37
        }
38
39
        if (!isset($event['project_id']) or !$project = Project::fromArray(
40
            $this->gitlab,
41
            $this->gitlab->projects()->show($event['project_id']))
0 ignored issues
show
Bug introduced by
It seems like $this->gitlab->projects(...w($event['project_id']) can also be of type string; however, parameter $data of Gitlab\Model\Project::fromArray() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
            /** @scrutinizer ignore-type */ $this->gitlab->projects()->show($event['project_id']))
Loading history...
42
        ){
43
            throw new BadRequestHttpException('Impossible te retrieve a Gitlab project from the request');
44
        }
45
46
        switch ($event['event_name']) {
47
            case 'project_destroy':
48
                $this->repositoryManager->deleteProject($project);
49
                break;
50
51
            case 'push':
52
            case 'tag_push':
53
                $this->repositoryManager->registerProject($project);
54
                break;
55
56
            default: return new JsonResponse([
57
                'status' => 200,
58
                'message' => 'CompoLab has NOT handled the Gitlab event',
59
                'project_id' => $event['project_id'],
60
                'event_name' => $event['event_name'],
61
            ]);
62
        }
63
64
        $this->repositoryManager->save();
65
66
        return new JsonResponse([
67
            'status' => 200,
68
            'message' => 'CompoLab has successfully handled the Gitlab event',
69
            'project_id' => $event['project_id'],
70
            'event_name' => $event['event_name'],
71
        ]);
72
    }
73
}
74