Completed
Push — master ( 7c00aa...d8aae5 )
by Ivannis Suárez
19:41
created

Handler/Resolver/NameOfMessage/ChainResolver.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Cubiche\Core\Bus\Middlewares\Handler\Resolver\NameOfMessage;
13
14
use Cubiche\Core\Bus\Exception\NotFoundException;
15
use Cubiche\Core\Bus\MessageInterface;
16
use Cubiche\Core\Collections\ArrayCollection\ArrayList;
17
18
/**
19
 * ChainResolver class.
20
 *
21
 * @author Ivannis Suárez Jerez <[email protected]>
22
 */
23 View Code Duplication
class ChainResolver implements ResolverInterface
0 ignored issues
show
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
{
25
    /**
26
     * @var ArrayList
27
     */
28
    protected $resolvers;
29
30
    /**
31
     * ChainResolver constructor.
32
     *
33
     * @param array $resolvers
34
     */
35
    public function __construct(array $resolvers)
36
    {
37
        $this->resolvers = new ArrayList();
38
        foreach ($resolvers as $resolver) {
39
            $this->addResolver($resolver);
40
        }
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function resolve(MessageInterface $message)
47
    {
48
        foreach ($this->resolvers as $resolver) {
49
            try {
50
                /* @var ResolverInterface $resolver */
51
                return $resolver->resolve($message);
52
            } catch (\Exception $exception) {
53
            }
54
        }
55
56
        throw $this->notFoundException($message);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    protected function addResolver(ResolverInterface $resolver)
63
    {
64
        $this->resolvers->add($resolver);
65
    }
66
67
    /**
68
     * @param MessageInterface $message
69
     *
70
     * @return NotFoundException
71
     */
72
    protected function notFoundException($message)
73
    {
74
        return NotFoundException::nameOfMessage($message);
75
    }
76
}
77