Browse Source

Refactoring services

develop
Guillaume 3 years ago
parent
commit
84d0849846
12 changed files with 93 additions and 71 deletions
  1. +10
    -1
      Container/Setting/SiteSettingContainer.php
  2. +10
    -1
      Container/Ticket/TicketContainer.php
  3. +1
    -2
      Controller/AbstractAdminController.php
  4. +6
    -0
      Controller/Setting/SettingAdminController.php
  5. +2
    -2
      Controller/Ticket/TicketAdminController.php
  6. +23
    -18
      EventSubscriber/SiteSettingEventSubscriber.php
  7. +0
    -35
      Model/Setting/EntitySettingTrait.php
  8. +2
    -2
      Model/Site/NewsModel.php
  9. +0
    -2
      Model/Site/SiteModel.php
  10. +9
    -4
      Repository/AbstractStore.php
  11. +1
    -1
      Repository/StoreInterface.php
  12. +29
    -3
      Solver/Setting/SettingSolver.php

+ 10
- 1
Container/Setting/SiteSettingContainer.php View File

use Lc\SovBundle\Factory\Setting\SiteSettingFactory; use Lc\SovBundle\Factory\Setting\SiteSettingFactory;
use Lc\SovBundle\Repository\Setting\SiteSettingRepositoryQuery; use Lc\SovBundle\Repository\Setting\SiteSettingRepositoryQuery;
use Lc\SovBundle\Repository\Setting\SiteSettingStore; use Lc\SovBundle\Repository\Setting\SiteSettingStore;
use Lc\SovBundle\Solver\Setting\SettingSolver;


class SiteSettingContainer class SiteSettingContainer
{ {
protected SiteSettingDefinition $definition; protected SiteSettingDefinition $definition;
protected SiteSettingRepositoryQuery $repositoryQuery; protected SiteSettingRepositoryQuery $repositoryQuery;
protected SiteSettingStore $store; protected SiteSettingStore $store;
protected SettingSolver $settingSolver;


public function __construct( public function __construct(
SiteSettingFactory $factory, SiteSettingFactory $factory,
SiteSettingDefinition $definition, SiteSettingDefinition $definition,
SiteSettingRepositoryQuery $repositoryQuery, SiteSettingRepositoryQuery $repositoryQuery,
SiteSettingStore $store
SiteSettingStore $store,
SettingSolver $settingSolver
) { ) {
$this->factory = $factory; $this->factory = $factory;
$this->definition = $definition; $this->definition = $definition;
$this->repositoryQuery = $repositoryQuery; $this->repositoryQuery = $repositoryQuery;
$this->store = $store; $this->store = $store;
$this->settingSolver = $settingSolver;
} }


public function getFactory(): SiteSettingFactory public function getFactory(): SiteSettingFactory
{ {
return $this->store; return $this->store;
} }

public function getSettingSolver(): SettingSolver
{
return $this->settingSolver;
}
} }

+ 10
- 1
Container/Ticket/TicketContainer.php View File

use Lc\SovBundle\Factory\Ticket\TicketFactory; use Lc\SovBundle\Factory\Ticket\TicketFactory;
use Lc\SovBundle\Repository\Ticket\TicketRepositoryQuery; use Lc\SovBundle\Repository\Ticket\TicketRepositoryQuery;
use Lc\SovBundle\Repository\Ticket\TicketStore; use Lc\SovBundle\Repository\Ticket\TicketStore;
use Lc\SovBundle\Solver\Ticket\TicketSolver;


class TicketContainer class TicketContainer
{ {
protected TicketBuilder $builder; protected TicketBuilder $builder;
protected TicketRepositoryQuery $repositoryQuery; protected TicketRepositoryQuery $repositoryQuery;
protected TicketStore $store; protected TicketStore $store;
protected TicketSolver $solver;


public function __construct( public function __construct(
TicketFactory $factory, TicketFactory $factory,
TicketBuilder $builder, TicketBuilder $builder,
TicketRepositoryQuery $repositoryQuery, TicketRepositoryQuery $repositoryQuery,
TicketStore $store
TicketStore $store,
TicketSolver $solver
) { ) {
$this->factory = $factory; $this->factory = $factory;
$this->builder = $builder; $this->builder = $builder;
$this->repositoryQuery = $repositoryQuery; $this->repositoryQuery = $repositoryQuery;
$this->store = $store; $this->store = $store;
$this->solver = $solver;
} }


public function getFactory(): TicketFactory public function getFactory(): TicketFactory
{ {
return $this->store; return $this->store;
} }

public function getSolver(): TicketSolver
{
return $this->solver;
}
} }

