Completed
Pull Request — master (#731)
by Sean
04:26
created

DeployDispatcher::redeploy()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 2
nop 1
1
<?php
2
3
/**
4
 * This dispatcher takes care of updating and returning information about this
5
 * projects git repository
6
 */
7
class DeployDispatcher extends Dispatcher {
0 ignored issues
show
Coding Style introduced by
The property $allowed_actions is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $action_types is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
9
	const ACTION_DEPLOY = 'deploys';
10
11
	/**
12
	 * @var array
13
	 */
14
	private static $allowed_actions = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
15
		'history',
16
		'upcoming',
17
		'currentbuild',
18
		'show',
19
		'delete',
20
		'log',
21
		'redeploy',
22
		'createdeployment',
23
		'start'
24
	];
25
26
	private static $dependencies = [
27
		'formatter' => '%$DeploynautAPIFormatter'
28
	];
29
30
	/**
31
	 * @var \DNProject
32
	 */
33
	protected $project = null;
34
35
	/**
36
	 * @var \DNEnvironment
37
	 */
38
	protected $environment = null;
39
40
	/**
41
	 * @var array
42
	 */
43
	private static $action_types = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $action_types is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
44
		self::ACTION_DEPLOY
45
	];
46
47 View Code Duplication
	public function init() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
		parent::init();
49
50
		$this->project = $this->getCurrentProject();
51
52
		if (!$this->project) {
53
			return $this->project404Response();
54
		}
55
56
		// Performs canView permission check by limiting visible projects
57
		$this->environment = $this->getCurrentEnvironment($this->project);
58
		if (!$this->environment) {
59
			return $this->environment404Response();
60
		}
61
	}
62
63
	/**
64
	 * @param \SS_HTTPRequest $request
65
	 * @return \HTMLText|\SS_HTTPResponse
66
	 */
67
	public function index(\SS_HTTPRequest $request) {
68
		return $this->redirect(\Controller::join_links($this->Link(), 'history'), 302);
69
	}
70
71
	/**
72
	 * @param \SS_HTTPRequest $request
73
	 * @return \SS_HTTPResponse
74
	 */
75 View Code Duplication
	public function history(\SS_HTTPRequest $request) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
		$data = [];
77
78
		$list = $this->environment->DeployHistory('DeployStarted');
79
80
		foreach ($list as $deployment) {
81
			$data[] = $this->formatter->getDeploymentData($deployment);
0 ignored issues
show
Documentation introduced by
The property formatter does not exist on object<DeployDispatcher>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
82
		}
83
84
		return $this->getAPIResponse([
85
			'list' => $data,
86
		], 200);
87
	}
88
89
	/**
90
	 * @param \SS_HTTPRequest $request
91
	 * @return \SS_HTTPResponse
92
	 */
93 View Code Duplication
	public function upcoming(\SS_HTTPRequest $request) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
		$data = [];
95
		$list = $this->environment->UpcomingDeployments();
96
		foreach ($list as $deployment) {
97
			$data[] = $this->formatter->getDeploymentData($deployment);
0 ignored issues
show
Documentation introduced by
The property formatter does not exist on object<DeployDispatcher>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
98
		}
99
		return $this->getAPIResponse([
100
			'list' => $data,
101
		], 200);
102
	}
103
104
	/**
105
	 * @param \SS_HTTPRequest $request
106
	 * @return \SS_HTTPResponse
107
	 */
108
	public function currentbuild(\SS_HTTPRequest $request) {
109
		$currentBuild = $this->environment->CurrentBuild();
110
		if (!$currentBuild) {
111
			return $this->getAPIResponse(['deployment' => []], 200);
112
		}
113
		return $this->getAPIResponse(['deployment' => $this->formatter->getDeploymentData($currentBuild)], 200);
0 ignored issues
show
Documentation introduced by
The property formatter does not exist on object<DeployDispatcher>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
114
	}
115
116
	/**
117
	 * @param \SS_HTTPRequest $request
118
	 * @return \SS_HTTPResponse
119
	 */
120
	public function show(\SS_HTTPRequest $request) {
121
		$deployment = DNDeployment::get()->byId($request->param('ID'));
122
		$errorResponse = $this->validateDeployment($deployment);
0 ignored issues
show
Documentation introduced by
$deployment is of type object<DataObject>|null, but the function expects a object<DNDeployment>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
123
		if ($errorResponse instanceof \SS_HTTPResponse) {
124
			return $errorResponse;
125
		}
126
		return $this->getAPIResponse(['deployment' => $this->formatter->getDeploymentData($deployment)], 200);
0 ignored issues
show
Documentation introduced by
The property formatter does not exist on object<DeployDispatcher>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
127
	}
