Completed
Push — master ( c4ad0d...36d425 )
by Nikolay
03:34
created

AbstractStrategy::prepareRawResponse()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 38
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 23
nc 7
nop 1
dl 0
loc 38
rs 8.6186
c 0
b 0
f 0
1
<?php
2
3
namespace Korobovn\CloudPayments\Message\Strategy;
4
5
use Korobovn\CloudPayments\Message\Strategy\Exception\ClassNotFoundException;
6
use Tarampampam\Wrappers\Json;
7
use Korobovn\CloudPayments\Message\Response\ResponseInterface;
8
use Korobovn\CloudPayments\Message\Strategy\Exception\IsNotInstanceOfException;
9
use Korobovn\CloudPayments\Message\Strategy\Specification\SpecificationInterface;
10
use Korobovn\CloudPayments\Message\Strategy\Exception\StrategyCannotCreateResponseException;
11
12
abstract class AbstractStrategy implements StrategyInterface
13
{
14
    /** @var array */
15
    protected $specifications = [];
16
17
    /**
18
     * @param array $raw_response
19
     *
20
     * @return ResponseInterface
21
     * @throws StrategyCannotCreateResponseException
22
     *
23
     * @throws IsNotInstanceOfException
24
     * @throws ClassNotFoundException
25
     */
26
    public function prepareRawResponse(array $raw_response): ResponseInterface
27
    {
28
        foreach ($this->specifications as $specification_class => $response_class) {
29
            if (! class_exists($specification_class)) {
30
                throw new ClassNotFoundException(sprintf(
31
                    'The class %s is not found',
32
                    $specification_class
33
                ));
34
            }
35
            $specification = new $specification_class;
36
            if (! ($specification instanceof SpecificationInterface)) {
37
                throw new IsNotInstanceOfException(sprintf(
38
                    'The class %s is not an instance of %s',
39
                    $specification_class, SpecificationInterface::class
40
                ));
41
            }
42
            if ($specification->isSatisfiedBy($raw_response)) {
43
                if (! class_exists($response_class)) {
44
                    throw new ClassNotFoundException(sprintf(
45
                        'The class %s is not found',
46
                        $response_class
47
                    ));
48
                }
49
                $response = new $response_class;
50
                if (! ($response instanceof ResponseInterface)) {
51
                    throw new IsNotInstanceOfException(sprintf(
52
                        'The class %s is not an instance of %s',
53
                        $response_class, ResponseInterface::class
54
                    ));
55
                }
56
57
                $response->fillFromArray($raw_response);
0 ignored issues
show
Bug introduced by
The method fillFromArray() does not exist on Korobovn\CloudPayments\M...ponse\ResponseInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Korobovn\CloudPayments\M...ponse\ResponseInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
                $response->/** @scrutinizer ignore-call */ 
58
                           fillFromArray($raw_response);
Loading history...
58
59
                return $response;
60
            }
61
        }
62
63
        throw $this->throwCannotCreateResponseException($raw_response);
64
    }
65
66
    /**
67
     * @param array $response
68
     *
69
     * @return StrategyCannotCreateResponseException
70
     */
71
    protected function throwCannotCreateResponseException(array $response): StrategyCannotCreateResponseException
72
    {
73
        /**
74
         * @todo can do logging $response
75
         */
76
        return new StrategyCannotCreateResponseException(
77
            sprintf('Strategy %s cannot create a response [%s]',
78
                static::class, Json::encode($response)
79
            )
80
        );
81
    }
82
}
83