Path: blob/master/src/applications/doorkeeper/engine/DoorkeeperImportEngine.php
12256 views
<?php12final class DoorkeeperImportEngine extends Phobject {34private $viewer;5private $refs = array();6private $phids = array();7private $localOnly;8private $throwOnMissingLink;9private $context = array();10private $timeout;1112public function setViewer(PhabricatorUser $viewer) {13$this->viewer = $viewer;14return $this;15}1617public function getViewer() {18return $this->viewer;19}2021public function setRefs(array $refs) {22assert_instances_of($refs, 'DoorkeeperObjectRef');23$this->refs = $refs;24return $this;25}2627public function getRefs() {28return $this->refs;29}3031public function withPHIDs(array $phids) {32$this->phids = $phids;33return $this;34}3536public function needLocalOnly($local_only) {37$this->localOnly = $local_only;38return $this;39}4041public function setContextProperty($key, $value) {42$this->context[$key] = $value;43return $this;44}4546public function setTimeout($timeout) {47$this->timeout = $timeout;48return $this;49}5051public function getTimeout() {52return $this->timeout;53}5455/**56* Configure behavior if remote refs can not be retrieved because an57* authentication link is missing.58*/59public function setThrowOnMissingLink($throw) {60$this->throwOnMissingLink = $throw;61return $this;62}6364public function execute() {65$refs = $this->getRefs();66$viewer = $this->getViewer();6768$keys = mpull($refs, 'getObjectKey');69if ($keys) {70$xobjs = id(new DoorkeeperExternalObjectQuery())71->setViewer($viewer)72->withObjectKeys($keys)73->execute();74$xobjs = mpull($xobjs, null, 'getObjectKey');75foreach ($refs as $ref) {76$xobj = idx($xobjs, $ref->getObjectKey());77if (!$xobj) {78$xobj = $ref79->newExternalObject()80->setImporterPHID($viewer->getPHID());8182// NOTE: Fill the new external object into the object map, so we'll83// reference the same external object if more than one ref is the84// same. This prevents issues later where we double-populate85// external objects when handed duplicate refs.86$xobjs[$ref->getObjectKey()] = $xobj;87}88$ref->attachExternalObject($xobj);89}90}9192if ($this->phids) {93$xobjs = id(new DoorkeeperExternalObjectQuery())94->setViewer($viewer)95->withPHIDs($this->phids)96->execute();97foreach ($xobjs as $xobj) {98$ref = $xobj->getRef();99$ref->attachExternalObject($xobj);100$refs[$ref->getObjectKey()] = $ref;101}102}103104if (!$this->localOnly) {105$bridges = id(new PhutilClassMapQuery())106->setAncestorClass('DoorkeeperBridge')107->setFilterMethod('isEnabled')108->execute();109110$timeout = $this->getTimeout();111foreach ($bridges as $key => $bridge) {112$bridge113->setViewer($viewer)114->setThrowOnMissingLink($this->throwOnMissingLink)115->setContext($this->context);116117if ($timeout !== null) {118$bridge->setTimeout($timeout);119}120}121122$working_set = $refs;123foreach ($bridges as $bridge) {124$bridge_refs = array();125foreach ($working_set as $key => $ref) {126if ($bridge->canPullRef($ref)) {127$bridge_refs[$key] = $ref;128unset($working_set[$key]);129}130}131if ($bridge_refs) {132$bridge->pullRefs($bridge_refs);133}134}135}136137return $refs;138}139140public function executeOne() {141return head($this->execute());142}143144}145146147