Completed
Push — master ( 6ae77b...70b3ca )
by Artem
01:23
created

processResponseForException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
/*
3
 * This file is part of the StfalconApiBundle.
4
 *
5
 * (c) Stfalcon LLC <stfalcon.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace StfalconStudio\ApiBundle\Service\Exception\ResponseProcessor;
14
15
use StfalconStudio\ApiBundle\Exception\CustomAppExceptionInterface;
16
17
/**
18
 * ExceptionResponseProcessor.
19
 */
20
final class ExceptionResponseProcessor implements ExceptionResponseProcessorInterface
21
{
22
    /** @var CustomAppExceptionResponseProcessorInterface[]|iterable */
23
    private $errorResponseProcessors;
24
25
    /**
26
     * @param iterable|CustomAppExceptionResponseProcessorInterface[] $errorResponseProcessors
27
     */
28
    public function __construct(iterable $errorResponseProcessors)
29
    {
30
        $this->errorResponseProcessors = $errorResponseProcessors;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function processResponseForException(CustomAppExceptionInterface $exception): array
37
    {
38
        $responseProcessor = $this->findAppropriateErrorResponseProcessorForException($exception);
39
40
        if ($responseProcessor instanceof CustomAppExceptionResponseProcessorInterface) {
41
            return $responseProcessor->processResponse($exception);
42
        }
43
44
        return [];
45
    }
46
47
    /**
48
     * @param CustomAppExceptionInterface $exception
49
     *
50
     * @return CustomAppExceptionResponseProcessorInterface|null
51
     */
52
    private function findAppropriateErrorResponseProcessorForException(CustomAppExceptionInterface $exception): ?CustomAppExceptionResponseProcessorInterface
53
    {
54
        foreach ($this->errorResponseProcessors as $errorResponseProcessor) {
55
            /** @var CustomAppExceptionResponseProcessorInterface $errorResponseProcessor */
56
            if ($errorResponseProcessor->supports($exception)) {
57
                return $errorResponseProcessor;
58
            }
59
        }
60
61
        return null;
62
    }
63
}
64