/**
  * Picks the profile with given number.
  *
  * @param string $number
  *
  * @return array
  *
  * @throws NonExistentNumberApiException
  */
 public function pick($number)
 {
     $number = PhoneNumberFixer::fix($number);
     $profile = $this->connectToStorageInternalWorker->connect()->findOne(['number' => $number], ['_id' => 0]);
     if (!$profile) {
         throw new NonExistentNumberApiException();
     }
     return $profile;
 }
 /**
  * Validates given mobile and alias.
  *
  * @param string $mobile
  * @param string $alias
  *
  * @throws InvalidMobileInternalException
  * @throws BlankAliasInternalException
  * @throws ExistentMobileInternalException
  */
 public function validate($mobile, $alias)
 {
     try {
         PhoneNumberFixer::fix($mobile);
     } catch (\InvalidArgumentException $e) {
         throw new InvalidMobileInternalException();
     }
     if ($alias === '') {
         throw new BlankAliasInternalException();
     }
     $subscription = $this->connectToStorageInternalWorker->connect()->findOne(['mobile' => $mobile]);
     if ($subscription) {
         throw new ExistentMobileInternalException();
     }
 }
 /**
  * Creates a profile.
  *
  * @param string $uniqueness
  * @param string $number
  *
  * @throws InvalidNumberSharedException
  * @throws ExistentNumberSharedException
  * @throws ExistentUniquenessSharedException
  * @throws \MongoCursorException
  */
 public function create($uniqueness, $number)
 {
     try {
         $number = PhoneNumberFixer::fix($number);
     } catch (\InvalidArgumentException $e) {
         throw new InvalidNumberSharedException();
     }
     try {
         $this->connectToStorageInternalWorker->connect()->insert(['uniqueness' => $uniqueness, 'number' => $number]);
     } catch (\MongoCursorException $e) {
         if (11000 == $e->getCode()) {
             if (strpos($e->getMessage(), '$number_1') !== false) {
                 throw new ExistentNumberSharedException();
             }
             throw new ExistentUniquenessSharedException();
         }
         throw $e;
     }
 }
 /**
  * @param string   $mobile
  * @param string   $uniqueness
  * @param string   $alias
  * @param string[] $topics
  * @param string   $resellPackage
  *
  * @throws NonExistentUniquenessApiException
  * @throws InvalidMobileApiException
  * @throws BlankAliasApiException
  * @throws ExistentMobileApiException
  * @throws NoTopicsApiException
  * @throws NonExistentTopicApiException
  * @throws NoResellPackageApiException
  * @throws NonExistentResellPackageApiException
  * @throws TrialNotAcceptedApiException
  * @throws InsufficientBalanceApiException
  */
 public function create($mobile, $uniqueness, $alias, $topics, $resellPackage)
 {
     try {
         $this->pickProfileApiWorker->pick($uniqueness);
     } catch (NonExistentUniquenessApiException $e) {
         throw $e;
     }
     try {
         $this->validateMobileAndAliasInternalWorker->validate($mobile, $alias);
     } catch (InvalidMobileInternalException $e) {
         throw new InvalidMobileApiException();
     } catch (BlankAliasInternalException $e) {
         throw new BlankAliasApiException();
     } catch (ExistentMobileInternalException $e) {
         throw new ExistentMobileApiException();
     }
     try {
         $this->validateTopicsInternalWorker->validate($topics);
     } catch (NoTopicsInternalException $e) {
         throw new NoTopicsApiException();
     } catch (NonExistentTopicInternalException $e) {
         throw new NonExistentTopicApiException();
     }
     $mobile = PhoneNumberFixer::fix($mobile);
     if (!$resellPackage) {
         throw new NoResellPackageApiException();
     }
     try {
         $resellPackage = $this->pickResellPackageInternalWorker->pick($resellPackage);
     } catch (NonExistentResellPackageInternalException $e) {
         throw new NonExistentResellPackageApiException();
     }
     if ($resellPackage['price'] == 0) {
         try {
             $this->createTrialSubscriptionInternalWorker->create($mobile, $uniqueness, $alias, $topics, 10);
         } catch (TrialNotAcceptedInternalException $e) {
             throw new TrialNotAcceptedApiException();
         }
     } else {
         try {
             $this->createPaidSubscriptionInternalWorker->create($mobile, $uniqueness, $alias, $topics, $resellPackage);
         } catch (InsufficientBalanceInternalException $e) {
             throw new InsufficientBalanceApiException();
         }
     }
 }