Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / almanac / storage / AlmanacInterface.php
blob6cd318186f6d21571e1e07c794dcfedc96d49347
1 <?php
3 final class AlmanacInterface
4 extends AlmanacDAO
5 implements
6 PhabricatorPolicyInterface,
7 PhabricatorDestructibleInterface,
8 PhabricatorExtendedPolicyInterface,
9 PhabricatorApplicationTransactionInterface,
10 PhabricatorConduitResultInterface {
12 protected $devicePHID;
13 protected $networkPHID;
14 protected $address;
15 protected $port;
17 private $device = self::ATTACHABLE;
18 private $network = self::ATTACHABLE;
20 public static function initializeNewInterface() {
21 return id(new AlmanacInterface());
24 protected function getConfiguration() {
25 return array(
26 self::CONFIG_AUX_PHID => true,
27 self::CONFIG_COLUMN_SCHEMA => array(
28 'address' => 'text64',
29 'port' => 'uint32',
31 self::CONFIG_KEY_SCHEMA => array(
32 'key_location' => array(
33 'columns' => array('networkPHID', 'address', 'port'),
35 'key_device' => array(
36 'columns' => array('devicePHID'),
38 'key_unique' => array(
39 'columns' => array('devicePHID', 'networkPHID', 'address', 'port'),
40 'unique' => true,
43 ) + parent::getConfiguration();
46 public function generatePHID() {
47 return PhabricatorPHID::generateNewPHID(
48 AlmanacInterfacePHIDType::TYPECONST);
51 public function getDevice() {
52 return $this->assertAttached($this->device);
55 public function attachDevice(AlmanacDevice $device) {
56 $this->device = $device;
57 return $this;
60 public function getNetwork() {
61 return $this->assertAttached($this->network);
64 public function attachNetwork(AlmanacNetwork $network) {
65 $this->network = $network;
66 return $this;
69 public function toAddress() {
70 return AlmanacAddress::newFromParts(
71 $this->getNetworkPHID(),
72 $this->getAddress(),
73 $this->getPort());
76 public function getAddressHash() {
77 return $this->toAddress()->toHash();
80 public function renderDisplayAddress() {
81 return $this->getAddress().':'.$this->getPort();
84 public function loadIsInUse() {
85 $binding = id(new AlmanacBindingQuery())
86 ->setViewer(PhabricatorUser::getOmnipotentUser())
87 ->withInterfacePHIDs(array($this->getPHID()))
88 ->setLimit(1)
89 ->executeOne();
91 return (bool)$binding;
95 /* -( PhabricatorPolicyInterface )----------------------------------------- */
98 public function getCapabilities() {
99 return array(
100 PhabricatorPolicyCapability::CAN_VIEW,
101 PhabricatorPolicyCapability::CAN_EDIT,
105 public function getPolicy($capability) {
106 return $this->getDevice()->getPolicy($capability);
109 public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
110 return $this->getDevice()->hasAutomaticCapability($capability, $viewer);
113 public function describeAutomaticCapability($capability) {
114 $notes = array(
115 pht('An interface inherits the policies of the device it belongs to.'),
116 pht(
117 'You must be able to view the network an interface resides on to '.
118 'view the interface.'),
121 return $notes;
125 /* -( PhabricatorExtendedPolicyInterface )--------------------------------- */
128 public function getExtendedPolicy($capability, PhabricatorUser $viewer) {
129 switch ($capability) {
130 case PhabricatorPolicyCapability::CAN_EDIT:
131 if ($this->getDevice()->isClusterDevice()) {
132 return array(
133 array(
134 new PhabricatorAlmanacApplication(),
135 AlmanacManageClusterServicesCapability::CAPABILITY,
139 break;
142 return array();
146 /* -( PhabricatorDestructibleInterface )----------------------------------- */
149 public function destroyObjectPermanently(
150 PhabricatorDestructionEngine $engine) {
152 $bindings = id(new AlmanacBindingQuery())
153 ->setViewer($engine->getViewer())
154 ->withInterfacePHIDs(array($this->getPHID()))
155 ->execute();
156 foreach ($bindings as $binding) {
157 $engine->destroyObject($binding);
160 $this->delete();
164 /* -( PhabricatorApplicationTransactionInterface )------------------------- */
167 public function getApplicationTransactionEditor() {
168 return new AlmanacInterfaceEditor();
171 public function getApplicationTransactionTemplate() {
172 return new AlmanacInterfaceTransaction();
176 /* -( PhabricatorConduitResultInterface )---------------------------------- */
179 public function getFieldSpecificationsForConduit() {
180 return array(
181 id(new PhabricatorConduitSearchFieldSpecification())
182 ->setKey('devicePHID')
183 ->setType('phid')
184 ->setDescription(pht('The device the interface is on.')),
185 id(new PhabricatorConduitSearchFieldSpecification())
186 ->setKey('networkPHID')
187 ->setType('phid')
188 ->setDescription(pht('The network the interface is part of.')),
189 id(new PhabricatorConduitSearchFieldSpecification())
190 ->setKey('address')
191 ->setType('string')
192 ->setDescription(pht('The address of the interface.')),
193 id(new PhabricatorConduitSearchFieldSpecification())
194 ->setKey('port')
195 ->setType('int')
196 ->setDescription(pht('The port number of the interface.')),
200 public function getFieldValuesForConduit() {
201 return array(
202 'devicePHID' => $this->getDevicePHID(),
203 'networkPHID' => $this->getNetworkPHID(),
204 'address' => (string)$this->getAddress(),
205 'port' => (int)$this->getPort(),
209 public function getConduitSearchAttachments() {
210 return array();