ListRequestsAction   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
dl 0
loc 64
rs 10
c 1
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A execute() 0 9 2
A process() 0 9 1
A searchForExecutionsCount() 0 16 3
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\Domain\Expectation;
23
use Mcustiel\Phiremock\Server\Model\RequestStorage;
24
use Mcustiel\Phiremock\Server\Utils\RequestExpectationComparator;
25
use Mcustiel\Phiremock\Server\Utils\RequestToExpectationMapper;
26
use Mcustiel\Phiremock\Server\Utils\Traits\ExpectationValidator;
27
use Psr\Http\Message\ResponseInterface;
28
use Psr\Http\Message\ServerRequestInterface;
29
use Psr\Log\LoggerInterface;
30
31
class ListRequestsAction implements ActionInterface
32
{
33
    use ExpectationValidator;
34
35
    /** @var RequestStorage */
36
    private $requestsStorage;
37
    /** @var RequestExpectationComparator */
38
    private $comparator;
39
    /** @var RequestToExpectationMapper */
40
    private $converter;
41
    /** @var LoggerInterface */
42
    private $logger;
43
44
    public function __construct(
45
        RequestToExpectationMapper $converter,
46
        RequestStorage $storage,
47
        RequestExpectationComparator $comparator,
48
        LoggerInterface $logger
49
    ) {
50
        $this->requestsStorage = $storage;
51
        $this->comparator = $comparator;
52
        $this->converter = $converter;
53
        $this->logger = $logger;
54
    }
55
56
    public function execute(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
57
    {
58
        if ($request->getBody()->__toString() === '') {
59
            $this->logger->info('Received empty body. Creating default');
60
            $request = $request->withBody(new StringStream('{"request": {"url": {"matches": "/.+/"}}, "response": {"statusCode": 200}}'));
61
        }
62
        $object = $this->converter->map($request);
63
64
        return $this->process($response, $object);
65
    }
66
67
    public function process(ResponseInterface $response, Expectation $expectation): ResponseInterface
68
    {
69
        $executions = $this->searchForExecutionsCount($expectation);
70
        $this->logger->debug('Listed ' . \count($executions) . ' request matching the expectation');
71
72
        return $response
73
            ->withStatus(200)
74
            ->withHeader('Content-Type', 'application/json')
75
            ->withBody(new StringStream(json_encode($executions)));
76
    }
77
78
    /** @return array[] */
79
    private function searchForExecutionsCount(Expectation $expectation): array
80
    {
81
        $executions = [];
82
        foreach ($this->requestsStorage->listRequests() as $request) {
83
            if ($this->comparator->equals($request, $expectation)) {
84
                $executions[] = [
85
                    'method'  => $request->getMethod(),
86
                    'url'     => (string) $request->getUri(),
87
                    'headers' => $request->getHeaders(),
88
                    'cookies' => $request->getCookieParams(),
89
                    'body'    => (string) $request->getBody(),
90
                ];
91
            }
92
        }
93
94
        return $executions;
95
    }
96
}
97