Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / auth / xaction / PhabricatorAuthContactNumberNumberTransaction.php
blob88d9d4bffcd8720448d6b726eace0f388e613f8e
1 <?php
3 final class PhabricatorAuthContactNumberNumberTransaction
4 extends PhabricatorAuthContactNumberTransactionType {
6 const TRANSACTIONTYPE = 'number';
8 public function generateOldValue($object) {
9 return $object->getContactNumber();
12 public function generateNewValue($object, $value) {
13 $number = new PhabricatorPhoneNumber($value);
14 return $number->toE164();
17 public function applyInternalEffects($object, $value) {
18 $object->setContactNumber($value);
21 public function getTitle() {
22 $old = $this->getOldValue();
23 $new = $this->getNewValue();
25 return pht(
26 '%s changed this contact number from %s to %s.',
27 $this->renderAuthor(),
28 $this->renderOldValue(),
29 $this->renderNewValue());
32 public function validateTransactions($object, array $xactions) {
33 $errors = array();
35 $current_value = $object->getContactNumber();
36 if ($this->isEmptyTextTransaction($current_value, $xactions)) {
37 $errors[] = $this->newRequiredError(
38 pht('Contact numbers must have a contact number.'));
39 return $errors;
42 $max_length = $object->getColumnMaximumByteLength('contactNumber');
43 foreach ($xactions as $xaction) {
44 $new_value = $xaction->getNewValue();
45 $new_length = strlen($new_value);
46 if ($new_length > $max_length) {
47 $errors[] = $this->newInvalidError(
48 pht(
49 'Contact numbers can not be longer than %s characters.',
50 new PhutilNumber($max_length)),
51 $xaction);
52 continue;
55 try {
56 new PhabricatorPhoneNumber($new_value);
57 } catch (Exception $ex) {
58 $errors[] = $this->newInvalidError(
59 pht(
60 'Contact number is invalid: %s',
61 $ex->getMessage()),
62 $xaction);
63 continue;
66 $new_value = $this->generateNewValue($object, $new_value);
68 $unique_key = id(clone $object)
69 ->setContactNumber($new_value)
70 ->newUniqueKey();
72 $other = id(new PhabricatorAuthContactNumberQuery())
73 ->setViewer(PhabricatorUser::getOmnipotentUser())
74 ->withUniqueKeys(array($unique_key))
75 ->executeOne();
77 if ($other) {
78 if ($other->getID() !== $object->getID()) {
79 $errors[] = $this->newInvalidError(
80 pht('Contact number is already in use.'),
81 $xaction);
82 continue;
86 $mfa_error = $this->newContactNumberMFAError($object, $xaction);
87 if ($mfa_error) {
88 $errors[] = $mfa_error;
89 continue;
93 return $errors;