[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/owners/mail/ -> PackageMail.php (source)

   1  <?php
   2  
   3  abstract class PackageMail extends PhabricatorMail {
   4  
   5    protected $package;
   6    protected $handles;
   7    protected $owners;
   8    protected $paths;
   9    protected $mailTo;
  10  
  11    public function __construct(PhabricatorOwnersPackage $package) {
  12      $this->package = $package;
  13    }
  14  
  15    abstract protected function getVerb();
  16  
  17    abstract protected function isNewThread();
  18  
  19    final protected function getPackage() {
  20      return $this->package;
  21    }
  22  
  23    final protected function getHandles() {
  24      return $this->handles;
  25    }
  26  
  27    final protected function getOwners() {
  28      return $this->owners;
  29    }
  30  
  31    final protected function getPaths() {
  32      return $this->paths;
  33    }
  34  
  35    final protected function getMailTo() {
  36      return $this->mailTo;
  37    }
  38  
  39    final protected function renderPackageTitle() {
  40      return $this->getPackage()->getName();
  41    }
  42  
  43    final protected function renderRepoSubSection($repository_phid, $paths) {
  44      $handles = $this->getHandles();
  45      $section = array();
  46      $section[] = '  In repository '.$handles[$repository_phid]->getName().
  47        ' - '.PhabricatorEnv::getProductionURI($handles[$repository_phid]
  48        ->getURI());
  49      foreach ($paths as $path => $excluded) {
  50        $section[] = '    '.($excluded ? 'Excluded' : 'Included').' '.$path;
  51      }
  52  
  53      return implode("\n", $section);
  54    }
  55  
  56    protected function needSend() {
  57      return true;
  58    }
  59  
  60    protected function loadData() {
  61      $package = $this->getPackage();
  62      $owners = $package->loadOwners();
  63      $this->owners = $owners;
  64  
  65      $owner_phids = mpull($owners, 'getUserPHID');
  66      $primary_owner_phid = $package->getPrimaryOwnerPHID();
  67      $mail_to = $owner_phids;
  68      if (!in_array($primary_owner_phid, $owner_phids)) {
  69        $mail_to[] = $primary_owner_phid;
  70      }
  71      $this->mailTo = $mail_to;
  72  
  73      $this->paths = array();
  74      $repository_paths = mgroup($package->loadPaths(), 'getRepositoryPHID');
  75      foreach ($repository_paths as $repository_phid => $paths) {
  76        $this->paths[$repository_phid] = mpull($paths, 'getExcluded', 'getPath');
  77      }
  78  
  79      $phids = array_merge(
  80        $this->mailTo,
  81        array($package->getActorPHID()),
  82        array_keys($this->paths));
  83      $this->handles = id(new PhabricatorHandleQuery())
  84        ->setViewer($this->getActor())
  85        ->withPHIDs($phids)
  86        ->execute();
  87    }
  88  
  89    final protected function renderSummarySection() {
  90      $package = $this->getPackage();
  91      $handles = $this->getHandles();
  92      $section = array();
  93      $section[] = $handles[$package->getActorPHID()]->getName().' '.
  94        strtolower($this->getVerb()).' '.$this->renderPackageTitle().'.';
  95      $section[] = '';
  96  
  97      $section[] = 'PACKAGE DETAIL';
  98      $section[] = '  '.PhabricatorEnv::getProductionURI(
  99        '/owners/package/'.$package->getID().'/');
 100  
 101      return implode("\n", $section);
 102    }
 103  
 104    protected function renderDescriptionSection() {
 105      return "PACKAGE DESCRIPTION\n".
 106        '  '.$this->getPackage()->getDescription();
 107    }
 108  
 109    protected function renderPrimaryOwnerSection() {
 110      $handles = $this->getHandles();
 111      return "PRIMARY OWNER\n".
 112        '  '.$handles[$this->getPackage()->getPrimaryOwnerPHID()]->getName();
 113    }
 114  
 115    protected function renderOwnersSection() {
 116      $handles = $this->getHandles();
 117      $owners = $this->getOwners();
 118      if (!$owners) {
 119        return null;
 120      }
 121  
 122      $owners = mpull($owners, 'getUserPHID');
 123      $owners = array_select_keys($handles, $owners);
 124      $owners = mpull($owners, 'getName');
 125      return "OWNERS\n".
 126        '  '.implode(', ', $owners);
 127    }
 128  
 129    protected function renderAuditingEnabledSection() {
 130      return "AUDITING ENABLED STATUS\n".
 131        '  '.($this->getPackage()->getAuditingEnabled() ? 'Enabled' : 'Disabled');
 132    }
 133  
 134    protected function renderPathsSection() {
 135      $section = array();
 136      $section[] = 'PATHS';
 137      foreach ($this->paths as $repository_phid => $paths) {
 138        $section[] = $this->renderRepoSubSection($repository_phid, $paths);
 139      }
 140  
 141      return implode("\n", $section);
 142    }
 143  
 144    final protected function renderBody() {
 145      $body = array();
 146      $body[] = $this->renderSummarySection();
 147      $body[] = $this->renderDescriptionSection();
 148      $body[] = $this->renderPrimaryOwnerSection();
 149      $body[] = $this->renderOwnersSection();
 150      $body[] = $this->renderAuditingEnabledSection();
 151      $body[] = $this->renderPathsSection();
 152      $body = array_filter($body);
 153      return implode("\n\n", $body)."\n";
 154    }
 155  
 156    final public function send() {
 157      $mails = $this->prepareMails();
 158  
 159      foreach ($mails as $mail) {
 160        $mail->saveAndSend();
 161      }
 162    }
 163  
 164    final public function prepareMails() {
 165      if (!$this->needSend()) {
 166        return array();
 167      }
 168  
 169      $this->loadData();
 170  
 171      $package = $this->getPackage();
 172      $prefix = PhabricatorEnv::getEnvConfig('metamta.package.subject-prefix');
 173      $verb = $this->getVerb();
 174      $threading = $this->getMailThreading();
 175      list($thread_id, $thread_topic) = $threading;
 176  
 177      $template = id(new PhabricatorMetaMTAMail())
 178        ->setSubject($this->renderPackageTitle())
 179        ->setSubjectPrefix($prefix)
 180        ->setVarySubjectPrefix("[{$verb}]")
 181        ->setFrom($package->getActorPHID())
 182        ->setThreadID($thread_id, $this->isNewThread())
 183        ->addHeader('Thread-Topic', $thread_topic)
 184        ->setRelatedPHID($package->getPHID())
 185        ->setIsBulk(true)
 186        ->setBody($this->renderBody());
 187  
 188      $reply_handler = $this->newReplyHandler();
 189      $mails = $reply_handler->multiplexMail(
 190        $template,
 191        array_select_keys($this->getHandles(), $this->getMailTo()),
 192        array());
 193      return $mails;
 194    }
 195  
 196    private function getMailThreading() {
 197      return array(
 198        'package-'.$this->getPackage()->getPHID(),
 199        'Package '.$this->getPackage()->getOriginalName(),
 200      );
 201    }
 202  
 203    private function newReplyHandler() {
 204      $reply_handler = PhabricatorEnv::newObjectFromConfig(
 205        'metamta.package.reply-handler');
 206      $reply_handler->setMailReceiver($this->getPackage());
 207      return $reply_handler;
 208    }
 209  
 210  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1