Browse Source

Refactoring services

develop
Guillaume 3 years ago
parent
commit
f140b057e4
12 changed files with 269 additions and 9 deletions
  1. +29
    -0
      Builder/Ticket/TicketBuilder.php
  2. +29
    -0
      Builder/User/UserBuilder.php
  3. +33
    -0
      Component/CookieComponent.php
  4. +95
    -0
      Component/EntityComponent.php
  5. +25
    -0
      Component/FormComponent.php
  6. +4
    -3
      Factory/Ticket/TicketFactory.php
  7. +3
    -1
      Factory/Ticket/TicketMessageFactory.php
  8. +32
    -0
      Model/User/User.php
  9. +8
    -0
      Repository/Ticket/TicketRepositoryQuery.php
  10. +10
    -0
      Repository/Ticket/TicketStore.php
  11. +1
    -0
      Repository/User/UserStore.php
  12. +0
    -5
      Resolver/UrlResolver.php

+ 29
- 0
Builder/Ticket/TicketBuilder.php View File

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

namespace Lc\SovBundle\Builder\Ticket;

use Lc\SovBundle\Component\FormComponent;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

class TicketBuilder
{
protected FormComponent $formComponent;
protected ParameterBagInterface $parameterBag;

public function __construct(FormComponent $formComponent, ParameterBagInterface $parameterBag)
{
$this->formComponent = $formComponent;
$this->parameterBag = $parameterBag;
}

// uploadImageTicketMessage
public function uploadImageTicketMessage($formTicket)
{
return $this->formComponent->uploadFile(
$formTicket,
'image',
$this->parameterBag->get('app.ticket_images_directory'),
$this->parameterBag->get('app.ticket_images_subdirectory')
);
}
}

+ 29
- 0
Builder/User/UserBuilder.php View File

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

namespace Lc\SovBundle\Builder\User;

use Doctrine\ORM\EntityManagerInterface;
use Lc\SovBundle\Model\Newsletter\NewsletterInterface;
use Lc\SovBundle\Model\User\UserInterface;

class UserBuilder
{
protected EntityManagerInterface $entityManager;

public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}

public function setNewsletter(UserInterface $user, NewsletterInterface $newsletter, bool $subscribeNewsletter)
{
if ($subscribeNewsletter) {
$user->addNewsletter($newsletter);
} else {
$user->removeNewsletter($newsletter);
}
$this->entityManager->persist($user);
$this->entityManager->flush();
}
}

+ 33
- 0
Component/CookieComponent.php View File

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

namespace Lc\SovBundle\Component;

use Lc\SovBundle\Resolver\UrlResolver;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

class CookieComponent
{
protected UrlResolver $urlResolver;
protected ParameterBagInterface $parameterBag;

public function __construct(UrlResolver $urlResolver, ParameterBagInterface $parameterBag)
{
$this->urlResolver = $urlResolver;
$this->parameterBag = $parameterBag;
}

public function getCookieDomain()
{
return ($this->urlResolver->isServerLocalhost()) ? null : $this->parameterBag->get('app.cookie_domain_distant');
}

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

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

+ 95
- 0
Component/EntityComponent.php View File

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

namespace Lc\SovBundle\Component;

use Doctrine\ORM\EntityManagerInterface;
use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
use Lc\SovBundle\Doctrine\Extension\SluggableInterface;
use Lc\SovBundle\Model\File\FileInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

class EntityComponent
{
protected EntityManagerInterface $entityManager;
protected ParameterBagInterface $parameterBag;

public function __construct(
EntityManagerInterface $entityManager,
ParameterBagInterface $parameterBag
) {
$this->entityManager = $entityManager;
$this->parameterBag = $parameterBag;
}

public function duplicateEntity($entity, $flush = true)
{
$newEntity = clone $entity;

if ($newEntity instanceof FileInterface) {
$newEntity = $this->duplicateImage($newEntity);
}

if ($newEntity instanceof ProductFamilyInterface) {
// @TODO : à adapter
//$newEntity = $this->productFamilyUtils->processBeforePersistProductFamily($newEntity, false, true);
}

if (method_exists($newEntity, 'getAddress') && is_object($newEntity->getAddress())) {
$address = $newEntity->getAddress();
$newAddress = $this->duplicateEntity($address);
$newEntity->setAddress($newAddress);
$this->entityManager->persist($newAddress);
}

if ($newEntity instanceof SluggableInterface) {
$this->entityManager->persist($newEntity);
if ($flush) {
$this->entityManager->flush();
}
$newEntity->setSlug(null);
}
$this->entityManager->persist($newEntity);
if ($flush) {
$this->entityManager->flush();
}


return $newEntity;
}

public function duplicateEntityToOtherHub($entity, $hub)
{
$newEntity = $this->duplicateEntity($entity);

if ($hub) {
$newEntity->setMerchant($hub);
}
$this->entityManager->persist($newEntity);
$this->entityManager->flush();

return $newEntity;
}

public function duplicateImage($entity, $folder = false)
{
$basePath = $this->parameterBag->get('kernel.project_dir') . '/public/uploads/images/';

if ($entity->getImage() && file_exists($basePath . $entity->getImage())) {
$extension = strtolower(pathinfo($basePath . $entity->getImage(), PATHINFO_EXTENSION));

if ($extension == "jpg" || $extension == "png" || $extension == "gif") {
$newImage = md5(uniqid()) . '.' . $extension;
if ($folder) {
$newImage = $folder . '/' . $newImage;
}
copy($basePath . $entity->getImage(), $basePath . $newImage);
$entity->setImage($newImage);
} else {
$entity->setImage(null);
}
} else {
$entity->setImage(null);
}
return $entity;
}
}

+ 25
- 0
Component/FormComponent.php View File

@@ -5,6 +5,7 @@ namespace Lc\SovBundle\Component;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpFoundation\File\Exception\FileException;

class FormComponent
{
@@ -30,4 +31,28 @@ class FormComponent
]);
}