128
129
	public function delete(\SS_HTTPRequest $request) {
130
		if ($request->httpMethod() !== 'POST') {
131
			return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405);
132
		}
133
134
		$this->checkSecurityToken();
135
136
		$deployment = DNDeployment::get()->byId($request->postVar('id'));
137
		$errorResponse = $this->validateDeployment($deployment);
0 ignored issues
show
Documentation introduced by
$deployment is of type object<DataObject>|null, but the function expects a object<DNDeployment>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
138
		if ($errorResponse instanceof \SS_HTTPResponse) {
139
			return $errorResponse;
140
		}
141
142
		$id = $deployment->ID;
143
		$deployment->delete();
144
145
		return $this->getAPIResponse([
146
			'id' => $id,
147
			'message' => 'Deployment has been deleted'
148
		], 201);
149
	}
150
151
	/**
152
	 * @param \SS_HTTPRequest $request
153
	 * @return \SS_HTTPResponse
154
	 */
155
	public function log(\SS_HTTPRequest $request) {
156
		$deployment = DNDeployment::get()->byId($request->param('ID'));
157
		$errorResponse = $this->validateDeployment($deployment);
0 ignored issues
show
Documentation introduced by
$deployment is of type object<DataObject>|null, but the function expects a object<DNDeployment>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
158
		if ($errorResponse instanceof \SS_HTTPResponse) {
159
			return $errorResponse;
160
		}
161
		$log = $deployment->log();
162
		$content = $log->exists() ? $log->content() : 'Waiting for action to start';
163
		$lines = explode(PHP_EOL, $content);
164
165
		return $this->getAPIResponse([
166
			'message' => $lines,
167
			'status' => $deployment->Status,
168
			'deployment' => $this->formatter->getDeploymentData($deployment),
0 ignored issues
show
Documentation introduced by
The property formatter does not exist on object<DeployDispatcher>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
169
		], 200);
170
	}
171
172
	/**
173
	 * @param \SS_HTTPRequest $request
174
	 * @return \SS_HTTPResponse
175
	 */
176
	public function redeploy(\SS_HTTPRequest $request) {
177
		$currentBuild = $this->environment->CurrentBuild();
178
		if (!$currentBuild || !$currentBuild->exists()) {
179
			return $this->redirect(Controller::join_links($this->environment->Link('overview'), 'deployment', 'new'));
180
		}
181
182
		$strategy = $this->environment->Backend()->planDeploy($this->environment, [
183
			'sha' => $currentBuild->SHA,
184
			'ref_type' => GitDispatcher::REF_TYPE_PREVIOUS,
185
			'branch' => $currentBuild->Branch
0 ignored issues
show
Documentation introduced by
The property Branch does not exist on object<DNDeployment>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
186
		]);
187
		$deployment = $strategy->createDeployment();
188
189
		return $this->redirect($deployment->Link());
190
	}
191
192
	/**
193
	 * Create deployment. Can't use {@link create()} as it's taken by Object.
194
	 *
195
	 * @param \SS_HTTPRequest $request
196
	 * @return \SS_HTTPResponse
197
	 */
198
	public function createdeployment(\SS_HTTPRequest $request) {
199
		if ($request->httpMethod() !== 'POST') {
200
			return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405);
201
		}
202
203
		$this->checkSecurityToken();
204
205
		// @todo the strategy should have been saved when there has been a request for an
206
		// approval or a bypass. This saved state needs to be checked if it's invalidated
207
		// if another deploy happens before this one
208
		$isBranchDeploy = (int) $request->postVar('ref_type') === GitDispatcher::REF_TYPE_BRANCH;
209
210
		$options = [
211
			'sha' => $request->postVar('ref'),
212
			'ref_type' => $request->postVar('ref_type'),
213
			'branch' => $isBranchDeploy ? $request->postVar('ref_name') : null,
214
			'title' => $request->postVar('title'),
215
			'summary' => $request->postVar('summary')
216
		];
217
218 View Code Duplication
		if ($request->postVar('options')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
219
			foreach (explode(',', $request->postVar('options')) as $option) {
220
				$options[$option] = true;
221
			}
222
		}
223
224
		$strategy = $this->environment->Backend()->planDeploy($this->environment, $options);
