Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/doorkeeper/engine/DoorkeeperImportEngine.php
12256 views
1
<?php
2
3
final class DoorkeeperImportEngine extends Phobject {
4
5
private $viewer;
6
private $refs = array();
7
private $phids = array();
8
private $localOnly;
9
private $throwOnMissingLink;
10
private $context = array();
11
private $timeout;
12
13
public function setViewer(PhabricatorUser $viewer) {
14
$this->viewer = $viewer;
15
return $this;
16
}
17
18
public function getViewer() {
19
return $this->viewer;
20
}
21
22
public function setRefs(array $refs) {
23
assert_instances_of($refs, 'DoorkeeperObjectRef');
24
$this->refs = $refs;
25
return $this;
26
}
27
28
public function getRefs() {
29
return $this->refs;
30
}
31
32
public function withPHIDs(array $phids) {
33
$this->phids = $phids;
34
return $this;
35
}
36
37
public function needLocalOnly($local_only) {
38
$this->localOnly = $local_only;
39
return $this;
40
}
41
42
public function setContextProperty($key, $value) {
43
$this->context[$key] = $value;
44
return $this;
45
}
46
47
public function setTimeout($timeout) {
48
$this->timeout = $timeout;
49
return $this;
50
}
51
52
public function getTimeout() {
53
return $this->timeout;
54
}
55
56
/**
57
* Configure behavior if remote refs can not be retrieved because an
58
* authentication link is missing.
59
*/
60
public function setThrowOnMissingLink($throw) {
61
$this->throwOnMissingLink = $throw;
62
return $this;
63
}
64
65
public function execute() {
66
$refs = $this->getRefs();
67
$viewer = $this->getViewer();
68
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());
82
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;
88
}
89
$ref->attachExternalObject($xobj);
90
}
91
}
92
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;
102
}
103
}
104
105
if (!$this->localOnly) {
106
$bridges = id(new PhutilClassMapQuery())
107
->setAncestorClass('DoorkeeperBridge')
108
->setFilterMethod('isEnabled')
109
->execute();
110
111
$timeout = $this->getTimeout();
112
foreach ($bridges as $key => $bridge) {
113
$bridge
114
->setViewer($viewer)
115
->setThrowOnMissingLink($this->throwOnMissingLink)
116
->setContext($this->context);
117
118
if ($timeout !== null) {
119
$bridge->setTimeout($timeout);
120
}
121
}
122
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]);
130
}
131
}
132
if ($bridge_refs) {
133
$bridge->pullRefs($bridge_refs);
134
}
135
}
136
}
137
138
return $refs;
139
}
140
141
public function executeOne() {
142
return head($this->execute());
143
}
144
145
}
146
147