Skip to content

Instantly share code, notes, and snippets.

@nask0
Created April 15, 2016 09:13
Show Gist options
  • Save nask0/7e517e01f01ac327c1e7ab6ff2f67538 to your computer and use it in GitHub Desktop.
Save nask0/7e517e01f01ac327c1e7ab6ff2f67538 to your computer and use it in GitHub Desktop.
<?php
namespace CloseContacts\Lib\DataServices;
use \CloseContacts\Lib\Utilities\Date;
use \CloseContacts\Models\UserVerifications;
use \CloseContacts\Models\Verifications as VerificationModel;
use \CloseContacts\Models\SmsMessagesSent as SmsMessagesSentModel;
class Verifications extends DataServiceAbstract
{
public function saveVerificationSMS( $userId, $deviceId, $verificationCode, $smsData )
{
try {
$tx = $this->_getTransaction();
// $tx->begin();
$message = new SmsMessagesSentModel();
$message->setTransaction( $tx );
$message->setUserId( $userId );
$message->setSmsId( $smsData['apiMessageId'] );
$message->setSmsText( $smsData['text'] );
$message->setSmsRecipient( $smsData['to'] );
$message->setSmsIsTest( 0 );
$message->setSmsIsSent( (int) $smsData['accepted'] );
$message->setSmsGateway( 'Clickatell' );
$message->setSmsCreatedAt( Date::getUTCTimestamp() );
$message->setSmsUpdatedAt( Date::getUTCTimestamp() );
if( false === $message->save() ) {
$tx->rollback( 'Unable to save verification SMS' );
}
$verification = new VerificationModel();
$verification->setTransaction( $tx );
$verification->setType( VerificationModel::VERIFICATION_TYPE_SMS );
$verification->setCode( (string) $verificationCode );
$verification->setAttempts( 0 );
if( true === (bool) $smsData['accepted'] ) {
$verification->setIsValid( VerificationModel::VERIFICATION_VALID );
} else {
$verification->setIsValid( VerificationModel::VERIFICATION_NOT_VALID );
}
$verification->setIsConfirmed( VerificationModel::VERIFICATION_NOT_CONFIRMED );
$verification->setExpiresAt( Date::getUTCTimestampFromTs( time() + 1800 ) ); // 30 min
$verification->setCreatedAt( Date::getUTCTimestamp() );
$verification->setUpdatedAt( Date::getUTCTimestamp() );
if( false === $verification->save() ) {
$tx->rollback( 'Unable to save verification' );
}
$userVerifications = new UserVerifications();
$userVerifications->setTransaction($tx);
$userVerifications->setUserId( $userId )
->setDeviceId((int) $deviceId )
->setVerificationId( $verification->getId() );
if( false === $userVerifications->save() ) {
$tx->rollback( 'Unable to save user verification' );
}
$tx->commit();
return true;
} catch( \Exception $e ) {
$this->_setValidationError( 'unexpected', 'Unable to save verification SMS (' . $e->getMessage() . ')' );
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment