PageRenderTime 29ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/applications/almanac/storage/AlmanacBinding.php

http://github.com/facebook/phabricator
PHP | 264 lines | 201 code | 57 blank | 6 comment | 5 complexity | a5e2e5eb1456d609dc43265215573d59 MD5 | raw file
Possible License(s): JSON, MPL-2.0-no-copyleft-exception, Apache-2.0, BSD-3-Clause, LGPL-2.0, MIT, LGPL-2.1, LGPL-3.0
  1. <?php
  2. final class AlmanacBinding
  3. extends AlmanacDAO
  4. implements
  5. PhabricatorPolicyInterface,
  6. PhabricatorApplicationTransactionInterface,
  7. AlmanacPropertyInterface,
  8. PhabricatorDestructibleInterface,
  9. PhabricatorExtendedPolicyInterface,
  10. PhabricatorConduitResultInterface {
  11. protected $servicePHID;
  12. protected $devicePHID;
  13. protected $interfacePHID;
  14. protected $mailKey;
  15. protected $isDisabled;
  16. private $service = self::ATTACHABLE;
  17. private $device = self::ATTACHABLE;
  18. private $interface = self::ATTACHABLE;
  19. private $almanacProperties = self::ATTACHABLE;
  20. public static function initializeNewBinding(AlmanacService $service) {
  21. return id(new AlmanacBinding())
  22. ->setServicePHID($service->getPHID())
  23. ->attachService($service)
  24. ->attachAlmanacProperties(array())
  25. ->setIsDisabled(0);
  26. }
  27. protected function getConfiguration() {
  28. return array(
  29. self::CONFIG_AUX_PHID => true,
  30. self::CONFIG_COLUMN_SCHEMA => array(
  31. 'mailKey' => 'bytes20',
  32. 'isDisabled' => 'bool',
  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. public function generatePHID() {
  49. return PhabricatorPHID::generateNewPHID(AlmanacBindingPHIDType::TYPECONST);
  50. }
  51. public function save() {
  52. if (!$this->mailKey) {
  53. $this->mailKey = Filesystem::readRandomCharacters(20);
  54. }
  55. return parent::save();
  56. }
  57. public function getName() {
  58. return pht('Binding %s', $this->getID());
  59. }
  60. public function getURI() {
  61. return '/almanac/binding/'.$this->getID().'/';
  62. }
  63. public function getService() {
  64. return $this->assertAttached($this->service);
  65. }
  66. public function attachService(AlmanacService $service) {
  67. $this->service = $service;
  68. return $this;
  69. }
  70. public function getDevice() {
  71. return $this->assertAttached($this->device);
  72. }
  73. public function attachDevice(AlmanacDevice $device) {
  74. $this->device = $device;
  75. return $this;
  76. }
  77. public function hasInterface() {
  78. return ($this->interface !== self::ATTACHABLE);
  79. }
  80. public function getInterface() {
  81. return $this->assertAttached($this->interface);
  82. }
  83. public function attachInterface(AlmanacInterface $interface) {
  84. $this->interface = $interface;
  85. return $this;
  86. }
  87. /* -( AlmanacPropertyInterface )------------------------------------------- */
  88. public function attachAlmanacProperties(array $properties) {
  89. assert_instances_of($properties, 'AlmanacProperty');
  90. $this->almanacProperties = mpull($properties, null, 'getFieldName');
  91. return $this;
  92. }
  93. public function getAlmanacProperties() {
  94. return $this->assertAttached($this->almanacProperties);
  95. }
  96. public function hasAlmanacProperty($key) {
  97. $this->assertAttached($this->almanacProperties);
  98. return isset($this->almanacProperties[$key]);
  99. }
  100. public function getAlmanacProperty($key) {
  101. return $this->assertAttachedKey($this->almanacProperties, $key);
  102. }
  103. public function getAlmanacPropertyValue($key, $default = null) {
  104. if ($this->hasAlmanacProperty($key)) {
  105. return $this->getAlmanacProperty($key)->getFieldValue();
  106. } else {
  107. return $default;
  108. }
  109. }
  110. public function getAlmanacPropertyFieldSpecifications() {
  111. return $this->getService()->getBindingFieldSpecifications($this);
  112. }
  113. public function newAlmanacPropertyEditEngine() {
  114. return new AlmanacBindingPropertyEditEngine();
  115. }
  116. public function getAlmanacPropertySetTransactionType() {
  117. return AlmanacBindingSetPropertyTransaction::TRANSACTIONTYPE;
  118. }
  119. public function getAlmanacPropertyDeleteTransactionType() {
  120. return AlmanacBindingDeletePropertyTransaction::TRANSACTIONTYPE;
  121. }
  122. /* -( PhabricatorPolicyInterface )----------------------------------------- */
  123. public function getCapabilities() {
  124. return array(
  125. PhabricatorPolicyCapability::CAN_VIEW,
  126. PhabricatorPolicyCapability::CAN_EDIT,
  127. );
  128. }
  129. public function getPolicy($capability) {
  130. return $this->getService()->getPolicy($capability);
  131. }
  132. public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
  133. return $this->getService()->hasAutomaticCapability($capability, $viewer);
  134. }
  135. public function describeAutomaticCapability($capability) {
  136. $notes = array(
  137. pht('A binding inherits the policies of its service.'),
  138. pht(
  139. 'To view a binding, you must also be able to view its device and '.
  140. 'interface.'),
  141. );
  142. return $notes;
  143. }
  144. /* -( PhabricatorExtendedPolicyInterface )--------------------------------- */
  145. public function getExtendedPolicy($capability, PhabricatorUser $viewer) {
  146. switch ($capability) {
  147. case PhabricatorPolicyCapability::CAN_EDIT:
  148. if ($this->getService()->isClusterService()) {
  149. return array(
  150. array(
  151. new PhabricatorAlmanacApplication(),
  152. AlmanacManageClusterServicesCapability::CAPABILITY,
  153. ),
  154. );
  155. }
  156. break;
  157. }
  158. return array();
  159. }
  160. /* -( PhabricatorApplicationTransactionInterface )------------------------- */
  161. public function getApplicationTransactionEditor() {
  162. return new AlmanacBindingEditor();
  163. }
  164. public function getApplicationTransactionTemplate() {
  165. return new AlmanacBindingTransaction();
  166. }
  167. /* -( PhabricatorDestructibleInterface )----------------------------------- */
  168. public function destroyObjectPermanently(
  169. PhabricatorDestructionEngine $engine) {
  170. $this->delete();
  171. }
  172. /* -( PhabricatorConduitResultInterface )---------------------------------- */
  173. public function getFieldSpecificationsForConduit() {
  174. return array(
  175. id(new PhabricatorConduitSearchFieldSpecification())
  176. ->setKey('servicePHID')
  177. ->setType('phid')
  178. ->setDescription(pht('The bound service.')),
  179. id(new PhabricatorConduitSearchFieldSpecification())
  180. ->setKey('devicePHID')
  181. ->setType('phid')
  182. ->setDescription(pht('The device the service is bound to.')),
  183. id(new PhabricatorConduitSearchFieldSpecification())
  184. ->setKey('interfacePHID')
  185. ->setType('phid')
  186. ->setDescription(pht('The interface the service is bound to.')),
  187. id(new PhabricatorConduitSearchFieldSpecification())
  188. ->setKey('disabled')
  189. ->setType('bool')
  190. ->setDescription(pht('Interface status.')),
  191. );
  192. }
  193. public function getFieldValuesForConduit() {
  194. return array(
  195. 'servicePHID' => $this->getServicePHID(),
  196. 'devicePHID' => $this->getDevicePHID(),
  197. 'interfacePHID' => $this->getInterfacePHID(),
  198. 'disabled' => (bool)$this->getIsDisabled(),
  199. );
  200. }
  201. public function getConduitSearchAttachments() {
  202. return array(
  203. id(new AlmanacPropertiesSearchEngineAttachment())
  204. ->setAttachmentKey('properties'),
  205. );
  206. }
  207. }