Completed
Pull Request — master (#697)
by Sean
04:45
created

DNDeploymentHandlers::onAnyTransition()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 12
nc 2
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 onAnyTransition(TransitionEvent $e) {
8
		$object = $e->getStateMachine()->getObject();
9
		if (!$object->hasMethod('log')) {
10
			// Not something we are able to log the transition for. Skip!
11
			return;
12
		}
13
14
		$log = $object->log();
15
		$log->write(sprintf(
16
			'State transitioned from "%s" to "%s" on %s (ID: %s, Title: %s)',
17
			$e->getInitialState()->getName(),
18
			$e->getTransition()->getState(),
19
			get_class($object),
20
			$object->ID,
21
			$object->Title
22
		));
23
	}
24
25
	public function onNew(TransitionEvent $e) {
26
		/** @var DNDeployment $deployment */
27
		$deployment = $e->getStateMachine()->getObject();
28
		$deployment->DeployRequested = null;
29
		$deployment->write();
30
	}
31
32
	public function onSubmit(TransitionEvent $e) {
33
		/** @var DNDeployment $deployment */
34
		$deployment = $e->getStateMachine()->getObject();
35
		$deployment->DeployRequested = SS_Datetime::now()->Rfc2822();
36
		$deployment->write();
37
		$this->sendEmailToApprover($deployment);
38
	}
39
40
	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...
41
		// @todo send email to requester that it's approved
42
	}
43
44
	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...
45
		// @todo send email to requester that it's rejected
46
	}
47
48
	public function onQueue(TransitionEvent $e) {
49
		/** @var DNDeployment $deployment */
50
		$deployment = $e->getStateMachine()->getObject();
51
52
		$token = $deployment->enqueueDeployment();
53
		$deployment->setResqueToken($token);
54
		$deployment->DeployStarted = SS_Datetime::now()->Rfc2822();
55
		$deployment->write();
56
57
		$log = $deployment->log();
58
		$log->write(sprintf(
59
			'Deploy queued as job %s (sigFile is %s)',
60
			$token,
61
			$deployment->getSigFile()
62
		));
63
	}
64
65
	public function onAbort(TransitionEvent $e) {
66
		$deployment = $e->getStateMachine()->getObject();
67
68
		// 2 is SIGINT - we can't use SIGINT constant in the mod_apache context.
69
		$deployment->setSignal(2);
70
	}
71
72
	protected function sendEmailToApprover(DNDeployment $deployment) {
73
		$deployer = $deployment->Deployer();
74
		$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...
75
		if (!$approver || !$approver->exists()) {
76
			return false;
77
		}
78
79
		$email = Email::create();
80
		$email->setTo(sprintf('%s <%s>', $approver->Name, $approver->Email));
81
		$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...
82
		$email->setSubject('Deployment has been submitted');
83
		$email->setTemplate('DeploymentNotificationSubmitted');
84
		$email->populateTemplate($deployment);
85
		$email->send();
86
87
		$log = $deployment->log();
88
		$log->write(sprintf(
89
			'Deployment submitted email sent to approver %s <%s>',
90
			$approver->Name,
91
			$approver->Email
92
		));
93
	}
94
}
95