Completed
Pull Request — master (#681)
by Sean
06:35
created

DNDeploymentHandlers::onReject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
use \Finite\Event\TransitionEvent;
4
5
class DNDeploymentHandlers extends Object {
0 ignored issues
show
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...
6
7
	public function onSubmit(TransitionEvent $e) {
8
		/** @var DNDeployment $deployment */
9
		$deployment = $e->getStateMachine()->getObject();
10
		$deployment->DeployRequested = SS_Datetime::now()->Rfc2822();
11
		$deployment->write();
12
		$this->sendEmailToApprover($deployment);
13
	}
14
15
	public function onApprove(TransitionEvent $e) {
0 ignored issues
show
Unused Code introduced by
The parameter $e is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
16
		// @todo send email to requester that it's approved
17
	}
18
19
	public function onReject(TransitionEvent $e) {
0 ignored issues
show
Unused Code introduced by
The parameter $e is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
20
		// @todo send email to requester that it's rejected
21
	}
22
23
	public function onQueue(TransitionEvent $e) {
24
		/** @var DNDeployment $deployment */
25
		$deployment = $e->getStateMachine()->getObject();
26
27
		$token = $deployment->enqueueDeployment();
28
		$deployment->setResqueToken($token);
29
		$deployment->DeployStarted = SS_Datetime::now()->Rfc2822();
30
		$deployment->write();
31
32
		$log = $deployment->log();
33
		$log->write(sprintf(
34
			'Deploy queued as job %s (sigFile is %s)',
35
			$token,
36
			$deployment->getSigFile()
37
		));
38
	}
39
40
	public function onAbort(TransitionEvent $e) {
41
		$deployment = $e->getStateMachine()->getObject();
42
43
		// 2 is SIGINT - we can't use SIGINT constant in the mod_apache context.
44
		$deployment->setSignal(2);
45
	}
46
47
	protected function sendEmailToApprover(DNDeployment $deployment) {
48
		$deployer = $deployment->Deployer();
49
		$approver = $deployment->Approver();
0 ignored issues
show
Documentation Bug introduced by
The method Approver does not exist on object<DNDeployment>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
50
		if (!$approver || !$approver->exists()) {
51
			return false;
52
		}
53
54
		$email = Email::create();
55
		$email->setTo(sprintf('%s <%s>', $approver->Name, $approver->Email));
56
		$email->replyTo(sprintf('%s <%s>', $deployer->Name, $deployer->Email));
0 ignored issues
show
Deprecated Code introduced by
The method Email::replyTo() has been deprecated with message: 4.0 Use the "setReplyTo" method instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
57
		$email->setSubject('Deployment has been submitted');
58
		$email->setTemplate('DeploymentNotificationSubmitted');
59
		$email->populateTemplate($deployment);
60
		$email->send();
61
62
		$log = $deployment->log();
63
		$log->write(sprintf(
64
			'Deployment submitted email sent to approver %s <%s>',
65
			$approver->Name,
66
			$approver->Email
67
		));
68
	}
69
}
70