|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of Phiremock. |
|
4
|
|
|
* |
|
5
|
|
|
* Phiremock is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
|
7
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
8
|
|
|
* (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* Phiremock is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU General Public License |
|
16
|
|
|
* along with Phiremock. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace Mcustiel\Phiremock\Server\Actions; |
|
20
|
|
|
|
|
21
|
|
|
use Mcustiel\Phiremock\Common\StringStream; |
|
22
|
|
|
use Mcustiel\Phiremock\Common\Utils\ArrayToScenarioStateInfoConverter; |
|
23
|
|
|
use Mcustiel\Phiremock\Domain\ScenarioStateInfo; |
|
24
|
|
|
use Mcustiel\Phiremock\Server\Model\ScenarioStorage; |
|
25
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
26
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
27
|
|
|
use Psr\Log\LoggerInterface; |
|
28
|
|
|
|
|
29
|
|
|
class SetScenarioStateAction implements ActionInterface |
|
30
|
|
|
{ |
|
31
|
|
|
/** @var ScenarioStorage */ |
|
32
|
|
|
private $storage; |
|
33
|
|
|
|
|
34
|
|
|
/** @var ArrayToScenarioStateInfoConverter */ |
|
35
|
|
|
private $converter; |
|
36
|
|
|
|
|
37
|
|
|
/** @var LoggerInterface */ |
|
38
|
|
|
private $logger; |
|
39
|
|
|
|
|
40
|
|
|
public function __construct( |
|
41
|
|
|
ArrayToScenarioStateInfoConverter $requestBuilder, |
|
42
|
|
|
ScenarioStorage $storage, |
|
43
|
|
|
LoggerInterface $logger |
|
44
|
|
|
) { |
|
45
|
|
|
$this->converter = $requestBuilder; |
|
46
|
|
|
$this->storage = $storage; |
|
47
|
|
|
$this->logger = $logger; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** @throws \Exception */ |
|
51
|
|
|
public function execute(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface |
|
52
|
|
|
{ |
|
53
|
|
|
$state = $this->parseRequestObject($request); |
|
54
|
|
|
if ($state->getScenarioName() === null || $state->getScenarioState() === null) { |
|
55
|
|
|
return $response |
|
56
|
|
|
->withStatus(400) |
|
57
|
|
|
->withHeader('Content-Type', 'application/json') |
|
58
|
|
|
->withBody( |
|
59
|
|
|
new StringStream( |
|
60
|
|
|
json_encode(['error' => 'Scenario name or state is not set']) |
|
61
|
|
|
) |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$this->storage->setScenarioState($state); |
|
66
|
|
|
$this->logger->debug( |
|
67
|
|
|
sprintf( |
|
68
|
|
|
'Scenario %s state is set to %s', |
|
69
|
|
|
$state->getScenarioName()->asString(), |
|
70
|
|
|
$state->getScenarioState()->asString() |
|
71
|
|
|
) |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
|
|
return $response |
|
75
|
|
|
->withStatus(200) |
|
76
|
|
|
->withHeader('Content-Type', 'application/json') |
|
77
|
|
|
->withBody($request->getBody()); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** @throws \Exception */ |
|
81
|
|
|
private function parseRequestObject(ServerRequestInterface $request): ScenarioStateInfo |
|
82
|
|
|
{ |
|
83
|
|
|
$object = $this->converter->convert( |
|
84
|
|
|
$this->parseJsonBody($request) |
|
85
|
|
|
); |
|
86
|
|
|
$this->logger->debug('Parsed scenario state: ' . var_export($object, true)); |
|
87
|
|
|
|
|
88
|
|
|
return $object; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** @throws \Exception */ |
|
92
|
|
|
private function parseJsonBody(ServerRequestInterface $request): array |
|
93
|
|
|
{ |
|
94
|
|
|
$body = $request->getBody()->__toString(); |
|
95
|
|
|
$this->logger->debug($body); |
|
96
|
|
|
if ($request->hasHeader('Content-Encoding') && 'base64' === implode(',', $request->getHeader('Content-Encoding'))) { |
|
97
|
|
|
$body = base64_decode($body, true); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$bodyJson = @json_decode($body, true); |
|
101
|
|
|
if (\JSON_ERROR_NONE !== json_last_error()) { |
|
102
|
|
|
throw new \Exception(json_last_error_msg()); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $bodyJson; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|