Completed
Pull Request — master (#16)
by Arnaud
48:07 queued 29:02
created

ResponseCodeHandler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

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