Browse Source

Adaptations newsletters + création MailjetSMS

develop
Guillaume 3 years ago
parent
commit
e680b175e3
8 changed files with 196 additions and 117 deletions
  1. +41
    -0
      ShopBundle/Form/DataTransformer/NewsletterSignupToBooleanTransformer.php
  2. +7
    -2
      ShopBundle/Manager/EntityManager.php
  3. +6
    -2
      ShopBundle/Model/Merchant.php
  4. +17
    -0
      ShopBundle/Model/Newsletter.php
  5. +6
    -5
      ShopBundle/Services/MailjetSMS.php
  6. +106
    -107
      ShopBundle/Services/UserUtils.php
  7. +13
    -0
      ShopBundle/Services/Utils.php
  8. +0
    -1
      ShopBundle/Twig/FrontendTwigExtension.php

+ 41
- 0
ShopBundle/Form/DataTransformer/NewsletterSignupToBooleanTransformer.php View File

@@ -0,0 +1,41 @@
<?php

namespace Lc\ShopBundle\Form\DataTransformer;

use Lc\ShopBundle\Context\ProductInterface;
use Doctrine\ORM\EntityManagerInterface;
use Lc\ShopBundle\Services\UtilsManager;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Security\Core\Security;

class NewsletterSignupToBooleanTransformer implements DataTransformerInterface
{
private $security ;
private $userUtils ;

public function __construct(Security $security, UtilsManager $utilsManager)
{
$this->security = $security;
$this->userUtils = $utilsManager->getUserUtils() ;
}

public function transform($newsletter)
{
$user = $this->security->getUser() ;

if($user && $newsletter) {
return $this->userUtils->isSubscribedToNewsletter($user, $newsletter) ;
}

return false ;
}

public function reverseTransform($isSubscribed)
{
// Impossible de retrouver la newsletter depuis sa valeur booléenne.
//throw new TransformationFailedException('Impossible de retrouver la newsletter depuis sa valeur booléenne.') ;

return false ;
}
}

+ 7
- 2
ShopBundle/Manager/EntityManager.php View File

@@ -47,9 +47,14 @@ class EntityManager
{

if ($this->security->getUser() === null) {
$entity->setUpdatedBy($this->getUserSystem());
$entity->setCreadtedBy($this->getUserSystem());
if(method_exists($entity, 'setUpdatedBy')) {
$entity->setUpdatedBy($this->getUserSystem());
}
if(method_exists($entity, 'setCreadtedBy')) {
$entity->setCreadtedBy($this->getUserSystem());
}
}

$this->persist($entity);
$this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::CREATE_EVENT);
return $this;

+ 6
- 2
ShopBundle/Model/Merchant.php View File

@@ -349,9 +349,13 @@ abstract class Merchant extends AbstractDocumentEntity
public function getNewsletter()
{
$newsletters = $this->getNewsletters() ;
if(isset($newsletters[0])) {
return $newsletters[0] ;

foreach($newsletters as $newsletter) {
if($newsletter->getIsMain()) {
return $newsletter ;
}
}

return false ;
}


+ 17
- 0
ShopBundle/Model/Newsletter.php View File

@@ -24,6 +24,11 @@ abstract class Newsletter extends AbstractDocumentEntity implements FilterMercha
*/
protected $users;

/**
* @ORM\Column(type="boolean", nullable=true)
*/
protected $isMain;

public function __toString()
{
return $this->getTitle() ;
@@ -73,4 +78,16 @@ abstract class Newsletter extends AbstractDocumentEntity implements FilterMercha

return $this;
}

public function getIsMain(): ?bool
{
return $this->isMain;
}

public function setIsMain(?bool $isMain): self
{
$this->isMain = $isMain;

return $this;
}
}

+ 6
- 5
ShopBundle/Services/MailjetSMS.php View File

@@ -19,7 +19,8 @@ class MailjetSMS

