Path: blob/master/src/applications/almanac/storage/AlmanacBinding.php
12256 views
<?php12final class AlmanacBinding3extends AlmanacDAO4implements5PhabricatorPolicyInterface,6PhabricatorApplicationTransactionInterface,7AlmanacPropertyInterface,8PhabricatorDestructibleInterface,9PhabricatorExtendedPolicyInterface,10PhabricatorConduitResultInterface {1112protected $servicePHID;13protected $devicePHID;14protected $interfacePHID;15protected $isDisabled;1617private $service = self::ATTACHABLE;18private $device = self::ATTACHABLE;19private $interface = self::ATTACHABLE;20private $almanacProperties = self::ATTACHABLE;2122public static function initializeNewBinding(AlmanacService $service) {23return id(new AlmanacBinding())24->setServicePHID($service->getPHID())25->attachService($service)26->attachAlmanacProperties(array())27->setIsDisabled(0);28}2930protected function getConfiguration() {31return array(32self::CONFIG_AUX_PHID => true,33self::CONFIG_COLUMN_SCHEMA => array(34'isDisabled' => 'bool',35),36self::CONFIG_KEY_SCHEMA => array(37'key_service' => array(38'columns' => array('servicePHID', 'interfacePHID'),39'unique' => true,40),41'key_device' => array(42'columns' => array('devicePHID'),43),44'key_interface' => array(45'columns' => array('interfacePHID'),46),47),48) + parent::getConfiguration();49}5051public function getPHIDType() {52return AlmanacBindingPHIDType::TYPECONST;53}5455public function getName() {56return pht('Binding %s', $this->getID());57}5859public function getURI() {60return urisprintf(61'/almanac/binding/%s/',62$this->getID());63}6465public function getService() {66return $this->assertAttached($this->service);67}6869public function attachService(AlmanacService $service) {70$this->service = $service;71return $this;72}7374public function getDevice() {75return $this->assertAttached($this->device);76}7778public function attachDevice(AlmanacDevice $device) {79$this->device = $device;80return $this;81}8283public function hasInterface() {84return ($this->interface !== self::ATTACHABLE);85}8687public function getInterface() {88return $this->assertAttached($this->interface);89}9091public function attachInterface(AlmanacInterface $interface) {92$this->interface = $interface;93return $this;94}959697/* -( AlmanacPropertyInterface )------------------------------------------- */9899100public function attachAlmanacProperties(array $properties) {101assert_instances_of($properties, 'AlmanacProperty');102$this->almanacProperties = mpull($properties, null, 'getFieldName');103return $this;104}105106public function getAlmanacProperties() {107return $this->assertAttached($this->almanacProperties);108}109110public function hasAlmanacProperty($key) {111$this->assertAttached($this->almanacProperties);112return isset($this->almanacProperties[$key]);113}114115public function getAlmanacProperty($key) {116return $this->assertAttachedKey($this->almanacProperties, $key);117}118119public function getAlmanacPropertyValue($key, $default = null) {120if ($this->hasAlmanacProperty($key)) {121return $this->getAlmanacProperty($key)->getFieldValue();122} else {123return $default;124}125}126127public function getAlmanacPropertyFieldSpecifications() {128return $this->getService()->getBindingFieldSpecifications($this);129}130131public function newAlmanacPropertyEditEngine() {132return new AlmanacBindingPropertyEditEngine();133}134135public function getAlmanacPropertySetTransactionType() {136return AlmanacBindingSetPropertyTransaction::TRANSACTIONTYPE;137}138139public function getAlmanacPropertyDeleteTransactionType() {140return AlmanacBindingDeletePropertyTransaction::TRANSACTIONTYPE;141}142143144/* -( PhabricatorPolicyInterface )----------------------------------------- */145146147public function getCapabilities() {148return array(149PhabricatorPolicyCapability::CAN_VIEW,150PhabricatorPolicyCapability::CAN_EDIT,151);152}153154public function getPolicy($capability) {155return $this->getService()->getPolicy($capability);156}157158public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {159return $this->getService()->hasAutomaticCapability($capability, $viewer);160}161162public function describeAutomaticCapability($capability) {163$notes = array(164pht('A binding inherits the policies of its service.'),165pht(166'To view a binding, you must also be able to view its device and '.167'interface.'),168);169170return $notes;171}172173174/* -( PhabricatorExtendedPolicyInterface )--------------------------------- */175176177public function getExtendedPolicy($capability, PhabricatorUser $viewer) {178switch ($capability) {179case PhabricatorPolicyCapability::CAN_EDIT:180if ($this->getService()->isClusterService()) {181return array(182array(183new PhabricatorAlmanacApplication(),184AlmanacManageClusterServicesCapability::CAPABILITY,185),186);187}188break;189}190191return array();192}193194/* -( PhabricatorApplicationTransactionInterface )------------------------- */195196197public function getApplicationTransactionEditor() {198return new AlmanacBindingEditor();199}200201public function getApplicationTransactionTemplate() {202return new AlmanacBindingTransaction();203}204205206/* -( PhabricatorDestructibleInterface )----------------------------------- */207208209public function destroyObjectPermanently(210PhabricatorDestructionEngine $engine) {211212$this->delete();213}214215216/* -( PhabricatorConduitResultInterface )---------------------------------- */217218219public function getFieldSpecificationsForConduit() {220return array(221id(new PhabricatorConduitSearchFieldSpecification())222->setKey('servicePHID')223->setType('phid')224->setDescription(pht('The bound service.')),225id(new PhabricatorConduitSearchFieldSpecification())226->setKey('devicePHID')227->setType('phid')228->setDescription(pht('The device the service is bound to.')),229id(new PhabricatorConduitSearchFieldSpecification())230->setKey('interfacePHID')231->setType('phid')232->setDescription(pht('The interface the service is bound to.')),233id(new PhabricatorConduitSearchFieldSpecification())234->setKey('disabled')235->setType('bool')236->setDescription(pht('Interface status.')),237);238}239240public function getFieldValuesForConduit() {241return array(242'servicePHID' => $this->getServicePHID(),243'devicePHID' => $this->getDevicePHID(),244'interfacePHID' => $this->getInterfacePHID(),245'disabled' => (bool)$this->getIsDisabled(),246);247}248249public function getConduitSearchAttachments() {250return array(251id(new AlmanacPropertiesSearchEngineAttachment())252->setAttachmentKey('properties'),253);254}255256}257258259