Code Duplication    Length = 51-51 lines in 2 locations

src/Resolver/DelegateResolver.php 1 location

@@ 11-61 (lines=51) @@
8
use Psr\Container\NotFoundExceptionInterface;
9
use Psr\Container\ContainerExceptionInterface;
10
11
class DelegateResolver implements DelegateResolverInterface
12
{
13
14
    /**
15
     * @var ContainerInterface
16
     */
17
    protected $container;
18
19
    /**
20
     * @param ContainerInterface $container
21
     */
22
    public function __construct(ContainerInterface $container)
23
    {
24
        $this->container = $container;
25
    }
26
27
    /**
28
     * @param string|callable|DelegateInterface $ref
29
     * @return DelegateInterface
30
     * @throws UnresolvedException
31
     */
32
    public function resolve($ref)
33
    {
34
        if ($ref instanceof DelegateInterface) {
35
            return $ref;
36
        }
37
38
        if (is_callable($ref)) {
39
            return new CallableDelegate($ref);
40
        }
41
42
        if (!is_string($ref)) {
43
            throw new \InvalidArgumentException("Argument 1 \$ref must be one of an instance of DelegateInterface, a callable or string.");
44
        }
45
46
        try {
47
            $entry = $this->container->get($ref);
48
        } catch (NotFoundExceptionInterface $e) {
49
            throw new UnresolvedException('Could not found an entry from the container.', 0, $e);
50
        } catch (ContainerExceptionInterface $e) {
51
            throw new UnresolvedException('Something wrong with the container.', 0, $e);
52
        }
53
54
        if (!$entry instanceof DelegateInterface) {
55
            $type = is_object($entry) ? get_class($entry) : gettype($entry);
56
            throw new UnresolvedException("Expected container returns an instance of DelegateInterface, {$type} given.");
57
        }
58
59
        return $entry;
60
    }
61
}
62

src/Resolver/MiddlewareResolver.php 1 location

@@ 11-61 (lines=51) @@
8
use Psr\Container\NotFoundExceptionInterface;
9
use Psr\Container\ContainerExceptionInterface;
10
11
class MiddlewareResolver implements MiddlewareResolverInterface
12
{
13
14
    /**
15
     * @var ContainerInterface
16
     */
17
    protected $container;
18
19
    /**
20
     * @param ContainerInterface $container
21
     */
22
    public function __construct(ContainerInterface $container)
23
    {
24
        $this->container = $container;
25
    }
26
27
    /**
28
     * @param string|callable|MiddlewareInterface $ref
29
     * @return MiddlewareInterface
30
     * @throws UnresolvedException
31
     */
32
    public function resolve($ref)
33
    {
34
        if ($ref instanceof MiddlewareInterface) {
35
            return $ref;
36
        }
37
38
        if (is_callable($ref)) {
39
            return new CallableMiddleware($ref);
40
        }
41
42
        if (!is_string($ref)) {
43
            throw new \InvalidArgumentException("Argument 1 \$ref must be one of an instance of MiddlewareInterface, a callable or string.");
44
        }
45
46
        try {
47
            $entry = $this->container->get($ref);
48
        } catch (NotFoundExceptionInterface $e) {
49
            throw new UnresolvedException('Could not found an entry from the container.', 0, $e);
50
        } catch (ContainerExceptionInterface $e) {
51
            throw new UnresolvedException('Something wrong with the container.', 0, $e);
52
        }
53
54
        if (!$entry instanceof MiddlewareInterface) {
55
            $type = is_object($entry) ? get_class($entry) : gettype($entry);
56
            throw new UnresolvedException("Expected container returns an instance of MiddlewareInterface, {$type} given.");
57
        }
58
59
        return $entry;
60
    }
61
}
62