Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / doorkeeper / engine / DoorkeeperImportEngine.php
blob02642cb99428a281b622ce12cd2e8deaebeed731
1 <?php
3 final class DoorkeeperImportEngine extends Phobject {
5 private $viewer;
6 private $refs = array();
7 private $phids = array();
8 private $localOnly;
9 private $throwOnMissingLink;
10 private $context = array();
11 private $timeout;
13 public function setViewer(PhabricatorUser $viewer) {
14 $this->viewer = $viewer;
15 return $this;
18 public function getViewer() {
19 return $this->viewer;
22 public function setRefs(array $refs) {
23 assert_instances_of($refs, 'DoorkeeperObjectRef');
24 $this->refs = $refs;
25 return $this;
28 public function getRefs() {
29 return $this->refs;
32 public function withPHIDs(array $phids) {
33 $this->phids = $phids;
34 return $this;
37 public function needLocalOnly($local_only) {
38 $this->localOnly = $local_only;
39 return $this;
42 public function setContextProperty($key, $value) {
43 $this->context[$key] = $value;
44 return $this;
47 public function setTimeout($timeout) {
48 $this->timeout = $timeout;
49 return $this;
52 public function getTimeout() {
53 return $this->timeout;
56 /**
57 * Configure behavior if remote refs can not be retrieved because an
58 * authentication link is missing.
60 public function setThrowOnMissingLink($throw) {
61 $this->throwOnMissingLink = $throw;
62 return $this;
65 public function execute() {
66 $refs = $this->getRefs();
67 $viewer = $this->getViewer();
69 $keys = mpull($refs, 'getObjectKey');
70 if ($keys) {
71 $xobjs = id(new DoorkeeperExternalObjectQuery())
72 ->setViewer($viewer)
73 ->withObjectKeys($keys)
74 ->execute();
75 $xobjs = mpull($xobjs, null, 'getObjectKey');
76 foreach ($refs as $ref) {
77 $xobj = idx($xobjs, $ref->getObjectKey());
78 if (!$xobj) {
79 $xobj = $ref
80 ->newExternalObject()
81 ->setImporterPHID($viewer->getPHID());
83 // NOTE: Fill the new external object into the object map, so we'll
84 // reference the same external object if more than one ref is the
85 // same. This prevents issues later where we double-populate
86 // external objects when handed duplicate refs.
87 $xobjs[$ref->getObjectKey()] = $xobj;
89 $ref->attachExternalObject($xobj);
93 if ($this->phids) {
94 $xobjs = id(new DoorkeeperExternalObjectQuery())
95 ->setViewer($viewer)
96 ->withPHIDs($this->phids)
97 ->execute();
98 foreach ($xobjs as $xobj) {
99 $ref = $xobj->getRef();
100 $ref->attachExternalObject($xobj);
101 $refs[$ref->getObjectKey()] = $ref;
105 if (!$this->localOnly) {
106 $bridges = id(new PhutilClassMapQuery())
107 ->setAncestorClass('DoorkeeperBridge')
108 ->setFilterMethod('isEnabled')
109 ->execute();
111 $timeout = $this->getTimeout();
112 foreach ($bridges as $key => $bridge) {
113 $bridge
114 ->setViewer($viewer)
115 ->setThrowOnMissingLink($this->throwOnMissingLink)
116 ->setContext($this->context);
118 if ($timeout !== null) {
119 $bridge->setTimeout($timeout);
123 $working_set = $refs;
124 foreach ($bridges as $bridge) {
125 $bridge_refs = array();
126 foreach ($working_set as $key => $ref) {
127 if ($bridge->canPullRef($ref)) {
128 $bridge_refs[$key] = $ref;
129 unset($working_set[$key]);
132 if ($bridge_refs) {
133 $bridge->pullRefs($bridge_refs);
138 return $refs;
141 public function executeOne() {
142 return head($this->execute());