public function send($to, $message)
{
$token = $this->parameterBag->get('mailjet.token_sms') ;
$token = $this->parameterBag->get('mailjet.sms.token') ;
$from = $this->parameterBag->get('mailjet.sms.from') ;

if($token && strlen($token) > 0) {
$response = $this->client->request(
@@ -28,12 +29,12 @@ class MailjetSMS
[
'headers' => [
'Authorization' => 'Bearer '.$token,
'Content-Type' => 'application/json',
//'Content-Type' => 'application/json',
],
'body' => [
'Text' => $message,
'json' => [
'From' => $from,
'To' => $to,
'From' => 'MJPilot'
'Text' => $message,
]
]
);

+ 106
- 107
ShopBundle/Services/UserUtils.php View File

@@ -13,116 +13,115 @@ use Symfony\Component\Security\Core\Security;

class UserUtils
{
protected $parameterBag;
protected $em;
protected $utils;
protected $requestStack;
protected $visitorRepository;
protected $merchantUtils;
protected $cookieChecker;
protected $visitor;

public function __construct(ParameterBagInterface $parameterBag, EntityManagerInterface $em, Utils $utils,
RequestStack $requestStack, MerchantUtilsInterface $merchantUtils, CookieChecker $cookieChecker,
Security $security)
{
$this->em = $em;
$this->parameterBag = $parameterBag;
$this->utils = $utils;
$this->requestStack = $requestStack;
$this->visitorRepository = $this->em->getRepository($this->em->getClassMetadata(VisitorInterface::class)->getName());
$this->merchantUtils = $merchantUtils;
$this->cookieChecker = $cookieChecker;
$this->security = $security;
}

public function getCookieNameVisitor()
{
return $this->parameterBag->get('app.cookie_name_visitor');
}

public function cryptCookie($data)
{
return base64_encode($data);
}

public function decryptCookie($data)
{
return base64_decode($data);
}

public function setCookieVisitor($response, $cookie)
{
$response->headers->setCookie(Cookie::create($this->getCookieNameVisitor(), $this->cryptCookie($cookie), new \DateTime('+2 months'), '/', $this->utils->getCookieDomain()));
}

public function updateVisitorCookie($response)
{
$response->headers->setCookie(Cookie::create($this->getCookieNameVisitor(), $this->cryptCookie($this->getVisitorCurrent()->getCookie()), new \DateTime('+2 months'), '/', $this->utils->getCookieDomain()));

}

public function getVisitor($cookie)
{
if (!isset($this->visitor[$cookie])) {
$this->visitor[$cookie] = $this->visitorRepository->findOneBy(['cookie' => $cookie]);
protected $parameterBag;
protected $em;
protected $utils;
protected $requestStack;
protected $visitorRepository;
protected $merchantUtils;
protected $cookieChecker;
protected $visitor;

public function __construct(ParameterBagInterface $parameterBag, EntityManagerInterface $em, Utils $utils,
RequestStack $requestStack, MerchantUtilsInterface $merchantUtils, CookieChecker $cookieChecker,
Security $security)
{
$this->em = $em;
$this->parameterBag = $parameterBag;
$this->utils = $utils;
$this->requestStack = $requestStack;
$this->visitorRepository = $this->em->getRepository($this->em->getClassMetadata(VisitorInterface::class)->getName());
$this->merchantUtils = $merchantUtils;
$this->cookieChecker = $cookieChecker;
$this->security = $security;
}
return $this->visitor[$cookie];
}

public function getVisitorCurrent()
{
$cookie = $this->requestStack->getCurrentRequest()->cookies->get($this->getCookieNameVisitor());
return $this->getVisitor($cookie);
}

public function addVisitor($cookie, $ip)
{
$classVisitor = $this->em->getClassMetadata(VisitorInterface::class)->getName();
$visitor = new $classVisitor;

$visitor->setCookie($cookie);
$visitor->setIp($ip);
$visitor->setTotalVisit(1);
$visitor->setLastAccess(new \DateTime());

$this->em->persist($visitor);
$this->em->flush();
}

public function updateVisitor($visitor)
{
$totalVisit = $visitor->getTotalVisit() + 1;
$visitor->setTotalVisit($totalVisit);
$visitor->setLastAccess(new \DateTime());

$this->em->persist($visitor);
$this->em->flush();
}

public function setNewsletter($user, $newsletter, $subscribeNewsletter)
{
$currentMerchant = $this->merchantUtils->getMerchantCurrent() ;
$newsletters = $currentMerchant->getNewsletters() ;

foreach($newsletters as $newsletterMerchant) {
if($newsletterMerchant->getId() == $newsletter->getId()) {
if($subscribeNewsletter) {
$user->addNewsletter($newsletter) ;
}
else {
$user->removeNewsletter($newsletter) ;
}

public function getCookieNameVisitor()
{
return $this->parameterBag->get('app.cookie_name_visitor');
}

public function cryptCookie($data)
{
return base64_encode($data);
}

public function decryptCookie($data)
{
return base64_decode($data);
}

public function setCookieVisitor($response, $cookie)
{
$response->headers->setCookie(Cookie::create($this->getCookieNameVisitor(), $this->cryptCookie($cookie), new \DateTime('+2 months'), '/', $this->utils->getCookieDomain()));
}

public function updateVisitorCookie($response)
{
$response->headers->setCookie(Cookie::create($this->getCookieNameVisitor(), $this->cryptCookie($this->getVisitorCurrent()->getCookie()), new \DateTime('+2 months'), '/', $this->utils->getCookieDomain()));

}

public function getVisitor($cookie)
{
if (!isset($this->visitor[$cookie])) {
$this->visitor[$cookie] = $this->visitorRepository->findOneBy(['cookie' => $cookie]);
}
return $this->visitor[$cookie];
}

public function getVisitorCurrent()
{
$cookie = $this->requestStack->getCurrentRequest()->cookies->get($this->getCookieNameVisitor());
return $this->getVisitor($cookie);
}

public function addVisitor($cookie, $ip)
{
$classVisitor = $this->em->getClassMetadata(VisitorInterface::class)->getName();
$visitor = new $classVisitor;

$visitor->setCookie($cookie);
$visitor->setIp($ip);
$visitor->setTotalVisit(1);
$visitor->setLastAccess(new \DateTime());

$this->em->persist($visitor);
$this->em->flush();
}

public function updateVisitor($visitor)
{
$totalVisit = $visitor->getTotalVisit() + 1;
$visitor->setTotalVisit($totalVisit);
$visitor->setLastAccess(new \DateTime());

$this->em->persist($visitor);
$this->em->flush();
}

public function setNewsletter($user, $newsletter, $subscribeNewsletter)
{
$currentMerchant = $this->merchantUtils->getMerchantCurrent();
$newsletters = $currentMerchant->getNewsletters();

foreach ($newsletters as $newsletterMerchant) {
if ($newsletterMerchant->getId() == $newsletter->getId()) {
if ($subscribeNewsletter) {
$user->addNewsletter($newsletter);
} else {
$user->removeNewsletter($newsletter);
}
}
}
}

$this->em->persist($user) ;
$this->em->flush() ;
}
$this->em->persist($user);
$this->em->flush();
}

public function isSubscribedToNewsletter($user, $newsletter)
{
return $user->getNewsletters()->contains($newsletter);
}
public function isSubscribedToNewsletter($user, $newsletter)
{
return $user->getNewsletters()->contains($newsletter);
}

}

+ 13
- 0
ShopBundle/Services/Utils.php View File

@@ -602,5 +602,18 @@ class Utils
return round((($price * 100)) / 100, $precision);
}

public function formatPhoneNumber($phone)
{
$phone = preg_replace('`[^0-9]`', '', $phone);
if(strlen($phone) == 10) {
$phone = '+33'.substr($phone, 1, 9) ;
}
elseif(strlen($phone) == 11 && substr($phone, 0, 2) == '33') {
$phone = '+'.$phone ;
}

return $phone ;
}


}

+ 0
- 1
ShopBundle/Twig/FrontendTwigExtension.php View File

@@ -104,5 +104,4 @@ class FrontendTwigExtension extends AbstractExtension
{
return $this->merchantRepository->findAll();
}

}

Loading…
Cancel
Save