fix: Add a deprecation and the new service that goes with it as replacement.

This commit is contained in:
Pol Dellaiera 2021-11-18 14:15:51 +01:00
parent 370a24d3f5
commit 674bf614dd
No known key found for this signature in database
GPG Key ID: D476DFE9C67467CA
3 changed files with 68 additions and 7 deletions

View File

@ -1,27 +1,42 @@
<?php <?php
declare(strict_types=1);
namespace Chill\MainBundle\Security\Resolver; namespace Chill\MainBundle\Security\Resolver;
class CenterResolverDispatcher use Chill\MainBundle\Entity\Center;
/**
* @deprecated Use CenterResolverManager and its interface CenterResolverManagerInterface
*/
final class CenterResolverDispatcher
{ {
/** /**
* @var iterabble|CenterResolverInterface[] * @var CenterResolverInterface[]
*/ */
private iterable $resolvers = []; private iterable $resolvers;
public function __construct(iterable $resolvers) public function __construct(iterable $resolvers = [])
{ {
$this->resolvers = $resolvers; $this->resolvers = $resolvers;
} }
/** /**
* @param mixed $entity
* @param array|null $options
* @return null|Center|Center[] * @return null|Center|Center[]
*/ */
public function resolveCenter($entity, ?array $options = []) public function resolveCenter($entity, ?array $options = [])
{ {
foreach($this->resolvers as $priority => $resolver) { trigger_deprecation(
'ChillMainBundle',
'dev-master',
'
Use the service CenterResolverManager through the interface CenterResolverManagerInterface.
The new method "CenterResolverManagerInterface::resolveCenters(): array" is available and the typing
has been improved in order to avoid mixing types.
'
);
foreach($this->resolvers as $resolver) {
if ($resolver->supports($entity, $options)) { if ($resolver->supports($entity, $options)) {
return $resolver->resolveCenter($entity, $options); return $resolver->resolveCenter($entity, $options);
} }

View File

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace Chill\MainBundle\Security\Resolver;
use Chill\MainBundle\Entity\Center;
final class CenterResolverManager implements CenterResolverManagerInterface
{
/**
* @var CenterResolverInterface[]
*/
private iterable $resolvers;
public function __construct(iterable $resolvers = [])
{
$this->resolvers = $resolvers;
}
public function resolveCenters($entity, ?array $options = []): array
{
foreach($this->resolvers as $resolver) {
if ($resolver->supports($entity, $options)) {
return (array) $resolver->resolveCenter($entity, $options);
}
}
return [];
}
}

View File

@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace Chill\MainBundle\Security\Resolver;
use Chill\MainBundle\Entity\Center;
interface CenterResolverManagerInterface
{
/**
* @return Center[]
*/
public function resolveCenters($entity, ?array $options = []): array;
}