// uploadImageTicketMessage
public function uploadFile($form, $child, $directory, $subdirectory)
{
$file = $form->get($child)->getData();

if ($file) {
$originalFilename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
$newFilename = uniqid().'.'.$file->guessExtension();

try {
$file->move(
$directory,
$newFilename
);
} catch (FileException $e) {
throw new \ErrorException("Une erreur est survenue lors de l'upload du fichier.");
}

return $subdirectory.$newFilename;
}

return false;
}

}

+ 4
- 3
Factory/Ticket/TicketFactory.php View File

@@ -5,19 +5,20 @@ namespace Lc\SovBundle\Factory\Ticket;
use App\Entity\Ticket\Ticket;
use Lc\SovBundle\Factory\AbstractFactory;
use Lc\SovBundle\Model\Ticket\TicketInterface;
use Lc\SovBundle\Model\Ticket\TicketModel;

class TicketFactory extends AbstractFactory implements TicketFactoryInterface
{

public function create(): TicketInterface
{
$ticket = new Ticket();

$ticketMessageFactory = new TicketMessageFactory();
$ticketMessage = $ticketMessageFactory->create() ;
$ticketMessage = $ticketMessageFactory->create($ticket) ;

$ticket->setStatus(TicketModel::TICKET_STATUS_OPEN);
$ticket->addTicketMessage($ticketMessage) ;

return $ticket;
}

}

+ 3
- 1
Factory/Ticket/TicketMessageFactory.php View File

@@ -4,14 +4,16 @@ namespace Lc\SovBundle\Factory\Ticket;

use App\Entity\Ticket\TicketMessage;
use Lc\SovBundle\Factory\AbstractFactory;
use Lc\SovBundle\Model\Ticket\TicketInterface;
use Lc\SovBundle\Model\Ticket\TicketMessageInterface;

class TicketMessageFactory extends AbstractFactory implements TicketMessageFactoryInterface
{
public function create(): TicketMessageInterface
public function create(TicketInterface $ticket): TicketMessageInterface
{
$ticketMessage = new TicketMessage();

$ticketMessage->setTicket($ticket);
$ticketMessage->setStatus(1);

return $ticketMessage;

+ 32
- 0
Model/User/User.php View File

@@ -5,6 +5,7 @@ namespace Lc\SovBundle\Model\User;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Lc\SovBundle\Doctrine\EntityInterface;
use Lc\SovBundle\Model\Newsletter\NewsletterInterface;
use Symfony\Component\Security\Core\User\UserInterface;

/**
@@ -48,6 +49,37 @@ abstract class User implements EntityInterface, UserInterface
*/
protected $groupUsers;

// isUserInGroupVip
public function isInGroupUserVip()
{
return $this->isInGroupByDevAlias('vip') ;
}

// isUserInGroup
public function isInGroupUser(GroupUserInterface $groupUser)
{
return $this->isInGroupByDevAlias($groupUser->getDevAlias());
}

// isUserInGroupByDevAlias
public function isInGroupByDevAlias(string $groupUserDevAlias)
{
$groupUsers = $this->getGroupUsers();

foreach($groupUsers as $groupUser) {
if($groupUser->getDevAlias() == $groupUserDevAlias) {
return true;
}
}

return false;
}

public function isSubscribedToNewsletter(NewsletterInterface $newsletter)
{
return $this->getNewsletters()->contains($newsletter);
}

public function getEmail(): ?string
{
return $this->email;

+ 8
- 0
Repository/Ticket/TicketRepositoryQuery.php View File

@@ -3,6 +3,7 @@
namespace Lc\SovBundle\Repository\Ticket;

use Knp\Component\Pager\PaginatorInterface;
use Lc\SovBundle\Model\User\UserInterface;
use Lc\SovBundle\Repository\AbstractRepositoryQuery;

class TicketRepositoryQuery extends AbstractRepositoryQuery implements TicketRepositoryQueryInterface
@@ -11,4 +12,11 @@ class TicketRepositoryQuery extends AbstractRepositoryQuery implements TicketRep
{
parent::__construct($repository, 'r', $paginator);
}

public function filterByUser(UserInterface $user)
{
return $this
->andWhere('.user = :user')
->setParameter('user', $user);
}
}

+ 10
- 0
Repository/Ticket/TicketStore.php View File

@@ -12,4 +12,14 @@ class TicketStore extends AbstractStore implements TicketStoreInterface
{
$this->query = $query;
}

// getTicketsByUser
public function getByUser($user)
{
$query = $this->query->create();

$query->filterByUser($user);

return $query->find();
}
}

+ 1
- 0
Repository/User/UserStore.php View File

@@ -2,6 +2,7 @@

namespace Lc\SovBundle\Repository\User;

use Lc\SovBundle\Model\User\UserInterface;
use Lc\SovBundle\Repository\AbstractStore;

class UserStore extends AbstractStore implements UserStoreInterface

+ 0
- 5
Resolver/UrlResolver.php View File

@@ -23,11 +23,6 @@ class UrlResolver
return in_array($_SERVER['REMOTE_ADDR'], ['127.0.0.1', '::1']);
}

public function getCookieDomain()
{
return ($this->isServerLocalhost()) ? null : $this->parameterBag->get('app.cookie_domain_distant');
}

public function isBot()
{
if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match(

Loading…
Cancel
Save