+ 1
- 2
Controller/AbstractAdminController.php View File

RequestStack::class => RequestStack::class, RequestStack::class => RequestStack::class,
EntityManagerInterface::class => EntityManagerInterface::class, EntityManagerInterface::class => EntityManagerInterface::class,
FilterManager::class => FilterManager::class, FilterManager::class => FilterManager::class,

FileContainer::class => FileContainer::class, FileContainer::class => FileContainer::class,
NewsletterContainer::class => NewsletterContainer::class, NewsletterContainer::class => NewsletterContainer::class,
ReminderContainer::class => ReminderContainer::class, ReminderContainer::class => ReminderContainer::class,
$context = $this->get(AdminContextProvider::class)->getContext(); $context = $this->get(AdminContextProvider::class)->getContext();


return $context->getCrudControllers()->findCrudFqcnByEntityFqcn( return $context->getCrudControllers()->findCrudFqcnByEntityFqcn(
$this->get('em')->getEntityName($interface)
$this->get(EntityManagerInterface::class)->getEntityName($interface)
); );
} }



+ 6
- 0
Controller/Setting/SettingAdminController.php View File



class SettingAdminController extends AbstractController class SettingAdminController extends AbstractController
{ {
protected EntityManagerInterface $entityManager;

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


/** /**
* @Route("/admin/setting/site", name="sov_admin_setting_site") * @Route("/admin/setting/site", name="sov_admin_setting_site")

+ 2
- 2
Controller/Ticket/TicketAdminController.php View File

ChoiceField::new('type') ChoiceField::new('type')
->autocomplete() ->autocomplete()
->setChoices( ->setChoices(
$translatorAdmin->transChoices(TicketModel::getTypeChoices(), 'Ticket', 'type')
$translatorAdmin->transChoices($this->get(TicketContainer::class)->getSolver()->getTypeChoices(), 'Ticket', 'type')
), ),
ChoiceField::new('status') ChoiceField::new('status')
->autocomplete() ->autocomplete()
->setChoices( ->setChoices(
$translatorAdmin->transChoices(TicketModel::getStatusChoices(), 'Ticket', 'status')
$translatorAdmin->transChoices($this->get(TicketContainer::class)->getSolver()->getStatusChoices(), 'Ticket', 'status')


) )
->setTemplatePath('@LcSov/admin/ticket/field/status.html.twig') ->setTemplatePath('@LcSov/admin/ticket/field/status.html.twig')

+ 23
- 18
EventSubscriber/SiteSettingEventSubscriber.php View File

use Lc\SovBundle\Factory\Site\SiteFactory; use Lc\SovBundle\Factory\Site\SiteFactory;
use Lc\SovBundle\Repository\Setting\SiteSettingRepository; use Lc\SovBundle\Repository\Setting\SiteSettingRepository;
use Lc\SovBundle\Repository\Site\SiteRepository; use Lc\SovBundle\Repository\Site\SiteRepository;
use Lc\SovBundle\Repository\Site\SiteStore;
use Lc\SovBundle\Solver\Setting\SettingSolver;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\KernelEvents;


class SiteSettingEventSubscriber implements EventSubscriberInterface class SiteSettingEventSubscriber implements EventSubscriberInterface
{ {
protected $em;
protected $siteSettingDefinition;
protected $siteSettingFactory;
protected $siteSettingRepository;
protected $siteRepository;
protected $siteFactory;
protected EntityManagerInterface $entityManager;
protected SiteSettingDefinitionInterface $siteSettingDefinition;
protected SiteSettingFactory $siteSettingFactory;
protected SiteSettingRepository $siteSettingRepository;
protected SiteStore $siteStore;
protected SiteFactory $siteFactory;
protected SettingSolver $settingSolver;


public function __construct( public function __construct(
EntityManagerInterface $em,
EntityManagerInterface $entityManager,
SiteSettingRepository $siteSettingRepository, SiteSettingRepository $siteSettingRepository,
SiteRepository $siteRepository,
SiteStore $siteStore,
SiteSettingDefinitionInterface $siteSettingDefinition, SiteSettingDefinitionInterface $siteSettingDefinition,
SiteSettingFactory $siteSettingFactory, SiteSettingFactory $siteSettingFactory,
SiteFactory $siteFactory
SiteFactory $siteFactory,
SettingSolver $settingSolver
) { ) {
$this->em = $em;
$this->entityManager = $entityManager;
$this->siteSettingDefinition = $siteSettingDefinition; $this->siteSettingDefinition = $siteSettingDefinition;
$this->siteSettingFactory = $siteSettingFactory; $this->siteSettingFactory = $siteSettingFactory;
$this->siteSettingRepository = $siteSettingRepository; $this->siteSettingRepository = $siteSettingRepository;
$this->siteRepository = $siteRepository;
$this->siteStore = $siteStore;
$this->siteFactory = $siteFactory; $this->siteFactory = $siteFactory;
$this->settingSolver = $settingSolver;
} }


public static function getSubscribedEvents() public static function getSubscribedEvents()
$site = $this->getSiteDefault(); $site = $this->getSiteDefault();
if (!$site) { if (!$site) {
$site = $this->siteFactory->create('default'); $site = $this->siteFactory->create('default');
$this->em->persist($site);
$this->em->flush($site);
$this->entityManager->persist($site);
$this->entityManager->flush($site);
} }


$settings = $this->siteSettingDefinition->getSettings(); $settings = $this->siteSettingDefinition->getSettings();


foreach ($settings as $category => $settingList) { foreach ($settings as $category => $settingList) {
foreach ($settingList as $settingName => $setting) { foreach ($settingList as $settingName => $setting) {
$entitySetting = $site->getSetting($settingName);
$entitySetting = $this->settingSolver->getSetting($site, $settingName);


if (!$entitySetting) { if (!$entitySetting) {




$entitySetting = $factory->create($site, $setting['name'], $text, $date, $file); $entitySetting = $factory->create($site, $setting['name'], $text, $date, $file);


$this->em->persist($entitySetting);
$this->entityManager->persist($entitySetting);
} else { } else {
$methodGetValue = 'get' . ucfirst($setting['field']); $methodGetValue = 'get' . ucfirst($setting['field']);
if ($entitySetting->$methodGetValue() === null if ($entitySetting->$methodGetValue() === null
&& $setting['default'] !== null) { && $setting['default'] !== null) {
$methodSetValue = 'set' . ucfirst($setting['field']); $methodSetValue = 'set' . ucfirst($setting['field']);
$entitySetting->$methodSetValue($setting['default']); $entitySetting->$methodSetValue($setting['default']);
$this->em->update($entitySetting);
$this->entityManager->update($entitySetting);
} }
} }
} }
} }


$this->em->flush();
$this->entityManager->flush();
} }


public function getSiteDefault() public function getSiteDefault()
{ {
return $this->siteRepository->findOneByDevAlias('default');
return $this->siteStore->getOneByDevAlias('default');
} }


} }

+ 0
- 35
Model/Setting/EntitySettingTrait.php View File

<?php

namespace Lc\SovBundle\Model\Setting;

//TODO à déplacer dans un solver

trait EntitySettingTrait
{
public function getSetting($name): ?SettingModel
{
if ($this->getSettings()) {
foreach ($this->getSettings() as $setting) {
if ($setting->getName() == $name) {
return $setting;
}
}
}

return null;
}

public function getSettingValue($name): ?string
{
if ($this->getSettings()) {
foreach ($this->getSettings() as $setting) {
if ($setting->getName() == $name) {
return $setting->getValue();
}
}
}

return null;
}

}

+ 2
- 2
Model/Site/NewsModel.php View File





/** /**
* @ORM\ManyToOne(targetEntity="Lc\SovBundle\Newsletter\NewsletterInsterface", inversedBy="news")
* @ORM\ManyToOne(targetEntity="Lc\SovBundle\Model\Newsletter\NewsletterInterface", inversedBy="news")
*/ */
protected $newsletter; protected $newsletter;


/** /**
* @ORM\ManyToOne(targetEntity="Lc\SovBundle\File\FileInsterface", cascade={"persist", "remove"})
* @ORM\ManyToOne(targetEntity="Lc\SovBundle\Model\File\FileInterface", cascade={"persist", "remove"})
*/ */
protected $image; protected $image;



+ 0
- 2
Model/Site/SiteModel.php View File

use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Lc\SovBundle\Model\Setting\EntitySettingTrait;
use Lc\SovBundle\Model\Setting\SiteSettingInterface; use Lc\SovBundle\Model\Setting\SiteSettingInterface;
use Lc\SovBundle\Doctrine\EntityInterface; use Lc\SovBundle\Doctrine\EntityInterface;
use Lc\SovBundle\Doctrine\Extension\DevAliasInterface; use Lc\SovBundle\Doctrine\Extension\DevAliasInterface;


abstract class SiteModel implements SiteInterface, EntityInterface, DevAliasInterface abstract class SiteModel implements SiteInterface, EntityInterface, DevAliasInterface
{ {
use EntitySettingTrait;
use DevAliasTrait; use DevAliasTrait;


/** /**

+ 9
- 4
Repository/AbstractStore.php View File

return $query->findOne(); return $query->findOne();
} }


public function getOneByDevAlias(string $devAlias, bool $isOnline = true, $query = null)
public function getOneByDevAlias(string $devAlias, $query = null)
{ {
$query = $this->createDefaultQuery($query); $query = $this->createDefaultQuery($query);
$query->filterByDevAlias($devAlias); $query->filterByDevAlias($devAlias);
return $query->findOne();
}


if ($isOnline) {
$query->filterIsOnline();
}
public function getOneOnlineByDevAlias(string $devAlias, $query = null)
{
$query = $this->createDefaultQuery($query);
$query
->filterByDevAlias($devAlias)
->filterIsOnline();


return $query->findOne(); return $query->findOne();
} }

+ 1
- 1
Repository/StoreInterface.php View File



public function getOneBySlug(string $slug, bool $isOnline = true, $query = null); public function getOneBySlug(string $slug, bool $isOnline = true, $query = null);


public function getOneByDevAlias(string $devAlias, bool $isOnline = true, $query = null);
public function getOneByDevAlias(string $devAlias, $query = null);


public function get($query = null); public function get($query = null);



+ 29
- 3
Solver/Setting/SettingSolver.php View File



namespace Lc\SovBundle\Solver\Setting; namespace Lc\SovBundle\Solver\Setting;


use Lc\SovBundle\Doctrine\EntityInterface;
use Lc\SovBundle\Model\Setting\SettingInterface; use Lc\SovBundle\Model\Setting\SettingInterface;
use Lc\SovBundle\Model\Ticket\TicketInterface;
use Lc\SovBundle\Model\Ticket\TicketModel;
use Lc\SovBundle\Model\Setting\SettingModel;


class SettingSolver class SettingSolver
{ {
public function getValue(SettingInterface $setting)
public function getSetting(EntityInterface $entity, string $name): ?SettingModel
{
if ($entity->getSettings()) {
foreach ($entity->getSettings() as $setting) {
if ($setting->getName() == $name) {
return $setting;
}
}
}

return null;
}

public function getSettingValue(EntityInterface $entity, string $name): ?string
{
$setting = $this->getSetting($entity, $name);

if($setting) {
return $this->getValue($setting);
}

return null;
}

public function getValue(SettingInterface $setting): ?string
{ {
if ($setting->getText()) { if ($setting->getText()) {
return $setting->getText(); return $setting->getText();
} elseif ($setting->getFile()) { } elseif ($setting->getFile()) {
return $setting->getFile(); return $setting->getFile();
} }

return null;
} }
} }

Loading…
Cancel
Save