Differential D19988 Diff 47721 src/applications/auth/xaction/PhabricatorAuthContactNumberNumberTransaction.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/auth/xaction/PhabricatorAuthContactNumberNumberTransaction.php
- This file was added.
<?php | |||||
final class PhabricatorAuthContactNumberNumberTransaction | |||||
extends PhabricatorAuthContactNumberTransactionType { | |||||
const TRANSACTIONTYPE = 'number'; | |||||
public function generateOldValue($object) { | |||||
return $object->getContactNumber(); | |||||
} | |||||
public function generateNewValue($object, $value) { | |||||
$number = new PhabricatorPhoneNumber($value); | |||||
return $number->toE164(); | |||||
} | |||||
public function applyInternalEffects($object, $value) { | |||||
$object->setContactNumber($value); | |||||
} | |||||
public function getTitle() { | |||||
$old = $this->getOldValue(); | |||||
$new = $this->getNewValue(); | |||||
return pht( | |||||
'%s changed this contact number from %s to %s.', | |||||
$this->renderAuthor(), | |||||
$this->renderOldValue(), | |||||
$this->renderNewValue()); | |||||
} | |||||
amckinley: Shouldn't this have the standard `if (!$old) { } else if (!$new) { } else { }`logic? | |||||
Done Inline ActionsYou can't remove the contact number (since the value is required), and the !$old transaction will always be a "create" transaction that doesn't ever render. epriestley: You can't remove the contact number (since the value is required), and the `!$old` transaction… | |||||
public function validateTransactions($object, array $xactions) { | |||||
$errors = array(); | |||||
$current_value = $object->getContactNumber(); | |||||
if ($this->isEmptyTextTransaction($current_value, $xactions)) { | |||||
$errors[] = $this->newRequiredError( | |||||
pht('Contact numbers must have a contact number.')); | |||||
return $errors; | |||||
} | |||||
$max_length = $object->getColumnMaximumByteLength('contactNumber'); | |||||
foreach ($xactions as $xaction) { | |||||
$new_value = $xaction->getNewValue(); | |||||
$new_length = strlen($new_value); | |||||
if ($new_length > $max_length) { | |||||
$errors[] = $this->newInvalidError( | |||||
pht( | |||||
'Contact numbers can not be longer than %s characters.', | |||||
new PhutilNumber($max_length)), | |||||
$xaction); | |||||
continue; | |||||
} | |||||
try { | |||||
new PhabricatorPhoneNumber($new_value); | |||||
} catch (Exception $ex) { | |||||
$errors[] = $this->newInvalidError( | |||||
pht( | |||||
'Contact number is invalid: %s', | |||||
$ex->getMessage()), | |||||
$xaction); | |||||
continue; | |||||
} | |||||
$normal_value = $this->generateNewValue($object, $new_value); | |||||
$other = id(new PhabricatorAuthContactNumberQuery()) | |||||
->setViewer(PhabricatorUser::getOmnipotentUser()) | |||||
->withContactNumbers(array($normal_value)) | |||||
->executeOne(); | |||||
if ($other) { | |||||
if ($other->getID() !== $object->getID()) { | |||||
$errors[] = $this->newInvalidError( | |||||
pht('Contact number is already in use.'), | |||||
$xaction); | |||||
continue; | |||||
} | |||||
} | |||||
} | |||||
return $errors; | |||||
} | |||||
} |
Shouldn't this have the standard if (!$old) { } else if (!$new) { } else { }logic?