Changeset View
Changeset View
Standalone View
Standalone View
src/applications/people/mail/PhabricatorPeopleMailEngine.php
<?php | <?php | ||||
abstract class PhabricatorPeopleMailEngine | abstract class PhabricatorPeopleMailEngine | ||||
extends Phobject { | extends Phobject { | ||||
private $sender; | private $sender; | ||||
private $recipient; | private $recipient; | ||||
private $recipientAddress; | |||||
final public function setSender(PhabricatorUser $sender) { | final public function setSender(PhabricatorUser $sender) { | ||||
$this->sender = $sender; | $this->sender = $sender; | ||||
return $this; | return $this; | ||||
} | } | ||||
final public function getSender() { | final public function getSender() { | ||||
if (!$this->sender) { | if (!$this->sender) { | ||||
Show All 9 Lines | abstract class PhabricatorPeopleMailEngine | ||||
final public function getRecipient() { | final public function getRecipient() { | ||||
if (!$this->recipient) { | if (!$this->recipient) { | ||||
throw new PhutilInvalidStateException('setRecipient'); | throw new PhutilInvalidStateException('setRecipient'); | ||||
} | } | ||||
return $this->recipient; | return $this->recipient; | ||||
} | } | ||||
final public function setRecipientAddress(PhutilEmailAddress $address) { | |||||
$this->recipientAddress = $address; | |||||
return $this; | |||||
} | |||||
final public function getRecipientAddress() { | |||||
if (!$this->recipientAddress) { | |||||
throw new PhutilInvalidStateException('recipientAddress'); | |||||
} | |||||
return $this->recipientAddress; | |||||
} | |||||
final public function hasRecipientAddress() { | |||||
return ($this->recipientAddress !== null); | |||||
} | |||||
final public function canSendMail() { | final public function canSendMail() { | ||||
try { | try { | ||||
$this->validateMail(); | $this->validateMail(); | ||||
return true; | return true; | ||||
} catch (PhabricatorPeopleMailEngineException $ex) { | } catch (PhabricatorPeopleMailEngineException $ex) { | ||||
return false; | return false; | ||||
} | } | ||||
} | } | ||||
final public function sendMail() { | final public function sendMail() { | ||||
$this->validateMail(); | $this->validateMail(); | ||||
$mail = $this->newMail(); | $mail = $this->newMail(); | ||||
if ($this->hasRecipientAddress()) { | |||||
$recipient_address = $this->getRecipientAddress(); | |||||
$mail->addRawTos(array($recipient_address->getAddress())); | |||||
} else { | |||||
$recipient = $this->getRecipient(); | |||||
$mail->addTos(array($recipient->getPHID())); | |||||
} | |||||
->setForceDelivery(true) | ->setForceDelivery(true) | ||||
->save(); | ->save(); | ||||
return $mail; | return $mail; | ||||
} | } | ||||
abstract public function validateMail(); | abstract public function validateMail(); | ||||
abstract protected function newMail(); | abstract protected function newMail(); | ||||
final protected function throwValidationException($title, $body) { | final protected function throwValidationException($title, $body) { | ||||
throw new PhabricatorPeopleMailEngineException($title, $body); | throw new PhabricatorPeopleMailEngineException($title, $body); | ||||
} | } | ||||
final protected function newRemarkupText($text) { | final protected function newRemarkupText($text) { | ||||
$recipient = $this->getRecipient(); | $recipient = $this->getRecipient(); | ||||
$engine = PhabricatorMarkupEngine::newMarkupEngine(array()) | $engine = PhabricatorMarkupEngine::newMarkupEngine(array()) | ||||
->setConfig('viewer', $recipient) | ->setConfig('viewer', $recipient) | ||||
->setConfig('uri.base', PhabricatorEnv::getProductionURI('/')) | ->setConfig('uri.base', PhabricatorEnv::getProductionURI('/')) | ||||
->setMode(PhutilRemarkupEngine::MODE_TEXT); | ->setMode(PhutilRemarkupEngine::MODE_TEXT); | ||||
return $engine->markupText($text); | return $engine->markupText($text); | ||||
} | } | ||||
} | } |