throwCannotCreateResponseException()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Korobovn\CloudPayments\Message\Strategy;
6
7
use Korobovn\CloudPayments\Message\Strategy\Exception\ClassNotFoundException;
8
use Tarampampam\Wrappers\Json;
9
use Korobovn\CloudPayments\Message\Response\ResponseInterface;
10
use Korobovn\CloudPayments\Message\Strategy\Exception\IsNotInstanceOfException;
11
use Korobovn\CloudPayments\Message\Strategy\Specification\SpecificationInterface;
12
use Korobovn\CloudPayments\Message\Strategy\Exception\StrategyCannotCreateResponseException;
13
14
abstract class AbstractStrategy implements StrategyInterface
15
{
16
    /**
17
     * Map matching between the `SpecificationInterface` and the `ResponseInterface`.
18
     * If the raw request meets a `SpecificationInterface`, a `ResponseInterface` is returned
19
     *
20
     * @var array
21
     */
22
    protected $specifications = [];
23
24
    /**
25
     * {@inheritDoc}
26
     */
27
    public function prepareRawResponse(array $raw_response): ResponseInterface
28
    {
29
        foreach ($this->specifications as $specification_class => $response_class) {
30
            if (! class_exists($specification_class)) {
31
                throw new ClassNotFoundException(sprintf(
32
                    'The class %s is not found',
33
                    $specification_class
34
                ));
35
            }
36
            $specification = new $specification_class;
37
            if (! ($specification instanceof SpecificationInterface)) {
38
                throw new IsNotInstanceOfException(sprintf(
39
                    'The class %s is not an instance of %s',
40
                    $specification_class, SpecificationInterface::class
41
                ));
42
            }
43
            if ($specification->isSatisfiedBy($raw_response)) {
44
                if (! class_exists($response_class)) {
45
                    throw new ClassNotFoundException(sprintf(
46
                        'The class %s is not found',
47
                        $response_class
48
                    ));
49
                }
50
                $response = new $response_class;
51
                if (! ($response instanceof ResponseInterface)) {
52
                    throw new IsNotInstanceOfException(sprintf(
53
                        'The class %s is not an instance of %s',
54
                        $response_class, ResponseInterface::class
55
                    ));
56
                }
57
58
                $response->fillObjectFromArray($raw_response);
59
60
                return $response;
61
            }
62
        }
63
64
        throw $this->throwCannotCreateResponseException($raw_response);
65
    }
66
67
    /**
68
     * @param array $response
69
     *
70
     * @return StrategyCannotCreateResponseException
71
     */
72
    protected function throwCannotCreateResponseException(array $response): StrategyCannotCreateResponseException
73
    {
74
        /**
75
         * @todo can do logging $response
76
         */
77
        return new StrategyCannotCreateResponseException(
78
            sprintf('Strategy %s cannot create a response [%s]',
79
                static::class, Json::encode($response)
80
            )
81
        );
82
    }
83
}
84