AddExpectationAction   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A process() 0 7 1
A execute() 0 11 2
A constructErrorResponse() 0 8 1
A constructResponse() 0 7 2
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\Domain\Expectation;
24
use Mcustiel\Phiremock\Server\Model\ExpectationStorage;
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 AddExpectationAction implements ActionInterface
32
{
33
    use ExpectationValidator;
34
35
    /** @var ExpectationStorage */
36
    private $storage;
37
    /** @var RequestToExpectationMapper */
38
    private $converter;
39
    /** @var LoggerInterface */
40
    private $logger;
41
42
    public function __construct(
43
        RequestToExpectationMapper $converter,
44
        ExpectationStorage $storage,
45
        LoggerInterface $logger
46
    ) {
47
        $this->converter = $converter;
48
        $this->logger = $logger;
49
        $this->storage = $storage;
50
    }
51
52
    public function execute(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
53
    {
54
        try {
55
            $object = $this->converter->map($request);
56
57
            return $this->process($response, $object);
58
        } catch (Exception $e) {
59
            $this->logger->error('An unexpected exception occurred: ' . $e->getMessage());
60
            $this->logger->debug($e->__toString());
61
62
            return $this->constructErrorResponse([$e->getMessage()], $response);
63
        }
64
    }
65
66
    private function process(ResponseInterface $response, Expectation $expectation): ResponseInterface
67
    {
68
        $this->logger->debug('process');
69
        $this->validateExpectationOrThrowException($expectation, $this->logger);
70
        $this->storage->addExpectation($expectation);
71
72
        return $this->constructResponse([], $response);
73
    }
74
75
    private function constructResponse(array $listOfErrors, ResponseInterface $response): ResponseInterface
76
    {
77
        if (empty($listOfErrors)) {
78
            return $response->withStatus(201)->withBody(new StringStream('{"result" : "OK"}'));
79
        }
80
81
        return $this->constructErrorResponse($listOfErrors, $response);
82
    }
83
84
    private function constructErrorResponse(array $listOfErrors, ResponseInterface $response): ResponseInterface
85
    {
86
        return $response->withStatus(500)
87
            ->withBody(
88
                new StringStream(
89
                    '{"result" : "ERROR", "details" : '
90
                    . json_encode($listOfErrors)
91
                    . '}'
92
                )
93
            );
94
    }
95
}
96