| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | use \Finite\Event\TransitionEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | class DNDeploymentHandlers extends Object { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 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) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 		if (!$this->canSendEmail($deployment)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 			return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		$deployer = $deployment->Deployer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 		$approver = $deployment->Approver(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 		if (!$this->canSendEmail($deployment)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 			return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 		$deployer = $deployment->Deployer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 		$approver = $deployment->Approver(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 		if (!$this->canSendEmail($deployment)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 			return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 		$deployer = $deployment->Deployer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 		$approver = $deployment->Approver(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 		if (!$this->canSendEmail($deployment)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 			return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 		$deployer = $deployment->Deployer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 		$approver = $deployment->Approver(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |  | 
            
                        
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.