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

ExceptionResponseProcessor   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 44
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A processResponseForException() 0 10 2
A findAppropriateErrorResponseProcessorForException() 0 11 3
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