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); |
|
|
|
|
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
|
|
|
|