Passed
Pull Request — master (#23)
by Ronan
05:57 queued 10s
created

ApiController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 27
c 1
b 0
f 0
dl 0
loc 54
ccs 0
cts 38
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A events() 0 40 5
1
<?php
2
3
namespace App\Controller\Project;
4
5
use App\Controller\Traits\ApiTrait;
6
use App\Controller\Traits\ProjectTrait;
7
use App\Model\Deployment;
8
use App\Model\Event;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface;
11
use Ronanchilvers\Orm\Orm;
12
13
/**
14
 * API Controller supporting the project UI
15
 *
16
 * @author Ronan Chilvers <[email protected]>
17
 */
18
class ApiController
19
{
20
    use ProjectTrait;
21
    use ApiTrait;
22
23
    /**
24
     * Get the event data for a project
25
     *
26
     * @param ServerRequestInterface $request
27
     * @param ResponseInterface $response
28
     * @param array $args
29
     * @return ResponseInterface
30
     * @author Ronan Chilvers <[email protected]>
31
     */
32
    public function events(
33
        ServerRequestInterface $request,
34
        ResponseInterface $response,
35
        array $args
36
    ) {
37
        if (!$project = $this->projectFromArgs($args)) {
38
            return $this->apiError(
39
                $response,
40
                'Invalid project'
41
            );
42
        }
43
        if (!isset($args['number']) || 0 == (int) $args['number']) {
44
            return $this->apiError(
45
                $response,
46
                'Invalid deployment number'
47
            );
48
        }
49
        $number = (int) $args['number'];
50
        $deployment = Orm::finder(Deployment::class)->forProjectIdAndNumber(
51
            $project->id,
52
            $number
53
        );
54
        if (!$deployment instanceof Deployment) {
55
            return $this->apiError(
56
                $response,
57
                'Deployment not found for project'
58
            );
59
        }
60
        $data = [
61
            // 'project' => $project->toArray(),
62
            'deployment' => $deployment->toArray(),
63
        ];
64
        $events = Orm::finder(Event::class)->arrayForDeploymentId(
65
            $deployment->id
66
        );
67
        $data['events'] = $events;
68
69
        return $this->apiResponse(
70
            $response,
71
            $data
72
        );
73
    }
74
}
75