Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / diffusion / identity / DiffusionRepositoryIdentityEngine.php
blob8cd8a4bfa22bd4eb197f0262042fea76b06c28b8
1 <?php
3 final class DiffusionRepositoryIdentityEngine
4 extends Phobject {
6 private $viewer;
7 private $sourcePHID;
8 private $dryRun;
10 public function setViewer(PhabricatorUser $viewer) {
11 $this->viewer = $viewer;
12 return $this;
15 public function getViewer() {
16 return $this->viewer;
19 public function setSourcePHID($source_phid) {
20 $this->sourcePHID = $source_phid;
21 return $this;
24 public function getSourcePHID() {
25 if (!$this->sourcePHID) {
26 throw new PhutilInvalidStateException('setSourcePHID');
29 return $this->sourcePHID;
32 public function setDryRun($dry_run) {
33 $this->dryRun = $dry_run;
34 return $this;
37 public function getDryRun() {
38 return $this->dryRun;
41 public function newResolvedIdentity($raw_identity) {
42 $identity = $this->loadRawIdentity($raw_identity);
44 if (!$identity) {
45 $identity = $this->newIdentity($raw_identity);
48 return $this->updateIdentity($identity);
51 public function newUpdatedIdentity(PhabricatorRepositoryIdentity $identity) {
52 return $this->updateIdentity($identity);
55 private function loadRawIdentity($raw_identity) {
56 $viewer = $this->getViewer();
58 return id(new PhabricatorRepositoryIdentityQuery())
59 ->setViewer($viewer)
60 ->withIdentityNames(array($raw_identity))
61 ->executeOne();
64 private function newIdentity($raw_identity) {
65 $source_phid = $this->getSourcePHID();
67 return id(new PhabricatorRepositoryIdentity())
68 ->setAuthorPHID($source_phid)
69 ->setIdentityName($raw_identity);
72 private function resolveIdentity(PhabricatorRepositoryIdentity $identity) {
73 $raw_identity = $identity->getIdentityName();
75 return id(new DiffusionResolveUserQuery())
76 ->withName($raw_identity)
77 ->execute();
80 private function updateIdentity(PhabricatorRepositoryIdentity $identity) {
82 // If we're updating an identity and it has a manual user PHID associated
83 // with it but the user is no longer valid, remove the value. This likely
84 // corresponds to a user that was destroyed.
86 $assigned_phid = $identity->getManuallySetUserPHID();
87 $unassigned = DiffusionIdentityUnassignedDatasource::FUNCTION_TOKEN;
88 if ($assigned_phid && ($assigned_phid !== $unassigned)) {
89 $viewer = $this->getViewer();
90 $user = id(new PhabricatorPeopleQuery())
91 ->setViewer($viewer)
92 ->withPHIDs(array($assigned_phid))
93 ->executeOne();
94 if (!$user) {
95 $identity->setManuallySetUserPHID(null);
99 $resolved_phid = $this->resolveIdentity($identity);
101 $identity->setAutomaticGuessedUserPHID($resolved_phid);
103 if ($this->getDryRun()) {
104 $identity->makeEphemeral();
105 } else {
106 $identity->save();
109 return $identity;
112 public function didUpdateEmailAddress($raw_address) {
113 PhabricatorWorker::scheduleTask(
114 'PhabricatorRepositoryIdentityChangeWorker',
115 array(
116 'emailAddresses' => array($raw_address),