225
226
		$approver = Member::get()->byId($request->postVar('approver_id'));
227
		if ($approver && $approver->exists()) {
228
			if (!$this->project->allowed(ApprovalsDispatcher::ALLOW_APPROVAL, $approver)) {
0 ignored issues
show
Documentation introduced by
$approver is of type object<DataObject>, but the function expects a object<Member>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
229
				return $this->getAPIResponse(['message' => 'The given approver does not have permissions to approve'], 403);
230
			}
231
		}
232
233
		$deployment = $strategy->createDeployment();
234
		if ($approver && $approver->exists()) {
235
			$deployment->ApproverID = $approver->ID;
0 ignored issues
show
Documentation introduced by
The property ApproverID does not exist on object<DNDeployment>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
236
			$deployment->write();
237
		}
238
239
		return $this->getAPIResponse([
240
			'message' => 'Deployment has been created',
241
			'deployment' => $this->formatter->getDeploymentData($deployment),
0 ignored issues
show
Documentation introduced by
The property formatter does not exist on object<DeployDispatcher>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
242
		], 201);
243
	}
244
245
	/**
246
	 * @param \SS_HTTPRequest $request
247
	 * @return \SS_HTTPResponse
248
	 */
249
	public function start(\SS_HTTPRequest $request) {
250
		if ($request->httpMethod() !== 'POST') {
251
			return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405);
252
		}
253
254
		$this->checkSecurityToken();
255
256
		$deployment = DNDeployment::get()->byId($request->postVar('id'));
257
		$errorResponse = $this->validateDeployment($deployment);
0 ignored issues
show
Documentation introduced by
$deployment is of type object<DataObject>|null, but the function expects a object<DNDeployment>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
258
		if ($errorResponse instanceof \SS_HTTPResponse) {
259
			return $errorResponse;
260
		}
261
262
		// The deployment cannot be started until it has been approved, or bypassed straight to approved state
263
		if ($deployment->State != DNDeployment::STATE_APPROVED) {
264
			return $this->getAPIResponse(['message' => 'This deployment has not been approved. Cannot deploy'], 403);
265
		}
266
267
		// until we have a system that can invalidate currently scheduled deployments due
268
		// to emergency deploys etc, replan the deployment to check if it's still valid.
269
		$options = $deployment->getDeploymentStrategy()->getOptions();
270
		$strategy = $this->environment->Backend()->planDeploy($this->environment, $options);
271
		$deployment->Strategy = $strategy->toJSON();
272
		$deployment->write();
273
274
		$deployment->getMachine()->apply(DNDeployment::TR_QUEUE);
275
276
		$location = \Controller::join_links(Director::absoluteBaseURL(), $this->Link('log'), $deployment->ID);
277
278
		$response = $this->getAPIResponse([
279
			'message' => 'Deployment has been queued',
280
			'location' => $location,
281
			'deployment' => $this->formatter->getDeploymentData($deployment),
0 ignored issues
show
Documentation introduced by
The property formatter does not exist on object<DeployDispatcher>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
282
		], 201);
283
284
		$response->addHeader('Location', $location);
285
286
		return $response;
287
	}
288
289
	/**
290
	 * @param string $action
291
	 * @return string
292
	 */
293
	public function Link($action = '') {
294
		return \Controller::join_links($this->environment->Link(), self::ACTION_DEPLOY, $action);
295
	}
296
297
	/**
298
	 * @param string $name
299
	 * @return array
300
	 */
301
	public function getModel($name = '') {
302
		return [];
303
	}
304
305
	/**
306
	 * Check if a DNDeployment exists and do permission checks on it. If there is something wrong it will return
307
	 * an APIResponse with the error, otherwise null.
308
	 *
309
	 * @param \DNDeployment $deployment
310
	 *
311
	 * @return null|SS_HTTPResponse
312
	 */
313 View Code Duplication
	protected function validateDeployment($deployment) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
314
		if (!$deployment || !$deployment->exists()) {
315
			return $this->getAPIResponse(['message' => 'This deployment does not exist'], 404);
316
		}
317
		if ($deployment->EnvironmentID != $this->environment->ID) {
318
			return $this->getAPIResponse(['message' => 'This deployment does not belong to the environment'], 403);
319
		}
320
		if (!$deployment->canView()) {
321
			return $this->getAPIResponse(['message' => 'You are not authorised to view this deployment'], 403);
322
		}
323
		return null;
324
	}
325
326
}
327