Skip to content

Commit

Permalink
refactor existing simple repos
Browse files Browse the repository at this point in the history
  • Loading branch information
limenet committed Feb 15, 2024
1 parent 4c47dea commit c0ee2e8
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 122 deletions.
13 changes: 0 additions & 13 deletions src/Exception/UnknownInputHandlerException.php

This file was deleted.

13 changes: 0 additions & 13 deletions src/Exception/UnknownRedirectHandlerException.php

This file was deleted.

54 changes: 6 additions & 48 deletions src/Repository/InputHandlerRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,13 @@

namespace Valantic\PimcoreFormsBundle\Repository;

use Valantic\PimcoreFormsBundle\Exception\UnknownInputHandlerException;
use Valantic\PimcoreFormsBundle\Form\InputHandler\InputHandlerInterface;

class InputHandlerRepository
/**
* @extends AbstractRepository<InputHandlerInterface>
*
* @internal
*/
class InputHandlerRepository extends AbstractRepository
{
/**
* @var array<string,InputHandlerInterface>
*/
protected array $inputHandlers;

/**
* @param iterable<InputHandlerInterface> $inputHandlers
*/
public function __construct(iterable $inputHandlers)
{
$this->inputHandlers = $this->iterableToArray($inputHandlers);
}

public function get(string $key): InputHandlerInterface
{
if (str_starts_with($key, '\\')) {
$key = substr($key, 1);
}

if (!array_key_exists($key, $this->inputHandlers)) {
throw new UnknownInputHandlerException($key);
}

return clone $this->inputHandlers[$key];
}

/**
* @param iterable<object> $iterables
*
* @return array<string,InputHandlerInterface>
*/
public function iterableToArray(iterable $iterables): array
{
$arr = [];

foreach ($iterables as $iterable) {
if (!($iterable instanceof InputHandlerInterface)) {
continue;
}

$name = $iterable::class;
$arr[$name] = $iterable;
}

return $arr;
}
}
54 changes: 6 additions & 48 deletions src/Repository/RedirectHandlerRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,13 @@

namespace Valantic\PimcoreFormsBundle\Repository;

use Valantic\PimcoreFormsBundle\Exception\UnknownRedirectHandlerException;
use Valantic\PimcoreFormsBundle\Form\RedirectHandler\RedirectHandlerInterface;

class RedirectHandlerRepository
/**
* @extends AbstractRepository<RedirectHandlerInterface>
*
* @internal
*/
class RedirectHandlerRepository extends AbstractRepository
{
/**
* @var array<string,RedirectHandlerInterface>
*/
protected array $redirectHandlers;

/**
* @param iterable<RedirectHandlerInterface> $redirectHandlers
*/
public function __construct(iterable $redirectHandlers)
{
$this->redirectHandlers = $this->iterableToArray($redirectHandlers);
}

public function get(string $key): RedirectHandlerInterface
{
if (str_starts_with($key, '\\')) {
$key = substr($key, 1);
}

if (!array_key_exists($key, $this->redirectHandlers)) {
throw new UnknownRedirectHandlerException($key);
}

return clone $this->redirectHandlers[$key];
}

/**
* @param iterable<object> $iterables
*
* @return array<string,RedirectHandlerInterface>
*/
public function iterableToArray(iterable $iterables): array
{
$arr = [];

foreach ($iterables as $iterable) {
if (!($iterable instanceof RedirectHandlerInterface)) {
continue;
}

$name = $iterable::class;
$arr[$name] = $iterable;
}

return $arr;
}
}

0 comments on commit c0ee2e8

Please sign in to comment.