Completed
Pull Request — master (#699)
by Sean
09:34 queued 05:29
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 to here 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
		$this->sendApprovedEmail($e->getStateMachine()->getObject());
39
	}
40
41
	public function onReject(TransitionEvent $e) {
42
		$this->sendRejectedEmail($e->getStateMachine()->getObject());
43
	}
44
45
	public function onQueue(TransitionEvent $e) {
46
		/** @var DNDeployment $deployment */
47
		$deployment = $e->getStateMachine()->getObject();
48
49
		$token = $deployment->enqueueDeployment();
50
		$deployment->setResqueToken($token);
51
		$deployment->DeployStarted = SS_Datetime::now()->Rfc2822();
52
		$deployment->write();
53
54
		$deployment->log()->write(sprintf(
55
			'Deploy queued as job %s (sigFile is %s)',
56
			$token,
57
			$deployment->getSigFile()
58
		));
59
	}
60
61
	public function onAbort(TransitionEvent $e) {
62
		$deployment = $e->getStateMachine()->getObject();
63
64
		// 2 is SIGINT - we can't use SIGINT constant in the mod_apache context.
65
		$deployment->setSignal(2);
66
	}
67
68
	protected function canSendEmail(DNDeployment $deployment) {
69
		$deployer = $deployment->Deployer();
70
		$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...
71
		if (!$deployer || !$deployer->exists()) {
72
			return false;
73
		}
74
		if (!$approver || !$approver->exists()) {
75
			return false;
76
		}
77
78
		return true;
79
	}
80
81 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...
82
		if (!$this->canSendEmail($deployment)) {
83
			return false;
84
		}
85
		$deployer = $deployment->Deployer();
86
		$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...
87
88
		$email = Email::create();
89
		$email->setTo(sprintf('%s <%s>', $approver->Name, $approver->Email));
90
		$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...
91
		$email->setSubject(sprintf('%s has submitted a deployment for your approval', $deployer->Name));
92
		$email->setTemplate('DeploymentSubmittedEmail');
93
		$email->populateTemplate($deployment);
94
		$email->send();
95
96
		$deployment->log()->write(sprintf(
97
			'Deployment submitted email sent to approver %s <%s>',
98
			$approver->Name,
99
			$approver->Email
100
		));
101
	}
102
103 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...
104
		if (!$this->canSendEmail($deployment)) {
105
			return false;
106
		}
107
		$deployer = $deployment->Deployer();
108
		$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...
109
110
		$email = Email::create();
111
		$email->setTo(sprintf('%s <%s>', $deployer->Name, $deployer->Email));
112
		$email->setSubject(sprintf('Your deployment has been approved by %s', $approver->Name));
113
		$email->setTemplate('DeploymentApprovedEmail');
114
		$email->populateTemplate($deployment);
115
		$email->send();
116
117
		$deployment->log()->write(sprintf(
118
			'Deployment approved email sent to requester %s <%s>',
119
			$deployer->Name,
120
			$deployer->Email
121
		));
122
	}
123
124 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...
125
		if (!$this->canSendEmail($deployment)) {
126
			return false;
127
		}
128
		$deployer = $deployment->Deployer();
129
		$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...
130
131
		$email = Email::create();
132
		$email->setTo(sprintf('%s <%s>', $deployer->Name, $deployer->Email));
133
		$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...
134
		$email->setSubject(sprintf('Your deployment has been rejected by %s', $approver->Name));
135
		$email->setTemplate('DeploymentRejectedEmail');
136
		$email->populateTemplate($deployment);
137
		$email->send();
138
139
		$deployment->log()->write(sprintf(
140
			'Deployment rejected email sent to requester %s <%s>',
141
			$deployer->Name,
142
			$deployer->Email
143
		));
144
	}
145
146 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...
147
		if (!$this->canSendEmail($deployment)) {
148
			return false;
149
		}
150
		$deployer = $deployment->Deployer();
151
		$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...
152
153
		$to = sprintf('%s <%s>, %s <%s>', $deployer->Name, $deployer->Email, $approver->Name, $approver->Email);
154
155
		$email = Email::create();
156
		$email->setTo($to);
157
		$email->setSubject('Deployment approval has been cancelled');
158
		$email->setTemplate('DeploymentApprovalCancellationEmail');
159
		$email->populateTemplate($deployment);
160
		$email->send();
161
162
		$deployment->log()->write(sprintf('Deployment approval cancellation email sent to %s', $to));
163
	}
164
165
}
166