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 Exception; |
22
|
|
|
use Mcustiel\Phiremock\Common\StringStream; |
23
|
|
|
use Mcustiel\Phiremock\Common\Utils\ExpectationToArrayConverterLocator; |
24
|
|
|
use Mcustiel\Phiremock\Server\Model\ExpectationStorage; |
25
|
|
|
use Psr\Http\Message\ResponseInterface; |
26
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
27
|
|
|
|
28
|
|
|
class ListExpectationsAction implements ActionInterface |
29
|
|
|
{ |
30
|
|
|
/** @var ExpectationStorage */ |
31
|
|
|
private $storage; |
32
|
|
|
/** @var ExpectationToArrayConverterLocator */ |
33
|
|
|
private $converterLocator; |
34
|
|
|
|
35
|
|
|
public function __construct( |
36
|
|
|
ExpectationStorage $storage, |
37
|
|
|
ExpectationToArrayConverterLocator $converterLocator |
38
|
|
|
) { |
39
|
|
|
$this->storage = $storage; |
40
|
|
|
$this->converterLocator = $converterLocator; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** @throws Exception */ |
44
|
|
|
public function execute(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface |
45
|
|
|
{ |
46
|
|
|
$list = []; |
47
|
|
|
foreach ($this->storage->listExpectations() as $expectation) { |
48
|
|
|
$list[] = $this->converterLocator->locate($expectation)->convert($expectation); |
49
|
|
|
} |
50
|
|
|
$jsonList = json_encode($list); |
51
|
|
|
|
52
|
|
|
return $response->withBody(new StringStream($jsonList)) |
53
|
|
|
->withHeader('Content-type', 'application/json'); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|