ExpectationValidator   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 6
Bugs 1 Features 0
Metric Value
eloc 17
dl 0
loc 45
rs 10
c 6
b 1
f 0
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A validateExpectationOrThrowException() 0 5 1
A validateResponseOrThrowException() 0 6 2
A validateScenarioNameOrThrowException() 0 9 4
A responseIsInvalid() 0 6 2
A validateScenarioStateOrThrowException() 0 7 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\Utils\Traits;
20
21
use Mcustiel\Phiremock\Domain\Expectation;
22
use Mcustiel\Phiremock\Domain\HttpResponse;
23
use Psr\Log\LoggerInterface;
24
use RuntimeException;
25
26
trait ExpectationValidator
27
{
28
    protected function validateExpectationOrThrowException(Expectation $expectation, LoggerInterface $logger): void
29
    {
30
        $this->validateResponseOrThrowException($expectation, $logger);
31
        $this->validateScenarioNameOrThrowException($expectation, $logger);
32
        $this->validateScenarioStateOrThrowException($expectation, $logger);
33
    }
34
35
    protected function validateResponseOrThrowException(Expectation $expectation, LoggerInterface $logger): void
36
    {
37
        $this->logger->debug('Validating response');
38
        if ($this->responseIsInvalid($expectation)) {
39
            $logger->error('Invalid response specified in expectation');
40
            throw new RuntimeException('Invalid response specified in expectation');
41
        }
42
    }
43
44
    protected function responseIsInvalid(Expectation $expectation): bool
45
    {
46
        /** @var HttpResponse $response */
47
        $response = $expectation->getResponse();
48
49
        return $response->isHttpResponse() && empty($response->getStatusCode());
50
    }
51
52
    protected function validateScenarioStateOrThrowException(
53
        Expectation $expectation,
54
        LoggerInterface $logger
55
    ): void {
56
        if ($expectation->getResponse()->hasNewScenarioState() && !$expectation->getRequest()->hasScenarioState()) {
57
            $logger->error('Scenario states misconfiguration');
58
            throw new RuntimeException('Trying to set scenario state without specifying scenario previous state');
59
        }
60
    }
61
62
    protected function validateScenarioNameOrThrowException(
63
        Expectation $expectation,
64
        LoggerInterface $logger
65
    ): void {
66
        if (!$expectation->hasScenarioName()
67
            && ($expectation->getRequest()->hasScenarioState() || $expectation->getResponse()->hasNewScenarioState())
68
        ) {
69
            $logger->error('Scenario name related misconfiguration');
70
            throw new RuntimeException('Expecting or trying to set scenario state without specifying scenario name');
71
        }
72
    }
73
}
74