src/Manager/CustomerManager.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Manager;
  3. use App\Entity\Customer;
  4. use App\Entity\Address;
  5. use App\Entity\CustomerLoginToken;
  6. use App\Entity\PriceGroup;
  7. class CustomerManager {
  8.     
  9.     private $session;
  10.     private $mailer;
  11.     private $em;
  12.     private $key "SDflkfoRE5459Z_çà#è79=/";
  13.     public function __construct(\Symfony\Component\HttpFoundation\Session\SessionInterface $session, \Doctrine\ORM\EntityManagerInterface $em, \App\Service\Mailer $mailer)
  14.     {
  15.         $this->session $session;
  16.         $this->em $em;
  17.         $this->mailer $mailer;
  18.         $this->cart null;
  19.     }
  20.     
  21.     public function getCustomer()
  22.     {
  23.         $id $this->session->get('customer_id');
  24.         if(!empty($id)){
  25.             $customer $this->em->getRepository('App:Customer')->find($id);
  26.             if(!empty($customer)){
  27.                 if($this->checkToken($customer)){
  28.                     return $customer;
  29.                 }
  30.             }
  31.         }
  32.         return false;
  33.     }
  34.     
  35.     public function getCustomerCountry()
  36.     {
  37.         $customer $this->getCustomer();
  38.         if($customer){
  39.             $defaultAddress $customer->getDefaultAddress();
  40.             if($defaultAddress){
  41.                 return $defaultAddress->getCountry();
  42.             }
  43.         }
  44.         return false;
  45.     }
  46.     
  47.     public function getPriceContext(): PriceGroup
  48.     {
  49.         $code $this->session->get('customer_price_context'PriceGroup::CODE_VIVOG);
  50.         return $this->em->getRepository(PriceGroup::class)->findOneByCode($code);
  51.     }
  52.     
  53.     public function setPriceContext(PriceGroup $priceGroup)
  54.     {
  55.         $customer $this->getCustomer();
  56.         if(empty($customer) || !$this->hasPriceContext($priceGroup)){
  57.             throw new \App\Exception\PriceGroupException();
  58.         }
  59.         $this->session->set('customer_price_context',$priceGroup->getCode());
  60.     }
  61.     
  62.     public function isExpert()
  63.     {
  64.         $currentPriceGroup $this->session->get('customer_price_context'false);
  65.         return $currentPriceGroup === PriceGroup::CODE_EXPERT;
  66.     }
  67.     
  68.     public function hasExpertContext()
  69.     {
  70.         $customer $this->getCustomer();
  71.         foreach($customer->getPriceGroups() as $priceGroup) {
  72.             if($priceGroup->getGroup()->getCode() == PriceGroup::CODE_EXPERT)
  73.                 return true;
  74.         }
  75.         return false;
  76.     }
  77.     
  78.     public function hasPriceContext(PriceGroup $priceGroup)
  79.     {
  80.         $customer $this->getCustomer();
  81.         foreach($customer->getPriceGroups() as $cPriceGroup) {
  82.             if($cPriceGroup->getGroup() == $priceGroup)
  83.                 return true;
  84.         }
  85.         return false;
  86.     }
  87.     
  88.     public function initCustomerSession(&$customer)
  89.     {
  90.         $this->updateLastLogin($customer);
  91.         $this->session->set('customer_id',$customer->getId());
  92.         $this->session->set('customer_email',$customer->getEmail());
  93.         $this->session->set('customer_token',$this->getToken($customer));
  94.     }
  95.     
  96.     protected function updateLastLogin(Customer $customer)
  97.     {
  98.         $customer->setLastLogin(new \DateTime);
  99.         $nbLogin $customer->getNbLogin()+1;
  100.         $customer->setNbLogin($nbLogin);
  101.         $this->em->persist($customer);
  102.         $this->em->flush();
  103.     }
  104.     
  105.     public function login($user,$pwd)
  106.     {
  107.         $customer $this->em->getRepository('App:Customer')->findOneBy(array(
  108.             'email'=>$user
  109.         ));
  110.         if(!empty($customer)){
  111.             if($this->checkPassword($pwd$customer->getPassword())){
  112.                 $priceGroups $customer->getPriceGroups();
  113.                 if(count($priceGroups) == 0) {
  114.                     throw new \App\Exception\PriceGroupException();
  115.                 }
  116.                 $this->initCustomerSession($customer);
  117.                 return $customer;
  118.             }
  119.         }
  120.         return false;
  121.     }
  122.     
  123.     
  124.     
  125.     public function checkLogin($user,$pwd)
  126.     {
  127.         $customer $this->em->getRepository('App:Customer')->findOneBy(array(
  128.             'email'=>$user
  129.         ));
  130.         if(!empty($customer)){
  131.             return $this->checkPassword($pwd$customer->getPassword());
  132.         }
  133.         return false;
  134.     }
  135.     
  136.     public function generateLoginToken(Customer $customer)
  137.     {
  138.         $code = \App\Helpers\Encoder::getRandomDigit(4);
  139.         $token = new CustomerLoginToken();
  140.         $token->setCustomer($customer);
  141.         $token->setToken($this->getLoginToken($code));
  142.     }
  143.     
  144.     protected function getLoginToken($code)
  145.     {
  146.         return md5($code.CustomerLoginToken::$KEY);
  147.     }
  148.     
  149.     public function checkCustomerLoginToken(Customer $customer$code)
  150.     {
  151.         $code = \App\Helpers\Encoder::getRandomDigit(4);
  152.         $token = new CustomerLoginToken();
  153.         $token->setCustomer($customer);
  154.         $token->setToken($this->getLoginToken($code));
  155.     }
  156.     
  157.     public function register($data$locale "fr")
  158.     {
  159.         $customer $this->em->getRepository('App:Customer')->findOneBy(array('email'=>$data['email']));
  160.         if(!empty($customer)){
  161.             $this->session->getFlashBag()->add('error''Un compte utilisateur utilise déjà cette adresse email : '.$data['email']);
  162.             return false;
  163.         }
  164.         $defaultGroup $this->em->getRepository('App:CustomerGroup')->find(1);
  165.         $customer = new \App\Entity\Customer();
  166.         $customer->setGroup($defaultGroup);
  167.         $customer->setEmailAddress($data['email']);
  168.         $customer->setGender($data['address']->getGender());
  169.         $customer->setFirstname($data['address']->getFirstName());
  170.         $customer->setLastname($data['address']->getLastName());
  171.         $customer->setTelephone($data['address']->getPhone());
  172.         $customer->setPassword($this->encryptPassword($data['plainPassword']));
  173. //        $customer->setNewsletter($data['newsletter']);
  174.         //$customer->setPassword(hex2bin($this->encryptPassword($data['plainPassword'])));
  175.         
  176.         $customer->setCompany($data['company']);
  177.         $customer->setSiret($data['siret']);
  178.         $customer->setTva($data['tva']);
  179.         $customer->setElevage($data['elevage']);
  180.         $customer->setMetier($data['metier']);
  181.         
  182.         $language $this->em->getRepository('App:Language')->findOneByCode($locale);
  183.         $customer->setLanguage($language);
  184.         $customer->setCreation(new \DateTime);
  185.         $customer->setStatus(Customer::STATUS_WAITING);
  186.         $this->em->persist($customer);
  187.         $this->em->flush();
  188.         
  189.         if(isset($data['address'])){
  190.             $address = new Address();
  191.             $address->setGender($data['address']->getGender());
  192.             $address->setFirstname($data['address']->getFirstName());
  193.             $address->setLastname($data['address']->getLastName());
  194.             $address->setCompany($data['address']->getCompany());
  195.             $address->setAddress1($data['address']->getAddress1());
  196.             $address->setAddress2($data['address']->getAddress2());
  197.             $address->setPostcode($data['address']->getPostcode());
  198.             $address->setCity($data['address']->getCity());
  199.             $address->setCountry($data['address']->getCountry());
  200.             $address->setPhone($data['address']->getPhone());
  201.             $address->setCustomer($customer);
  202.             $this->em->persist($address);
  203.             $customer->setDefaultAddress($address);
  204.             $this->em->persist($customer);
  205.             $this->em->flush();
  206.         }
  207. //        $this->mailer->sendAccountConfirmation($customer, $locale);
  208.         
  209.         $customerPrice = new \App\Entity\CustomerPriceGroup();
  210.         $customerPrice->setCustomer($customer);
  211.         if($data['expert']) {
  212.             $priceGroup $this->em->getRepository(PriceGroup::class)->findOneByCode(PriceGroup::CODE_EXPERT);
  213.             $customerPrice->setSapId($data['expertId']);
  214.         }else{
  215.             $priceGroup $this->em->getRepository(PriceGroup::class)->findOneByCode(PriceGroup::CODE_VIVOG);
  216.         }
  217.         $customerPrice->setGroup($priceGroup);
  218.         $this->em->persist($customerPrice);
  219.         $this->em->flush();
  220.         $this->em->refresh($customer);
  221. //        $this->setPriceContext($priceGroup);
  222.         
  223.         $this->mailer->notifyRegistration($customer);
  224.         $this->initCustomerSession($customer);
  225.         return $customer;
  226.     }
  227.     
  228.     public function delete(Customer $customer)
  229.     {
  230.         try{
  231.             $this->em->remove($customer);
  232.             $this->em->flush();
  233.         } catch (\Exception $ex) {
  234.             return false;
  235.         }
  236.         return true;
  237.     }
  238.     
  239.     public function checkPassword(string $plainstring $encrypted)
  240.     {
  241.         if (!empty($plain) && !empty($encrypted)) {
  242.             $stack explode(':'$encrypted);
  243.             if (sizeof($stack) != 2) return false;
  244.             return md5($stack[1] . $plain) == $stack[0];
  245.         }
  246.         return false;
  247.     }
  248.     
  249.     public function encryptPassword($plain)
  250.     {
  251.         $salt substr(md5($plain), 02);
  252.         return md5($salt.$plain).':'.$salt;
  253.     }
  254.     
  255.     public function renewPassword($email$locale)
  256.     {
  257.         $customer $this->em->getRepository('App:Customer')->findOneByEmail($email);
  258.         if(empty($customer)){
  259.             return "Aucun compte client ne correspond à l'email saisi.";
  260.         }else{
  261.             $pwd $this->generatePassword();
  262.             $customer->setPassword($this->encryptPassword($pwd));
  263.             try{
  264.                 $this->mailer->sendPassword($customer,$pwd,$locale);
  265.                 $this->em->persist($customer);
  266.                 $this->em->flush();
  267.                 return true;
  268.             } catch (Exception $ex) {
  269.                 return "Erreur lors de la génération du mot de passe";
  270.                 return $ex->getMessage();
  271.             }
  272.         }
  273.         return  false;
  274.     }
  275.     
  276.     private function generatePassword($length=8)
  277.     {
  278.         //$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  279.         $chars 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  280.         $count mb_strlen($chars);
  281.         for ($i 0$result ''$i $length$i++) {
  282.             $index rand(0$count 1);
  283.             $result .= mb_substr($chars$index1);
  284.         }
  285.         return $result;
  286.     }
  287.     
  288.     public function changePassword(Customer $customer$currentPwd$newPwd)
  289.     {
  290.         if($this->checkPassword($currentPwd$customer->getPassword())){
  291.             $customer->setPassword($this->encryptPassword($newPwd));
  292.             $this->em->persist($customer);
  293.             $this->em->flush();
  294.             return true;
  295.         }
  296.         return "Mot de passe incorrect";
  297.     }
  298.     
  299.     public function logout()
  300.     {
  301. //        $this->log('déconnexion');
  302.         $this->session->remove('customer_id');
  303.         $this->session->remove('customer_fullname');
  304.         $this->session->remove('customer_email');
  305.         $this->session->remove('customer_token');
  306.     }
  307.     
  308.     public function isLogged() {
  309.         $customer $this->getCustomer();
  310.         return $customer != false;
  311.     }
  312.     
  313.     public function isElite() {
  314.         $customer $this->getCustomer();
  315.         return $customer && $customer->hasEliteDiscount();
  316.     }
  317.     
  318.     public function log($action='')
  319.     {
  320.         $customer $this->getCustomer();
  321.         if($customer===false)
  322.             return;
  323.         $log = new \App\Entity\CustomerLog();
  324.         $log->setCustomer($customer);
  325.         $log->setDate(new \DateTime());
  326.         $log->setAction($action);
  327.         $this->em->persist($log);
  328.         $this->em->flush();
  329.     }
  330.     
  331.     private function getToken(\App\Entity\Customer $customer)
  332.     {
  333.         return md5($customer->getId()."-".$customer->getEmail()."-".$this->key);
  334.     }
  335.     
  336.     private function checkToken(\App\Entity\Customer $customer)
  337.     {
  338.         return $this->session->get('customer_token') === $this->getToken($customer);
  339.     }
  340.     
  341.     /**
  342.      * Colonne 1 = Code client Synoptic
  343.      * Colonne 2 = Nom réduit
  344.      * Colonne 3 = Raison sociale
  345.      * Colonne 4 = Code tarif du client
  346.      * Colonne 5 = Code remise du client
  347.      */
  348.     public function importDiscountFromCsv($csvFile) {
  349.         if(!file_exists($csvFile))
  350.             throw new \Exception('File not found');
  351.         if (($handle fopen($csvFile"r")) !== FALSE) {
  352.             $row 0;
  353.             while (($data fgetcsv($handlenull";")) !== FALSE) {
  354.                 $customers $this->em->getRepository('App:Customer')->findBySynopticId($data[0]);
  355.                 foreach($customers as $customer){
  356.                     $customer->setPriceCode($data[3]);
  357.                     $customer->setDiscountCode($data[4]);
  358.                     $this->em->persist($customer);
  359.                     $this->em->flush();
  360.                 }
  361.             }
  362.             fclose($handle);
  363.         }
  364.     }
  365.     
  366.     public function canSeePrices() {
  367.         if(!$this->isLogged())
  368.             return false;
  369.         $customer $this->getCustomer();
  370.         $status $customer->getStatus();
  371.         return empty($status) || $status == Customer::STATUS_VALIDATED;
  372.     }
  373.     
  374.     public function validate(Customer $customerbool $test) {
  375.         $value $test Customer::STATUS_VALIDATED Customer::STATUS_BLOCKED;
  376.         $customer->setStatus($value);
  377.         $this->em->persist($customer);
  378.         $this->em->flush();
  379.         $this->em->refresh($customer);
  380.         $this->mailer->sendCustomerValidation($customer);
  381.     }
  382.     
  383.     public function anonymize(Customer $customer) {
  384.         $customer->setFirstname('anonyme');
  385.         $customer->setLastname('anonyme');
  386.         $customer->setEmailAddress(uniqid().'@vivog.fr');
  387.         $customer->setCompany('anonyme');
  388.         $this->em->persist($customer);
  389.         $this->em->flush();
  390.     }
  391.     
  392.     
  393. }