Fix issues reported by PHPStan.

This commit is contained in:
Pol Dellaiera 2021-11-09 15:17:24 +01:00
parent 48ea67968e
commit 2eb30d2ae8
No known key found for this signature in database
GPG Key ID: D476DFE9C67467CA
7 changed files with 124 additions and 137 deletions

View File

@ -1,8 +1,10 @@
<?php <?php
namespace Chill\DocStoreBundle\Repository; declare(strict_types=1);
use App\Entity\AccompanyingCourseDocument; namespace Chill\DocStoreBundle\EntityRepository;
use Chill\DocStoreBundle\Entity\AccompanyingCourseDocument;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry; use Doctrine\Persistence\ManagerRegistry;

View File

@ -1,35 +1,32 @@
<?php <?php
/*
*/ declare(strict_types=1);
namespace Chill\MainBundle\DependencyInjection\CompilerPass; namespace Chill\MainBundle\DependencyInjection\CompilerPass;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerBuilder;
use Chill\MainBundle\Form\PermissionsGroupType; use Chill\MainBundle\Form\PermissionsGroupType;
use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\Reference;
use LogicException;
/**
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class ACLFlagsCompilerPass implements CompilerPassInterface class ACLFlagsCompilerPass implements CompilerPassInterface
{ {
public function process(ContainerBuilder $container) public function process(ContainerBuilder $container)
{ {
$permissionGroupType = $container->getDefinition(PermissionsGroupType::class); $permissionGroupType = $container->getDefinition(PermissionsGroupType::class);
foreach($container->findTaggedServiceIds('chill_main.flags') as $id => $tags) { foreach($container->findTaggedServiceIds('chill_main.flags') as $id => $tags) {
$reference = new Reference($id); $reference = new Reference($id);
foreach ($tags as $tag) { foreach ($tags as $tag) {
switch($tag['scope']) { switch($tag['scope']) {
case PermissionsGroupType::FLAG_SCOPE: case PermissionsGroupType::FLAG_SCOPE:
$permissionGroupType->addMethodCall('addFlagProvider', [ $reference ]); $permissionGroupType->addMethodCall('addFlagProvider', [ $reference ]);
break; break;
default: default:
throw new \LogicalException(sprintf( throw new LogicException(sprintf(
"This tag 'scope' is not implemented: %s, on service with id %s", $tag['scope'], $id) "This tag 'scope' is not implemented: %s, on service with id %s", $tag['scope'], $id)
); );
} }

View File

@ -30,27 +30,27 @@ use Chill\MainBundle\DependencyInjection\Widget\HasWidgetFactoriesExtensionInter
/** /**
* Compile the configurations and inject required service into container. * Compile the configurations and inject required service into container.
* *
* The widgets are services tagged with : * The widgets are services tagged with :
* *
* ``` * ```
* { name: chill_widget, alias: my_alias, place: my_place } * { name: chill_widget, alias: my_alias, place: my_place }
* ``` * ```
* *
* Or, if the tag does not exist or if you need to add some config to your * Or, if the tag does not exist or if you need to add some config to your
* service depending on the config, you should use a `WidgetFactory` (see * service depending on the config, you should use a `WidgetFactory` (see
* `WidgetFactoryInterface`. * `WidgetFactoryInterface`.
* *
* To reuse this compiler pass, simple execute the doProcess metho in your * To reuse this compiler pass, simple execute the doProcess metho in your
* compiler. Example : * compiler. Example :
* *
* ``` * ```
* namespace Chill\MainBundle\DependencyInjection\CompilerPass; * namespace Chill\MainBundle\DependencyInjection\CompilerPass;
* *
* use Symfony\Component\DependencyInjection\ContainerBuilder; * use Symfony\Component\DependencyInjection\ContainerBuilder;
* use Chill\MainBundle\DependencyInjection\Widget\AbstractWidgetsCompilerPass; * use Chill\MainBundle\DependencyInjection\Widget\AbstractWidgetsCompilerPass;
* class WidgetsCompilerPass extends AbstractWidgetsCompilerPass { * class WidgetsCompilerPass extends AbstractWidgetsCompilerPass {
* *
* public function process(ContainerBuilder $container) * public function process(ContainerBuilder $container)
* { * {
* $this->doProcess($container, 'chill_main', 'chill_main.widgets'); * $this->doProcess($container, 'chill_main', 'chill_main.widgets');
@ -58,58 +58,58 @@ use Chill\MainBundle\DependencyInjection\Widget\HasWidgetFactoriesExtensionInter
* } * }
* ``` * ```
* *
* *
*/ */
abstract class AbstractWidgetsCompilerPass implements CompilerPassInterface abstract class AbstractWidgetsCompilerPass implements CompilerPassInterface
{ {
private $widgetServices = array(); private $widgetServices = array();
/** /**
* *
* @var WidgetFactoryInterface[] * @var WidgetFactoryInterface[]
*/ */
private $widgetFactories; private $widgetFactories;
/** /**
* The service which will manage the widgets * The service which will manage the widgets
* *
* @var string * @var string
*/ */
const WIDGET_MANAGER = 'chill.main.twig.widget'; const WIDGET_MANAGER = 'chill.main.twig.widget';
/** /**
* the method wich register the widget into give service. * the method wich register the widget into give service.
*/ */
const WIDGET_MANAGER_METHOD_REGISTER = 'addWidget'; const WIDGET_MANAGER_METHOD_REGISTER = 'addWidget';
/** /**
* the value of the `name` key in service definitions's tag * the value of the `name` key in service definitions's tag
* *
* @var string * @var string
*/ */
const WIDGET_SERVICE_TAG_NAME = 'chill_widget'; const WIDGET_SERVICE_TAG_NAME = 'chill_widget';
/** /**
* the key used to collect the alias in the service definition's tag. * the key used to collect the alias in the service definition's tag.
* the alias must be * the alias must be
* injected into the configuration under 'alias' key. * injected into the configuration under 'alias' key.
* *
* @var string * @var string
*/ */
const WIDGET_SERVICE_TAG_ALIAS = 'alias'; const WIDGET_SERVICE_TAG_ALIAS = 'alias';
/** /**
* the key used to collect the authorized place in the service definition's tag * the key used to collect the authorized place in the service definition's tag
* *
* @var string * @var string
*/ */
const WIDGET_SERVICE_TAG_PLACES = 'place'; const WIDGET_SERVICE_TAG_PLACES = 'place';
/** /**
* the key to use to order widget for a given place * the key to use to order widget for a given place
*/ */
const WIDGET_CONFIG_ORDER = 'order'; const WIDGET_CONFIG_ORDER = 'order';
/** /**
* the key to use to identify widget for a given place * the key to use to identify widget for a given place
*/ */
@ -118,24 +118,25 @@ abstract class AbstractWidgetsCompilerPass implements CompilerPassInterface
/** /**
* process the configuration and the container to add the widget available * process the configuration and the container to add the widget available
* *
* @param ContainerBuilder $container * @param ContainerBuilder $container
* @param string $extension the extension of your bundle * @param string $extension the extension of your bundle
* @param string $containerWidgetConfigParameterName the key under which we can use the widget configuration * @param string $containerWidgetConfigParameterName the key under which we can use the widget configuration
* @throws \LogicException * @throws \LogicException
* @throws \UnexpectedValueException if the given extension does not implement HasWidgetExtensionInterface * @throws \UnexpectedValueException if the given extension does not implement HasWidgetExtensionInterface
* @throws \InvalidConfigurationException if there are errors in the config
*/ */
public function doProcess(ContainerBuilder $container, $extension, public function doProcess(
$containerWidgetConfigParameterName) ContainerBuilder $container,
{ $extension,
$containerWidgetConfigParameterName
) {
if (!$container->hasDefinition(self::WIDGET_MANAGER)) { if (!$container->hasDefinition(self::WIDGET_MANAGER)) {
throw new \LogicException("the service ".self::WIDGET_MANAGER." should". throw new \LogicException("the service ".self::WIDGET_MANAGER." should".
" be present. It is required by ".self::class); " be present. It is required by ".self::class);
} }
$managerDefinition = $container->getDefinition(self::WIDGET_MANAGER); $managerDefinition = $container->getDefinition(self::WIDGET_MANAGER);
// collect the widget factories // collect the widget factories
/* @var $extensionClass HasWidgetFactoriesExtensionInterface */ /* @var $extensionClass HasWidgetFactoriesExtensionInterface */
$extensionClass = $container->getExtension($extension); $extensionClass = $container->getExtension($extension);
@ -148,19 +149,19 @@ abstract class AbstractWidgetsCompilerPass implements CompilerPassInterface
HasWidgetFactoriesExtensionInterface::class, HasWidgetFactoriesExtensionInterface::class,
get_class($extensionClass))); get_class($extensionClass)));
} }
$this->widgetFactories = $extensionClass->getWidgetFactories(); $this->widgetFactories = $extensionClass->getWidgetFactories();
// collect the availabled tagged services // collect the availabled tagged services
$this->collectTaggedServices($container); $this->collectTaggedServices($container);
// collect the widgets and their config : // collect the widgets and their config :
$widgetParameters = $container->getParameter($containerWidgetConfigParameterName); $widgetParameters = $container->getParameter($containerWidgetConfigParameterName);
// and add them to the delegated_block // and add them to the delegated_block
foreach($widgetParameters as $place => $widgets) { foreach($widgetParameters as $place => $widgets) {
foreach ($widgets as $param) { foreach ($widgets as $param) {
$alias = $param[self::WIDGET_CONFIG_ALIAS]; $alias = $param[self::WIDGET_CONFIG_ALIAS];
// check that the service exists // check that the service exists
@ -168,43 +169,43 @@ abstract class AbstractWidgetsCompilerPass implements CompilerPassInterface
throw new InvalidConfigurationException(sprintf("The alias %s". throw new InvalidConfigurationException(sprintf("The alias %s".
" is not defined.", $alias)); " is not defined.", $alias));
} }
// check that the widget is allowed at this place // check that the widget is allowed at this place
if (!$this->isPlaceAllowedForWidget($place, $alias, $container)) { if (!$this->isPlaceAllowedForWidget($place, $alias, $container)) {
throw new \InvalidConfigurationException(sprintf( throw new InvalidConfigurationException(sprintf(
"The widget with alias %s is not allowed at place %s", "The widget with alias %s is not allowed at place %s",
$alias, $alias,
$place $place
)); ));
} }
// get the order, eventually corrected // get the order, eventually corrected
$order = $this->cacheAndGetOrdering($place, $param[self::WIDGET_CONFIG_ORDER]); $order = $this->cacheAndGetOrdering($place, $param[self::WIDGET_CONFIG_ORDER]);
// register the widget with config to the service, using the method // register the widget with config to the service, using the method
// `addWidget` // `addWidget`
if ($this->widgetServices[$alias] instanceof WidgetFactoryInterface) { if ($this->widgetServices[$alias] instanceof WidgetFactoryInterface) {
/* @var $factory WidgetFactoryInterface */ /* @var $factory WidgetFactoryInterface */
$factory = $this->widgetServices[$alias]; $factory = $this->widgetServices[$alias];
// get the config (under the key which equals to widget_alias // get the config (under the key which equals to widget_alias
$config = isset($param[$factory->getWidgetAlias()]) ? $config = isset($param[$factory->getWidgetAlias()]) ?
$param[$factory->getWidgetAlias()] : array(); $param[$factory->getWidgetAlias()] : array();
// register the service into the container // register the service into the container
$serviceId =$this->registerServiceIntoContainer($container, $serviceId =$this->registerServiceIntoContainer($container,
$factory, $place, $order, $config); $factory, $place, $order, $config);
$managerDefinition->addMethodCall(self::WIDGET_MANAGER_METHOD_REGISTER, $managerDefinition->addMethodCall(self::WIDGET_MANAGER_METHOD_REGISTER,
array( array(
$place, $place,
$order, $order,
new Reference($serviceId), new Reference($serviceId),
$config $config
)); ));
} else { } else {
$managerDefinition->addMethodCall(self::WIDGET_MANAGER_METHOD_REGISTER, $managerDefinition->addMethodCall(self::WIDGET_MANAGER_METHOD_REGISTER,
array( array(
$place, $place,
$order, $order,
new Reference($this->widgetServices[$alias]), new Reference($this->widgetServices[$alias]),
array() // the config is alway an empty array array() // the config is alway an empty array
)); ));
@ -212,10 +213,10 @@ abstract class AbstractWidgetsCompilerPass implements CompilerPassInterface
} }
} }
} }
/** /**
* register the service into container. * register the service into container.
* *
* @param ContainerBuilder $container * @param ContainerBuilder $container
* @param WidgetFactoryInterface $factory * @param WidgetFactoryInterface $factory
* @param string $place * @param string $place
@ -231,28 +232,28 @@ abstract class AbstractWidgetsCompilerPass implements CompilerPassInterface
array $config array $config
) { ) {
$serviceId = $factory->getServiceId($container, $place, $order, $config); $serviceId = $factory->getServiceId($container, $place, $order, $config);
$definition = $factory->createDefinition($container, $place, $definition = $factory->createDefinition($container, $place,
$order, $config); $order, $config);
$container->setDefinition($serviceId, $definition); $container->setDefinition($serviceId, $definition);
return $serviceId; return $serviceId;
} }
/** /**
* cache of ordering by place. * cache of ordering by place.
* *
* @internal used by function cacheAndGetOrdering * @internal used by function cacheAndGetOrdering
* @var array * @var array
*/ */
private $cacheOrdering = array(); private $cacheOrdering = array();
/** /**
* check if the ordering has already be used for the given $place and, * check if the ordering has already be used for the given $place and,
* if yes, correct the ordering by incrementation of 1 until the ordering * if yes, correct the ordering by incrementation of 1 until the ordering
* has not be used. * has not be used.
* *
* recursive method. * recursive method.
* *
* @param string $place * @param string $place
* @param float $ordering * @param float $ordering
* @return float * @return float
@ -262,7 +263,7 @@ abstract class AbstractWidgetsCompilerPass implements CompilerPassInterface
if (!array_key_exists($place, $this->cacheOrdering)) { if (!array_key_exists($place, $this->cacheOrdering)) {
$this->cacheOrdering[$place] = array(); $this->cacheOrdering[$place] = array();
} }
// check if the order exists // check if the order exists
if (array_search($ordering, $this->cacheOrdering[$place])) { if (array_search($ordering, $this->cacheOrdering[$place])) {
// if the order exists, increment of 1 and try again // if the order exists, increment of 1 and try again
@ -270,14 +271,14 @@ abstract class AbstractWidgetsCompilerPass implements CompilerPassInterface
} else { } else {
// cache the ordering // cache the ordering
$this->cacheOrdering[$place][] = $ordering; $this->cacheOrdering[$place][] = $ordering;
return $ordering; return $ordering;
} }
} }
/** /**
* get the places where the service is allowed * get the places where the service is allowed
* *
* @param Definition $definition * @param Definition $definition
* @return unknown * @return unknown
*/ */
@ -288,7 +289,7 @@ abstract class AbstractWidgetsCompilerPass implements CompilerPassInterface
->getAllowedPlaces())) { ->getAllowedPlaces())) {
return true; return true;
} }
} else { } else {
$definition = $container->findDefinition($this->widgetServices[$widgetAlias]); $definition = $container->findDefinition($this->widgetServices[$widgetAlias]);
@ -300,17 +301,17 @@ abstract class AbstractWidgetsCompilerPass implements CompilerPassInterface
} }
} }
} }
return false; return false;
} }
/** /**
* This method collect all service tagged with `self::WIDGET_SERVICE_TAG`, and * This method collect all service tagged with `self::WIDGET_SERVICE_TAG`, and
* add also the widget defined by factories * add also the widget defined by factories
* *
* This method also check that the service is correctly tagged with `alias` and * This method also check that the service is correctly tagged with `alias` and
* `places`, or the factory give a correct alias and more than one place. * `places`, or the factory give a correct alias and more than one place.
* *
* @param ContainerBuilder $container * @param ContainerBuilder $container
* @throws InvalidConfigurationException * @throws InvalidConfigurationException
* @throws InvalidArgumentException * @throws InvalidArgumentException
@ -320,13 +321,13 @@ abstract class AbstractWidgetsCompilerPass implements CompilerPassInterface
// first, check the service tagged in service definition // first, check the service tagged in service definition
foreach ($container->findTaggedServiceIds(self::WIDGET_SERVICE_TAG_NAME) as $id => $attrs) { foreach ($container->findTaggedServiceIds(self::WIDGET_SERVICE_TAG_NAME) as $id => $attrs) {
foreach ($attrs as $attr) { foreach ($attrs as $attr) {
// check the alias is set // check the alias is set
if (!isset($attr[self::WIDGET_SERVICE_TAG_ALIAS])) { if (!isset($attr[self::WIDGET_SERVICE_TAG_ALIAS])) {
throw new InvalidConfigurationException("you should add an ".self::WIDGET_SERVICE_TAG_ALIAS. throw new InvalidConfigurationException("you should add an ".self::WIDGET_SERVICE_TAG_ALIAS.
" key on the service ".$id); " key on the service ".$id);
} }
// check the place is set // check the place is set
if (!isset($attr[self::WIDGET_SERVICE_TAG_PLACES])) { if (!isset($attr[self::WIDGET_SERVICE_TAG_PLACES])) {
throw new InvalidConfigurationException(sprintf( throw new InvalidConfigurationException(sprintf(
@ -335,54 +336,54 @@ abstract class AbstractWidgetsCompilerPass implements CompilerPassInterface
$id $id
)); ));
} }
// check the alias does not exists yet // check the alias does not exists yet
if (array_key_exists($attr[self::WIDGET_SERVICE_TAG_ALIAS], $this->widgetServices)) { if (array_key_exists($attr[self::WIDGET_SERVICE_TAG_ALIAS], $this->widgetServices)) {
throw new InvalidArgumentException("a service has already be defined with the ". throw new InvalidArgumentException("a service has already be defined with the ".
self::WIDGET_SERVICE_TAG_ALIAS." ".$attr[self::WIDGET_SERVICE_TAG_ALIAS]); self::WIDGET_SERVICE_TAG_ALIAS." ".$attr[self::WIDGET_SERVICE_TAG_ALIAS]);
} }
// register the service as available // register the service as available
$this->widgetServices[$attr[self::WIDGET_SERVICE_TAG_ALIAS]] = $id; $this->widgetServices[$attr[self::WIDGET_SERVICE_TAG_ALIAS]] = $id;
} }
} }
// add the services defined by factories // add the services defined by factories
foreach($this->widgetFactories as $factory) { foreach($this->widgetFactories as $factory) {
/* @var $factory WidgetFactoryInterface */ /* @var $factory WidgetFactoryInterface */
$alias = $factory->getWidgetAlias(); $alias = $factory->getWidgetAlias();
// check the alias is not empty // check the alias is not empty
if (empty($alias)) { if (empty($alias)) {
throw new \LogicException(sprintf( throw new \LogicException(sprintf(
"the widget factory %s returns an empty alias", "the widget factory %s returns an empty alias",
get_class($factory))); get_class($factory)));
} }
// check the places are not empty // check the places are not empty
if (!is_array($factory->getAllowedPlaces())) { if (!is_array($factory->getAllowedPlaces())) {
throw new \UnexpectedValueException("the method 'getAllowedPlaces' " throw new \UnexpectedValueException("the method 'getAllowedPlaces' "
. "should return a non-empty array. Unexpected value on ". . "should return a non-empty array. Unexpected value on ".
get_class($factory)); get_class($factory));
} }
if (count($factory->getAllowedPlaces()) == 0) { if (count($factory->getAllowedPlaces()) == 0) {
throw new \LengthException("The method 'getAllowedPlaces' should " throw new \LengthException("The method 'getAllowedPlaces' should "
. "return a non-empty array, but returned 0 elements on ". . "return a non-empty array, but returned 0 elements on ".
get_class($factory).'::getAllowedPlaces()'); get_class($factory).'::getAllowedPlaces()');
} }
// check the alias does not exists yet // check the alias does not exists yet
if (array_key_exists($alias, $this->widgetServices)) { if (array_key_exists($alias, $this->widgetServices)) {
throw new InvalidArgumentException("a service has already be defined with the ". throw new InvalidArgumentException("a service has already be defined with the ".
self::WIDGET_SERVICE_TAG_ALIAS." ".$alias); self::WIDGET_SERVICE_TAG_ALIAS." ".$alias);
} }
// register the factory as available // register the factory as available
$this->widgetServices[$factory->getWidgetAlias()] = $factory; $this->widgetServices[$factory->getWidgetAlias()] = $factory;
} }
} }
} }

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Chill\MainBundle\Repository; namespace Chill\MainBundle\Repository;
use Chill\MainBundle\Entity\GroupCenter;
use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\User;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;

