3 final class DoorkeeperImportEngine
extends Phobject
{
6 private $refs = array();
7 private $phids = array();
9 private $throwOnMissingLink;
10 private $context = array();
13 public function setViewer(PhabricatorUser
$viewer) {
14 $this->viewer
= $viewer;
18 public function getViewer() {
22 public function setRefs(array $refs) {
23 assert_instances_of($refs, 'DoorkeeperObjectRef');
28 public function getRefs() {
32 public function withPHIDs(array $phids) {
33 $this->phids
= $phids;
37 public function needLocalOnly($local_only) {
38 $this->localOnly
= $local_only;
42 public function setContextProperty($key, $value) {
43 $this->context
[$key] = $value;
47 public function setTimeout($timeout) {
48 $this->timeout
= $timeout;
52 public function getTimeout() {
53 return $this->timeout
;
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;
65 public function execute() {
66 $refs = $this->getRefs();
67 $viewer = $this->getViewer();
69 $keys = mpull($refs, 'getObjectKey');
71 $xobjs = id(new DoorkeeperExternalObjectQuery())
73 ->withObjectKeys($keys)
75 $xobjs = mpull($xobjs, null, 'getObjectKey');
76 foreach ($refs as $ref) {
77 $xobj = idx($xobjs, $ref->getObjectKey());
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);
94 $xobjs = id(new DoorkeeperExternalObjectQuery())
96 ->withPHIDs($this->phids
)
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')
111 $timeout = $this->getTimeout();
112 foreach ($bridges as $key => $bridge) {
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]);
133 $bridge->pullRefs($bridge_refs);
141 public function executeOne() {
142 return head($this->execute());