Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / people / mail / PhabricatorPeopleMailEngine.php
blob6b7aa7818e33659576597ddb6207054a6cfb5807
1 <?php
3 abstract class PhabricatorPeopleMailEngine
4 extends Phobject {
6 private $sender;
7 private $recipient;
8 private $recipientAddress;
9 private $activityLog;
11 final public function setSender(PhabricatorUser $sender) {
12 $this->sender = $sender;
13 return $this;
16 final public function getSender() {
17 if (!$this->sender) {
18 throw new PhutilInvalidStateException('setSender');
20 return $this->sender;
23 final public function setRecipient(PhabricatorUser $recipient) {
24 $this->recipient = $recipient;
25 return $this;
28 final public function getRecipient() {
29 if (!$this->recipient) {
30 throw new PhutilInvalidStateException('setRecipient');
32 return $this->recipient;
35 final public function setRecipientAddress(PhutilEmailAddress $address) {
36 $this->recipientAddress = $address;
37 return $this;
40 final public function getRecipientAddress() {
41 if (!$this->recipientAddress) {
42 throw new PhutilInvalidStateException('recipientAddress');
44 return $this->recipientAddress;
47 final public function hasRecipientAddress() {
48 return ($this->recipientAddress !== null);
51 final public function setActivityLog(PhabricatorUserLog $activity_log) {
52 $this->activityLog = $activity_log;
53 return $this;
56 final public function getActivityLog() {
57 return $this->activityLog;
60 final public function canSendMail() {
61 try {
62 $this->validateMail();
63 return true;
64 } catch (PhabricatorPeopleMailEngineException $ex) {
65 return false;
69 final public function sendMail() {
70 $this->validateMail();
71 $mail = $this->newMail();
73 if ($this->hasRecipientAddress()) {
74 $recipient_address = $this->getRecipientAddress();
75 $mail->addRawTos(array($recipient_address->getAddress()));
76 } else {
77 $recipient = $this->getRecipient();
78 $mail->addTos(array($recipient->getPHID()));
81 $activity_log = $this->getActivityLog();
82 if ($activity_log) {
83 $activity_log->save();
85 $body = array();
86 $body[] = rtrim($mail->getBody(), "\n");
87 $body[] = pht('Activity Log ID: #%d', $activity_log->getID());
88 $body = implode("\n\n", $body)."\n";
90 $mail->setBody($body);
93 $mail
94 ->setForceDelivery(true)
95 ->save();
97 return $mail;
100 abstract public function validateMail();
101 abstract protected function newMail();
103 final protected function throwValidationException($title, $body) {
104 throw new PhabricatorPeopleMailEngineException($title, $body);
107 final protected function newRemarkupText($text) {
108 $recipient = $this->getRecipient();
110 $engine = PhabricatorMarkupEngine::newMarkupEngine(array())
111 ->setConfig('viewer', $recipient)
112 ->setConfig('uri.base', PhabricatorEnv::getProductionURI('/'))
113 ->setMode(PhutilRemarkupEngine::MODE_TEXT);
115 $rendered_text = $engine->markupText($text);
116 $rendered_text = rtrim($rendered_text, "\n");
118 return $rendered_text;