Test Failed
Push — feature/html-code-handler ( 0406d3...1467c9 )
by Arnaud
02:28
created

HtmlHandler::handle()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 36
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 17
nc 9
nop 4
dl 0
loc 36
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
namespace LAG\SmokerBundle\Response\Handler;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Goutte\Client;
7
use LAG\SmokerBundle\Exception\Exception;
8
use LAG\SmokerBundle\Url\Registry\UrlProviderRegistry;
9
use LAG\SmokerBundle\Url\Requirements\Mapping\MappingResolverInterface;
10
use LAG\SmokerBundle\Url\Requirements\Registry\RequirementsProviderRegistry;
11
use Symfony\Component\BrowserKit\Response;
12
use Symfony\Component\DomCrawler\Crawler;
13
14
class HtmlHandler extends AbstractHandler
15
{
16
    /**
17
     * @var MappingResolverInterface
18
     */
19
    private $mappingResolver;
20
21
    /**
22
     * @var EntityManagerInterface
23
     */
24
    private $entityManager;
25
26
    /**
27
     * @var UrlProviderRegistry
28
     */
29
    private $urlProviderRegistry;
30
    /**
31
     * @var RequirementsProviderRegistry
32
     */
33
    private $requirementsProviderRegistry;
34
35
    public function __construct(
36
        MappingResolverInterface $mappingResolver,
37
        EntityManagerInterface $entityManager,
38
        UrlProviderRegistry $urlProviderRegistry,
39
        RequirementsProviderRegistry $requirementsProviderRegistry,
40
        array $configuration = []
41
    ) {
42
        parent::__construct($configuration);
43
44
        $this->mappingResolver = $mappingResolver;
45
        $this->entityManager = $entityManager;
46
        $this->configuration = $configuration;
47
        $this->urlProviderRegistry = $urlProviderRegistry;
48
        $this->requirementsProviderRegistry = $requirementsProviderRegistry;
49
    }
50
51
    public function handle(string $routeName, Crawler $crawler, Client $client, array $options = []): void
52
    {
53
        $configuration = $this->getConfiguration($routeName);
54
        $mapping = $this->mappingResolver->resolve($this->getMappingName($routeName), $routeName);
0 ignored issues
show
Bug introduced by
It seems like $this->getMappingName($routeName) can also be of type null; however, parameter $routeName of LAG\SmokerBundle\Url\Req...verInterface::resolve() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
        $mapping = $this->mappingResolver->resolve(/** @scrutinizer ignore-type */ $this->getMappingName($routeName), $routeName);
Loading history...
Bug introduced by
$routeName of type string is incompatible with the type boolean expected by parameter $filterMappingData of LAG\SmokerBundle\Url\Req...verInterface::resolve(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
        $mapping = $this->mappingResolver->resolve($this->getMappingName($routeName), /** @scrutinizer ignore-type */ $routeName);
Loading history...
55
56
        foreach ($configuration as $selector => $content) {
57
            if ($this->isDynamicString($content)) {
58
                /** @var Response $response */
59
                $response = $client->getResponse();
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
60
                $requirements = [];
61
                //$response->getHeader();
62
63
                foreach ($this->requirementsProviderRegistry->all() as $requirementsProvider) {
64
                    if (!$requirementsProvider->supports($routeName)) {
65
                        continue;
66
                    }
67
                    $requirements = array_merge($requirements, $requirementsProvider->getRequirements($routeName));
68
                }
69
                $criteria = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $criteria is dead and can be removed.
Loading history...
70
71
                foreach ($requirements as $requirement) {
72
73
                }
74
75
76
77
78
                var_dump($mapping, $client->getResponse());
0 ignored issues
show
Security Debugging Code introduced by
var_dump($mapping, $client->getResponse()) looks like debug code. Are you sure you do not want to remove it?
Loading history...
79
                die;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
80
//                $provider = $this->registry->get('default');
81
//                $provider->getRequirements($routeName, [
82
//                    'where' => '',
83
//                ]);
84
            } else {
85
                if (false === strpos($crawler->filter($selector)->text(), $content)) {
86
                    throw new Exception();
87
                }
88
            }
89
        }
90
    }
91
92
    /**
93
     * Return the unique name of the response handler.
94
     *
95
     * @return string
96
     */
97
    public function getName(): string
98
    {
99
        return 'html';
100
    }
101
102
    protected function isDynamicString(string $content)
103
    {
104
        if ('{{' !== substr($content, 0, 2)) {
105
            return false;
106
        }
107
108
        if ('}}' !== substr($content, -2)) {
109
            return false;
110
        }
111
112
        return true;
113
    }
114
}
115