Completed
Pull Request — master (#698)
by Sean
04:07
created

DNDeploymentHandlers::canSendEmail()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 3
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
24
	public function onSubmit(TransitionEvent $e) {
25
		/** @var DNDeployment $deployment */
26
		$deployment = $e->getStateMachine()->getObject();
27
		$deployment->DeployRequested = SS_Datetime::now()->Rfc2822();
28
		$deployment->write();
29
		$this->sendSubmittedEmail($deployment);
30
	}
31
32
	public function onApprove(TransitionEvent $e) {
33
		$this->sendApprovedEmail($e->getStateMachine()->getObject());
34
	}
35
36
	public function onReject(TransitionEvent $e) {
37
		$this->sendRejectedEmail($e->getStateMachine()->getObject());
38
	}
39
40
	public function onQueue(TransitionEvent $e) {
41
		/** @var DNDeployment $deployment */
42
		$deployment = $e->getStateMachine()->getObject();
43
44
		$token = $deployment->enqueueDeployment();
45
		$deployment->setResqueToken($token);
46
		$deployment->DeployStarted = SS_Datetime::now()->Rfc2822();
47
		$deployment->write();
48
49
		$deployment->log()->write(sprintf(
50
			'Deploy queued as job %s (sigFile is %s)',
51
			$token,
52
			$deployment->getSigFile()
53
		));
54
	}
55
56
	public function onAbort(TransitionEvent $e) {
57
		$deployment = $e->getStateMachine()->getObject();
58
59
		// 2 is SIGINT - we can't use SIGINT constant in the mod_apache context.
60
		$deployment->setSignal(2);
61
	}
62
63
	protected function canSendEmail(DNDeployment $deployment) {
64
		$deployer = $deployment->Deployer();
65
		$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...
66
		if (!$deployer || !$deployer->exists()) {
67
			return false;
68
		}
69
		if (!$approver || !$approver->exists()) {
70
			return false;
71
		}
72
73
		return true;
74
	}
75
76 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...
77
		if (!$this->canSendEmail($deployment)) {
78
			return false;
79
		}
80
		$deployer = $deployment->Deployer();
81
		$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...
82
83
		$email = Email::create();
84
		$email->setTo(sprintf('%s <%s>', $approver->Name, $approver->Email));
85
		$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...
86
		$email->setSubject('Deployment has been submitted');
87
		$email->setTemplate('DeploymentSubmittedEmail');
88
		$email->populateTemplate($deployment);
89
		$email->send();
90
91
		$deployment->log()->write(sprintf(
92
			'Deployment submitted email sent to approver %s <%s>',
93
			$approver->Name,
94
			$approver->Email
95
		));
96
	}
97
98
	protected function sendApprovedEmail(DNDeployment $deployment) {
99
		if (!$this->canSendEmail($deployment)) {
100
			return false;
101
		}
102
		$deployer = $deployment->Deployer();
103
104
		$email = Email::create();
105
		$email->setTo(sprintf('%s <%s>', $deployer->Name, $deployer->Email));
106
		$email->setSubject('Deployment has been approved');
107
		$email->setTemplate('DeploymentApprovedEmail');
108
		$email->populateTemplate($deployment);
109
		$email->send();
110
111
		$deployment->log()->write(sprintf(
112
			'Deployment approved email sent to requester %s <%s>',
113
			$deployer->Name,
114
			$deployer->Email
115
		));
116
	}
117
118 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...
119
		if (!$this->canSendEmail($deployment)) {
120
			return false;
121
		}
122
		$deployer = $deployment->Deployer();
123
		$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...
124
125
		$email = Email::create();
126
		$email->setTo(sprintf('%s <%s>', $deployer->Name, $deployer->Email));
127
		$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...
128
		$email->setSubject('Deployment has been rejected');
129
		$email->setTemplate('DeploymentRejectedEmail');
130
		$email->populateTemplate($deployment);
131
		$email->send();
132
133
		$deployment->log()->write(sprintf(
134
			'Deployment rejected email sent to requester %s <%s>',
135
			$deployer->Name,
136
			$deployer->Email
137
		));
138
	}
139
140
}
141