ResponseCodeHandler   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 32
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 18 5
A getName() 0 3 1
1
<?php
2
3
namespace LAG\SmokerBundle\Response\Handler;
4
5
use Goutte\Client;
6
use LAG\SmokerBundle\Exception\Exception;
7
use Symfony\Component\DomCrawler\Crawler;
8
use Symfony\Component\HttpFoundation\Response;
9
10
class ResponseCodeHandler extends AbstractHandler
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15 4
    public function handle(string $routeName, Crawler $crawler, Client $client, array $options = []): void
16
    {
17 4
        $expectedResponseCode = Response::HTTP_OK;
18 4
        $configuration = $this->configuration[$routeName]['handlers'][$this->getName()];
19
20 4
        if (!is_array($configuration)) {
21 4
            $expectedResponseCode = $configuration;
22 1
        } elseif (key_exists('code', $configuration)) {
23 1
            $expectedResponseCode = $configuration['code'];
24
        }
25
26 4
        if (null === $client->getResponse()) {
27 1
            throw new Exception('The client has no response. It should make a request before handle a response');
28
        }
29 3
        $responseCode = $client->getResponse()->getStatusCode();
30
31 3
        if ((string) $expectedResponseCode !== (string) $responseCode) {
32 1
            throw new Exception('Excepted code '.$expectedResponseCode.', got '.$responseCode.' for route '.$routeName);
33
        }
34 2
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 6
    public function getName(): string
40
    {
41 6
        return 'response_code';
42
    }
43
}
44