View File

@ -5,15 +5,15 @@ namespace Chill\MainBundle\Util;
/** /**
* Utilities to compare date periods * Utilities to compare date periods
* *
* This class allow to compare periods when there are period covering. The * This class allow to compare periods when there are period covering. The
* argument `minCovers` allow to find also when there are more than 2 period * argument `minCovers` allow to find also when there are more than 2 period
* which intersects. * which intersects.
* *
* Example: a team may have maximum 2 leaders on a same period: you will * Example: a team may have maximum 2 leaders on a same period: you will
* find here all periods where there are more than 2 leaders. * find here all periods where there are more than 2 leaders.
* *
* Usage: * Usage:
* *
* ```php * ```php
* $cover = new DateRangeCovering(2); // 2 means we will have periods * $cover = new DateRangeCovering(2); // 2 means we will have periods
* // when there are 2+ periods intersecting * // when there are 2+ periods intersecting
@ -73,7 +73,7 @@ class DateRangeCovering
$this->addToSequence($start->getTimestamp(), $k, null); $this->addToSequence($start->getTimestamp(), $k, null);
$this->addToSequence( $this->addToSequence(
NULL === $end ? PHP_INT_MAX : $end->getTimestamp(), null, $k NULL === $end ? PHP_INT_MAX : $end->getTimestamp(), null, $k
); );
return $this; return $this;
} }
@ -182,7 +182,7 @@ class DateRangeCovering
$foundExisting = false; $foundExisting = false;
list($nStart, $nEnd, $nMetadata) = $intersection; list($nStart, $nEnd, $nMetadata) = $intersection;
\array_walk($intersections, \array_walk($intersections,
function(&$i, $key) use ($nStart, $nEnd, $nMetadata, $foundExisting) { function(&$i, $key) use ($nStart, $nEnd, $nMetadata, $foundExisting) {
if ($foundExisting) { if ($foundExisting) {
return; return;
@ -205,7 +205,7 @@ class DateRangeCovering
{ {
if (!$this->computed) { if (!$this->computed) {
throw new \LogicException(sprintf("You cannot call the method %s before ". throw new \LogicException(sprintf("You cannot call the method %s before ".
"'process'", __METHOD)); "'process'", __METHOD__));
} }
return count($this->intersections) > 0; return count($this->intersections) > 0;
@ -215,7 +215,7 @@ class DateRangeCovering
{ {
if (!$this->computed) { if (!$this->computed) {
throw new \LogicException(sprintf("You cannot call the method %s before ". throw new \LogicException(sprintf("You cannot call the method %s before ".
"'process'", __METHOD)); "'process'", __METHOD__));
} }
return $this->intersections; return $this->intersections;

View File

@ -1,20 +1,7 @@
<?php <?php
/*
* Copyright (C) 2016-2019 Champs-Libres <info@champs-libres.coop> declare(strict_types=1);
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\PersonBundle\CRUD\Controller; namespace Chill\PersonBundle\CRUD\Controller;
use Chill\MainBundle\CRUD\Controller\CRUDController; use Chill\MainBundle\CRUD\Controller\CRUDController;
@ -23,11 +10,8 @@ use Chill\PersonBundle\Entity\Person;
use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RedirectResponse;
use BadMethodCallException;
/**
* Controller for entities attached as one-to-on to a person
*
*/
class OneToOneEntityPersonCRUDController extends CRUDController class OneToOneEntityPersonCRUDController extends CRUDController
{ {
protected function getTemplateFor($action, $entity, Request $request) protected function getTemplateFor($action, $entity, Request $request)
@ -35,11 +19,11 @@ class OneToOneEntityPersonCRUDController extends CRUDController
if (!empty($this->crudConfig[$action]['template'])) { if (!empty($this->crudConfig[$action]['template'])) {
return $this->crudConfig[$action]['template']; return $this->crudConfig[$action]['template'];
} }
switch ($action) { switch ($action) {
case 'new': case 'new':
return '@ChillPerson/CRUD/new.html.twig'; return '@ChillPerson/CRUD/new.html.twig';
case 'edit': case 'edit':
return '@ChillPerson/CRUD/edit.html.twig'; return '@ChillPerson/CRUD/edit.html.twig';
case 'index': case 'index':
return '@ChillPerson/CRUD/index.html.twig'; return '@ChillPerson/CRUD/index.html.twig';
@ -49,41 +33,41 @@ class OneToOneEntityPersonCRUDController extends CRUDController
. "action"); . "action");
} }
} }
protected function getEntity($action, $id, Request $request): ?object protected function getEntity($action, $id, Request $request): ?object
{ {
$entity = parent::getEntity($action, $id, $request); $entity = parent::getEntity($action, $id, $request);
if (NULL === $entity) { if (NULL === $entity) {
$entity = $this->createEntity($action, $request); $entity = $this->createEntity($action, $request);
$person = $this->getDoctrine() $person = $this->getDoctrine()
->getManager() ->getManager()
->getRepository(Person::class) ->getRepository(Person::class)
->find($id); ->find($id);
$entity->setPerson($person); $entity->setPerson($person);
} }
return $entity; return $entity;
} }
protected function onPreFlush(string $action, $entity, FormInterface $form, Request $request) protected function onPreFlush(string $action, $entity, FormInterface $form, Request $request)
{ {
$this->getDoctrine()->getManager()->persist($entity); $this->getDoctrine()->getManager()->persist($entity);
} }
protected function onPostFetchEntity($action, Request $request, $entity): ?Response protected function onPostFetchEntity($action, Request $request, $entity): ?Response
{ {
if (FALSE === $this->getDoctrine()->getManager()->contains($entity)) { if (FALSE === $this->getDoctrine()->getManager()->contains($entity)) {
return new RedirectResponse($this->generateRedirectOnCreateRoute($action, $request, $entity)); return new RedirectResponse($this->generateRedirectOnCreateRoute($action, $request, $entity));
} }
return null; return null;
} }
protected function generateRedirectOnCreateRoute($action, Request $request, $entity) protected function generateRedirectOnCreateRoute($action, Request $request, $entity)
{ {
throw new BadMethodCallException("not implemtented yet"); throw new BadMethodCallException('Not implemented yet.');
} }
} }

View File

@ -1,8 +1,10 @@
<?php <?php
declare(strict_types=1);
namespace Chill\PersonBundle\Repository\Household; namespace Chill\PersonBundle\Repository\Household;
use Chill\PersonBundle\Entity\Household\HouseholdMembers; use Chill\PersonBundle\Entity\Household\HouseholdMember;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
@ -12,6 +14,6 @@ final class HouseholdMembersRepository
public function __construct(EntityManagerInterface $entityManager) public function __construct(EntityManagerInterface $entityManager)
{ {
$this->repository = $entityManager->getRepository(HouseholdMembers::class); $this->repository = $entityManager->getRepository(HouseholdMember::class);
} }
} }