Completed
Pull Request — master (#699)
by Sean
26:18 queued 20:48
created

sendApprovalCancellationEmail()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 13

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
dl 18
loc 18
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 13
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 onAfterTransition(TransitionEvent $e) {
8
		/** @var DNDeployment $deployment */
9
		$deployment = $e->getStateMachine()->getObject();
10
		$deployment->log()->write(sprintf(
11
			'State transitioned from "%s" to "%s"',
12
			$e->getInitialState()->getName(),
13
			$e->getTransition()->getState()
14
		));
15
	}
16
17
	public function onNew(TransitionEvent $e) {
18
		/** @var DNDeployment $deployment */
19
		$deployment = $e->getStateMachine()->getObject();
20
		$deployment->DeployRequested = null;
21
		$deployment->write();
22
23
		// If we transitioned from Submitted state, then send a cancellation email
24
		if ($e->getInitialState()->getName() === DNDeployment::STATE_SUBMITTED) {
25
			$this->sendApprovalCancellationEmail($deployment);
26
		}
27
	}
28
29
	public function onSubmit(TransitionEvent $e) {
30
		/** @var DNDeployment $deployment */
31
		$deployment = $e->getStateMachine()->getObject();
32
		$deployment->DeployRequested = SS_Datetime::now()->Rfc2822();
33
		$deployment->write();
34
		$this->sendSubmittedEmail($deployment);
35
	}
36
37
	public function onApprove(TransitionEvent $e) {
38
		// If we transitioned from Submitted state, then send an approved email.
39
		// This is especially important as bypassing goes from New to Approved, and we
40
		// don't want to be sending emails about approved when it was bypassed.
41
		if ($e->getInitialState()->getName() === DNDeployment::STATE_SUBMITTED) {
42
			$this->sendApprovedEmail($e->getStateMachine()->getObject());
43
		}
44
	}
45
46
	public function onReject(TransitionEvent $e) {
47
		// If we transitioned from Submitted state, then send an rejected email.
48
		if ($e->getInitialState()->getName() === DNDeployment::STATE_SUBMITTED) {
49
			$this->sendRejectedEmail($e->getStateMachine()->getObject());
50
		}
51
	}
52
53
	public function onQueue(TransitionEvent $e) {
54
		/** @var DNDeployment $deployment */
55
		$deployment = $e->getStateMachine()->getObject();
56
57
		$token = $deployment->enqueueDeployment();
58
		$deployment->setResqueToken($token);
59
		$deployment->DeployStarted = SS_Datetime::now()->Rfc2822();
60
		$deployment->write();
61
62
		$deployment->log()->write(sprintf(
63
			'Deploy queued as job %s (sigFile is %s)',
64
			$token,
65
			$deployment->getSigFile()
66
		));
67
	}
68
69
	public function onAbort(TransitionEvent $e) {
70
		$deployment = $e->getStateMachine()->getObject();
71
72
		// 2 is SIGINT - we can't use SIGINT constant in the mod_apache context.
73
		$deployment->setSignal(2);
74
	}
75
76
	protected function canSendEmail(DNDeployment $deployment) {
77
		$deployer = $deployment->Deployer();
78
		$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...
79
		if (!$deployer || !$deployer->exists()) {
80
			return false;
81
		}
82
		if (!$approver || !$approver->exists()) {
83
			return false;
84
		}
85
86
		return true;
87
	}
88
89 View Code Duplication
	protected function sendSubmittedEmail(DNDeployment $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...
90
		if (!$this->canSendEmail($deployment)) {
91
			return false;
92
		}
93
		$deployer = $deployment->Deployer();
94
		$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...
95
96
		$email = Email::create();
97
		$email->setTo(sprintf('%s <%s>', $approver->Name, $approver->Email));
98
		$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...
99
		$email->setSubject(sprintf('%s has submitted a deployment for your approval', $deployer->Name));
100
		$email->setTemplate('DeploymentSubmittedEmail');
101
		$email->populateTemplate($deployment);
102
		$email->send();
103
104
		$deployment->log()->write(sprintf(
105
			'Deployment submitted email sent to approver %s <%s>',
106
			$approver->Name,
107
			$approver->Email
108
		));
109
	}
110
111 View Code Duplication
	protected function sendApprovedEmail(DNDeployment $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...
112
		if (!$this->canSendEmail($deployment)) {
113
			return false;
114
		}
115
		$deployer = $deployment->Deployer();
116
		$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...
117
118
		$email = Email::create();
119
		$email->setTo(sprintf('%s <%s>', $deployer->Name, $deployer->Email));
120
		$email->setSubject(sprintf('Your deployment has been approved by %s', $approver->Name));
121
		$email->setTemplate('DeploymentApprovedEmail');
122
		$email->populateTemplate($deployment);
123
		$email->send();
124
125
		$deployment->log()->write(sprintf(
126
			'Deployment approved email sent to requester %s <%s>',
127
			$deployer->Name,
128
			$deployer->Email
129
		));
130
	}
131
132 View Code Duplication
	protected function sendRejectedEmail(DNDeployment $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...
133
		if (!$this->canSendEmail($deployment)) {
134
			return false;
135
		}
136
		$deployer = $deployment->Deployer();
137
		$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...
138
139
		$email = Email::create();
140
		$email->setTo(sprintf('%s <%s>', $deployer->Name, $deployer->Email));
141
		$email->replyTo(sprintf('%s <%s>', $approver->Name, $approver->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...
142
		$email->setSubject(sprintf('Your deployment has been rejected by %s', $approver->Name));
143
		$email->setTemplate('DeploymentRejectedEmail');
144
		$email->populateTemplate($deployment);
145
		$email->send();
146
147
		$deployment->log()->write(sprintf(
148
			'Deployment rejected email sent to requester %s <%s>',
149
			$deployer->Name,
150
			$deployer->Email
151
		));
152
	}
153
154 View Code Duplication
	protected function sendApprovalCancellationEmail(DNDeployment $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...
155
		if (!$this->canSendEmail($deployment)) {
156
			return false;
157
		}
158
		$deployer = $deployment->Deployer();
159
		$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...
160
161
		$to = sprintf('%s <%s>, %s <%s>', $deployer->Name, $deployer->Email, $approver->Name, $approver->Email);
162
163
		$email = Email::create();
164
		$email->setTo($to);
165
		$email->setSubject('Deployment approval has been cancelled');
166
		$email->setTemplate('DeploymentApprovalCancellationEmail');
167
		$email->populateTemplate($deployment);
168
		$email->send();
169
170
		$deployment->log()->write(sprintf('Deployment approval cancellation email sent to %s', $to));
171
	}
172
173
}
174