[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/almanac/storage/ -> AlmanacBinding.php (source)

   1  <?php
   2  
   3  final class AlmanacBinding
   4    extends AlmanacDAO
   5    implements
   6      PhabricatorPolicyInterface,
   7      PhabricatorCustomFieldInterface,
   8      PhabricatorApplicationTransactionInterface,
   9      AlmanacPropertyInterface {
  10  
  11    protected $servicePHID;
  12    protected $devicePHID;
  13    protected $interfacePHID;
  14    protected $mailKey;
  15  
  16    private $service = self::ATTACHABLE;
  17    private $device = self::ATTACHABLE;
  18    private $interface = self::ATTACHABLE;
  19    private $customFields = self::ATTACHABLE;
  20    private $almanacProperties = self::ATTACHABLE;
  21  
  22    public static function initializeNewBinding(AlmanacService $service) {
  23      return id(new AlmanacBinding())
  24        ->setServicePHID($service->getPHID())
  25        ->attachAlmanacProperties(array());
  26    }
  27  
  28    public function getConfiguration() {
  29      return array(
  30        self::CONFIG_AUX_PHID => true,
  31        self::CONFIG_COLUMN_SCHEMA => array(
  32          'mailKey' => 'bytes20',
  33        ),
  34        self::CONFIG_KEY_SCHEMA => array(
  35          'key_service' => array(
  36            'columns' => array('servicePHID', 'interfacePHID'),
  37            'unique' => true,
  38          ),
  39          'key_device' => array(
  40            'columns' => array('devicePHID'),
  41          ),
  42          'key_interface' => array(
  43            'columns' => array('interfacePHID'),
  44          ),
  45        ),
  46      ) + parent::getConfiguration();
  47    }
  48  
  49    public function generatePHID() {
  50      return PhabricatorPHID::generateNewPHID(AlmanacBindingPHIDType::TYPECONST);
  51    }
  52  
  53    public function save() {
  54      if (!$this->mailKey) {
  55        $this->mailKey = Filesystem::readRandomCharacters(20);
  56      }
  57      return parent::save();
  58    }
  59  
  60    public function getURI() {
  61      return '/almanac/binding/'.$this->getID().'/';
  62    }
  63  
  64    public function getService() {
  65      return $this->assertAttached($this->service);
  66    }
  67  
  68    public function attachService(AlmanacService $service) {
  69      $this->service = $service;
  70      return $this;
  71    }
  72  
  73    public function getDevice() {
  74      return $this->assertAttached($this->device);
  75    }
  76  
  77    public function attachDevice(AlmanacDevice $device) {
  78      $this->device = $device;
  79      return $this;
  80    }
  81  
  82    public function getInterface() {
  83      return $this->assertAttached($this->interface);
  84    }
  85  
  86    public function attachInterface(AlmanacInterface $interface) {
  87      $this->interface = $interface;
  88      return $this;
  89    }
  90  
  91  
  92  /* -(  AlmanacPropertyInterface  )------------------------------------------- */
  93  
  94  
  95    public function attachAlmanacProperties(array $properties) {
  96      assert_instances_of($properties, 'AlmanacProperty');
  97      $this->almanacProperties = mpull($properties, null, 'getFieldName');
  98      return $this;
  99    }
 100  
 101    public function getAlmanacProperties() {
 102      return $this->assertAttached($this->almanacProperties);
 103    }
 104  
 105    public function hasAlmanacProperty($key) {
 106      $this->assertAttached($this->almanacProperties);
 107      return isset($this->almanacProperties[$key]);
 108    }
 109  
 110    public function getAlmanacProperty($key) {
 111      return $this->assertAttachedKey($this->almanacProperties, $key);
 112    }
 113  
 114    public function getAlmanacPropertyValue($key, $default = null) {
 115      if ($this->hasAlmanacProperty($key)) {
 116        return $this->getAlmanacProperty($key)->getFieldValue();
 117      } else {
 118        return $default;
 119      }
 120    }
 121  
 122  
 123  /* -(  PhabricatorPolicyInterface  )----------------------------------------- */
 124  
 125  
 126    public function getCapabilities() {
 127      return array(
 128        PhabricatorPolicyCapability::CAN_VIEW,
 129        PhabricatorPolicyCapability::CAN_EDIT,
 130      );
 131    }
 132  
 133    public function getPolicy($capability) {
 134      return $this->getService()->getPolicy($capability);
 135    }
 136  
 137    public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
 138      return $this->getService()->hasAutomaticCapability($capability, $viewer);
 139    }
 140  
 141    public function describeAutomaticCapability($capability) {
 142      return array(
 143        pht('A binding inherits the policies of its service.'),
 144        pht(
 145          'To view a binding, you must also be able to view its device and '.
 146          'interface.'),
 147      );
 148    }
 149  
 150  
 151  /* -(  PhabricatorCustomFieldInterface  )------------------------------------ */
 152  
 153  
 154    public function getCustomFieldSpecificationForRole($role) {
 155      return array();
 156    }
 157  
 158    public function getCustomFieldBaseClass() {
 159      return 'AlmanacCustomField';
 160    }
 161  
 162    public function getCustomFields() {
 163      return $this->assertAttached($this->customFields);
 164    }
 165  
 166    public function attachCustomFields(PhabricatorCustomFieldAttachment $fields) {
 167      $this->customFields = $fields;
 168      return $this;
 169    }
 170  
 171  
 172  /* -(  PhabricatorApplicationTransactionInterface  )------------------------- */
 173  
 174  
 175    public function getApplicationTransactionEditor() {
 176      return new AlmanacBindingEditor();
 177    }
 178  
 179    public function getApplicationTransactionObject() {
 180      return $this;
 181    }
 182  
 183    public function getApplicationTransactionTemplate() {
 184      return new AlmanacBindingTransaction();
 185    }
 186  
 187  }


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