/** * - Insère un compte gratuit. * - Insère l'utilisateur qui va posséder ce compte. * - Gestion des exceptions MongoCursor: {@link http://www.php.net/manual/en/class.mongocursorexception.php} * - Gestion des erreurs, avec notamment: * Annulation de l'insertion du compte gratuit si l'insertion de l'utilisateur a échoué * @author Alban Truc * @param string $lastName * @param string $firstName * @param string $email * @param string $password * @param string $geolocation * @since 02/2014 * @return bool TRUE si l'insertion a réussi, FALSE sinon */ public function addFreeUser($lastName, $firstName, $email, $password, $geolocation) { $accountId = new MongoId(); $userId = new MongoId(); //@link http://www.php.net/manual/en/class.mongodate.php $time = time(); $end = $time + 30 * 24 * 60 * 60; // + 30 jours //Caractéristiques du compte gratuit $account = array('_id' => $accountId, 'state' => (int) 1, 'idUser' => $userId, 'idRefPlan' => new MongoId('52eb5e743263d8b6a4395df0'), 'storage' => (int) 0, 'ratio' => (int) 0, 'startDate' => new MongoDate($time), 'endDate' => new MongoDate($end)); $isAccountAdded = $this->accountPdoManager->create($account); if ($isAccountAdded == TRUE) { //Caractéristiques de l'utilisateur $user = array('_id' => $userId, 'state' => (int) 0, 'isAdmin' => false, 'idCurrentAccount' => $accountId, 'lastName' => $lastName, 'firstName' => $firstName, 'password' => $password, 'email' => $email, 'geolocation' => $geolocation, 'apiKey' => $this->generateGUID()); $info = self::create($user); if ($info != TRUE) { //annuler l'insertion de l'account $removeInfo = $this->accountPdoManager->remove($account); if ($removeInfo == TRUE) { $info['error'] .= 'The account created for this user has been removed successfully.'; } else { $info['error'] .= 'The account created for this user has not been removed successfully: ' . $removeInfo; } //contient le détail de l'erreur de suppression } return $info; } else { return $isAccountAdded; } //Message d'erreur approprié }
$userManager = new UserPdoManager(); $accountManager = new AccountPdoManager(); $planManager = new RefPlanPdoManager(); //Verifie la disponibilité de l'adresse mail if ($userManager->checkEmailAvailability($email) != FALSE) { $accountId = new MongoId(); $userId = new MongoId(); //crypte le password $password = $userManager->encrypt($password); //@link http://www.php.net/manual/en/class.mongodate.php $time = time(); $end = $time + 30 * 24 * 60 * 60; // + 30 jours //info compte $account = array('_id' => $accountId, 'state' => new MongoInt32($state), 'idUser' => $userId, 'idRefPlan' => new MongoId($plan), 'storage' => (int) 0, 'ratio' => (int) 0, 'startDate' => new MongoDate($time), 'endDate' => new MongoDate($end)); $isAccountAdded = $accountManager->create($account); //Si aucun pb apres ajout du compte, ajoute l'user, sinon suppresion de user if ($isAccountAdded == TRUE) { //infos user $user = array('_id' => $userId, 'isAdmin' => $isAdmin, 'state' => new MongoInt32($state), 'idCurrentAccount' => $accountId, 'firstName' => _sanitize($firstname), 'lastName' => _sanitize($lastname), 'password' => $password, 'email' => $email, 'geolocation' => $geo, 'apiKey' => $userManager->generateGUID()); $isUserAdded = $userManager->create($user); if ($isUserAdded != TRUE) { //annule l'insertion de l'account $removeAccount = $accountManager->remove($account); if ($removeAccount == TRUE) { $isUserAdded['error'] .= 'The account created for this user has been removed successfully.'; } else { $isUserAdded['error'] .= 'The account created for this user has not been removed successfully: ' . $removeAccount; } //contient le détail de l'erreur de suppression } else {
$paymentPdoManager->create($payment); /* * Récupère le compte actuel */ $criteria = array('state' => (int) 1, 'idUser' => new MongoId($custom[0])); $updateAccount = array('$set' => array('state' => new MongoInt32(0))); $account = $accountPdoManager->findAndModify($criteria, $updateAccount, NULL, array('new' => TRUE)); /*Si le compte existe*/ if ($account instanceof Account) { $time = time(); $end = $time + 30 * 24 * 60 * 60; // + 30 jours //Récupère l'id de l'user qui vient d'acheter, le plan qu'il vient d'acheter, son espace //de stockage, son ratio. Met à jour sa date d'achat et de fin d'abonnement $newAccount = array('_id' => new MongoId(), 'state' => (int) 1, 'idUser' => new MongoId($custom[0]), 'idRefPlan' => new MongoId($custom[1]), 'storage' => $account->getStorage(), 'ratio' => $account->getRatio(), 'startDate' => new MongoDate($time), 'endDate' => new MongoDate($end)); $accountPdoManager->create($newAccount); //critères de recherche $searchQuery = array('_id' => new MongoId($custom[0])); //les modifications à réaliser //en mettant un $set, on change uniquement le champ voulu // sans le $set, on ferais un delete puis un insert $updateCriteria = array('$set' => array('idCurrentAccount' => $newAccount['_id'])); //mise a jour de l'idCurrentAccount de l'user qui vient d'acheter $updateUser = $userPdoManager->findAndModify($searchQuery, $updateCriteria, NULL, array('new' => TRUE)); } // 1 FindAndModify pour récup le compte actuel: state à 0 + option récup la version modifiée OK // 2 Insére un nouveau compte avec storage et ratio de l'ancien compte et Id du nouveau refPlan OK // 3 Update de l'idCurrentAccount du user OK // SI marche, affiche de message, sinon contacter le service technique } }
var_dump($account); echo '____------<br />'; echo '____recupere uniquement le champ state (et id qui est obligatoire) APRES modification'; $updatedAccount = $accountPdoManager->findAndModify($searchQuery, $updateCriteria, $fields, $options); var_dump($updatedAccount); echo '____------<br />'; $fields = NULL; $options = array('remove' => true); //supprimera au lieu de faire un update echo '____account que l\'on supprime'; $result = $accountPdoManager->findAndModify($searchQuery, $updateCriteria, $fields, $options); var_dump($result); echo '____---------------------<br />'; $account->setState(0); echo '____Reinsertion de l\'objet precedemment supprime'; $add = $accountPdoManager->create($account); var_dump($add); echo '----------------------------------------<br />'; echo 'Utilisation du create, affiche true en cas de succes'; $newInsert = array('test' => TRUE); $createResult = $accountPdoManager->create($newInsert); var_dump($createResult); echo '----------------------------------------<br />'; echo 'Utilisation de l\'update, affiche true en cas de succes'; $criteria = array('test' => TRUE); $update = array('$set' => array('number' => (int) 500)); $updateResult = $accountPdoManager->update($criteria, $update); var_dump($updateResult); // true si l'update a réussi echo '----------------------------------------<br />'; echo 'Utilisation du remove, affiche true en cas de succes';