Passed
Pull Request — master (#29)
by Ronan
03:34
created

ApiController::events()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 40
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 40
ccs 0
cts 38
cp 0
rs 9.2248
cc 5
nc 4
nop 3
crap 30
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 App\Model\Project;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Http\Message\ServerRequestInterface;
12
use Ronanchilvers\Orm\Orm;
13
14
/**
15
 * API Controller supporting the project UI
16
 *
17
 * @author Ronan Chilvers <[email protected]>
18
 */
19
class ApiController
20
{
21
    use ProjectTrait;
22
    use ApiTrait;
23
24
    /**
25
     * Get the event data for a project
26
     *
27
     * @param ServerRequestInterface $request
28
     * @param ResponseInterface $response
29
     * @param array $args
30
     * @return ResponseInterface
31
     * @author Ronan Chilvers <[email protected]>
32
     */
33
    public function events(
34
        ServerRequestInterface $request,
35
        ResponseInterface $response,
36
        array $args
37
    ) {
38
        if (!$project = $this->projectFromArgs($args)) {
39
            return $this->apiError(
40
                $response,
41
                'Invalid project'
42
            );
43
        }
44
        if (!isset($args['number']) || 0 == (int) $args['number']) {
45
            return $this->apiError(
46
                $response,
47
                'Invalid deployment number'
48
            );
49
        }
50
        $number = (int) $args['number'];
51
        $deployment = Orm::finder(Deployment::class)->forProjectIdAndNumber(
52
            $project->id,
53
            $number
54
        );
55
        if (!$deployment instanceof Deployment) {
56
            return $this->apiError(
57
                $response,
58
                'Deployment not found for project'
59
            );
60
        }
61
        $data = [
62
            // 'project' => $project->toArray(),
63
            'deployment' => $deployment->toArray(),
64
        ];
65
        $events = Orm::finder(Event::class)->arrayForDeploymentId(
66
            $deployment->id
67
        );
68
        $data['events'] = $events;
69
70
        return $this->apiResponse(
71
            $response,
72
            $data
73
        );
74
    }
75
76
    /**
77
     * Trigger a build of a project specified by the project token
78
     *
79
     * @author Ronan Chilvers <[email protected]>
80
     */
81
    public function build(
82
        ServerRequestInterface $request,
83
        ResponseInterface $response,
84
        $args
85
    ) {
86
        try {
87
            $project = Orm::finder(Project::class)->forToken($args['token']);
88
            if (!$project instanceof Project) {
89
                throw new RuntimeException(
0 ignored issues
show
Bug introduced by
The type App\Controller\Project\RuntimeException was not found. Did you mean RuntimeException? If so, make sure to prefix the type with \.
Loading history...
90
                    'Invalid or unknown project token',
91
                    400
92
                );
93
            }
94
            if (!$project->isDeployable()) {
95
                throw new RuntimeException(
96
                    'Project is not deployable at the moment',
97
                    400
98
                );
99
            }
100
            $provider = Provider::forProject(
0 ignored issues
show
Bug introduced by
The type App\Controller\Project\Provider was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
101
                $project
102
            );
103
            $finder = Orm::finder(Event::class);
104
            Orm::transaction(function() use ($project, $provider, $finder) {
105
                try {
106
                    $deployment = Orm::finder(Deployment::class)->nextForProject(
107
                        $project
108
                    );
109
                    $deployment->source = Security::email();
0 ignored issues
show
Bug introduced by
The type App\Controller\Project\Security was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
110
                    if (!$deployment->save()) {
111
                        Log::debug('Unable to create new deployment object', [
0 ignored issues
show
Bug introduced by
The type App\Controller\Project\Log was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
112
                            'project' => $project->toArray(),
113
                        ]);
114
                        throw new RuntimeException('Unable to create new deployment');
115
                    }
116
                    $finder->event(
117
                        'info',
118
                        $deployment,
119
                        'Initialise',
120
                        sprintf("Querying %s for head commit data", $provider->getLabel())
121
                    );
122
                    $head = $provider->getHeadInfo($project->repository, $type, $branch);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $branch seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $type seems to be never defined.
Loading history...
123
                    $finder->event(
124
                        'info',
125
                        $deployment,
126
                        'Initialise',
127
                        "Commit data : " . json_encode($head, JSON_PRETTY_PRINT)
128
                    );
129
                    Log::debug('Updating deployment commit information', $head);
130
                    $deployment->sha       = $head['sha'];
131
                    $deployment->author    = $head['author'];
132
                    $deployment->committer = $head['committer'];
133
                    $deployment->message   = $head['message'];
134
                    if (!$deployment->save()) {
135
                        throw new RuntimeException(
136
                            'Unable to create new deployment',
137
                            500
138
                        );
139
                    }
140
                    if (!$project->markDeploying()) {
141
                        throw new RuntimeException(
142
                            'Unable to mark project as deploying',
143
                            500
144
                        );
145
                    }
146
                    Queue::dispatch(
0 ignored issues
show
Bug introduced by
The type App\Controller\Project\Queue was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
147
                        new DeployJob($deployment)
0 ignored issues
show
Bug introduced by
The type App\Controller\Project\DeployJob was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
148
                    );
149
                } catch (Exception $ex) {
0 ignored issues
show
Bug introduced by
The type App\Controller\Project\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
150
                    if (isset($deployment) && $deployment instanceof Deployment) {
151
                        $finder->event(
152
                            'error',
153
                            $deployment,
154
                            'Initialise',
155
                            $ex->getMessage()
156
                        );
157
                    }
158
                    throw $ex;
159
                }
160
            });
161
162
            Session::flash([
0 ignored issues
show
Bug introduced by
The type App\Controller\Project\Session was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
163
                'heading' => 'Deploy queued successfully'
164
            ]);
165
        } catch (Exception $ex) {
166
            $message = [$ex->getMessage()];
167
            if ($previous = $ex->getPrevious()) {
168
                $message[] = $previous->getMessage();
169
            }
170
            $message = implode(' - ', $message);
171
            Session::flash(
172
                [
173
                    'heading' => 'Failed to initialise new deployment',
174
                    'content' => get_class($ex) . ' : ' . $message,
175
                ],
176
                'error'
177
            );
178
            Log::error('Failed to initialise new deployment', [
179
                'exception' => $ex,
180
            ]);
181
        }
182
183
        return $response->withRedirect(
0 ignored issues
show
Bug introduced by
The method withRedirect() does not exist on Psr\Http\Message\ResponseInterface. It seems like you code against a sub-type of Psr\Http\Message\ResponseInterface such as Slim\Http\Response. ( Ignorable by Annotation )

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

183
        return $response->/** @scrutinizer ignore-call */ withRedirect(
Loading history...
184
            Router::pathFor('project.view', [
0 ignored issues
show
Bug introduced by
The type App\Controller\Project\Router was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
185
                'key' => $project->key
186
            ])
187
        );
188
189
190
191
    }
192
}
193