Test Failed
Push — feature/html-code-handler ( 2b7167...3f6bac )
by Arnaud
02:50
created

HtmlHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 5
dl 0
loc 14
rs 10
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 LAG\SmokerBundle\Url\UrlInfo;
12
use Symfony\Component\BrowserKit\Response;
13
use Symfony\Component\DomCrawler\Crawler;
14
15
class HtmlHandler extends AbstractHandler
16
{
17
    /**
18
     * @var MappingResolverInterface
19
     */
20
    private $mappingResolver;
21
22
    /**
23
     * @var EntityManagerInterface
24
     */
25
    private $entityManager;
26
27
    /**
28
     * @var UrlProviderRegistry
29
     */
30
    private $urlProviderRegistry;
31
    /**
32
     * @var RequirementsProviderRegistry
33
     */
34
    private $requirementsProviderRegistry;
35
36
    public function __construct(
37
        MappingResolverInterface $mappingResolver,
38
        EntityManagerInterface $entityManager,
39
        UrlProviderRegistry $urlProviderRegistry,
40
        RequirementsProviderRegistry $requirementsProviderRegistry,
41
        array $configuration = []
42
    ) {
43
        parent::__construct($configuration);
44
45
        $this->mappingResolver = $mappingResolver;
46
        $this->entityManager = $entityManager;
47
        $this->configuration = $configuration;
48
        $this->urlProviderRegistry = $urlProviderRegistry;
49
        $this->requirementsProviderRegistry = $requirementsProviderRegistry;
50
    }
51
52
    public function handle(string $routeName, Crawler $crawler, Client $client, array $options = []): void
53
    {
54
        $configuration = $this->getConfiguration($routeName);
55
        $mapping = $this->mappingResolver->resolve($this->getMappingName($routeName), $routeName);
0 ignored issues
show
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

55
        $mapping = $this->mappingResolver->resolve($this->getMappingName($routeName), /** @scrutinizer ignore-type */ $routeName);
Loading history...
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

55
        $mapping = $this->mappingResolver->resolve(/** @scrutinizer ignore-type */ $this->getMappingName($routeName), $routeName);
Loading history...
Unused Code introduced by
The assignment to $mapping is dead and can be removed.
Loading history...
56
57
        foreach ($configuration as $selector => $content) {
58
            if ($this->isDynamicString($content)) {
59
                /** @var Response $response */
60
                $response = $client->getResponse();
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
61
                $requirements = [];
62
                /** @var UrlInfo $test */
63
                $test = $options['url_info'];
64
                //$response->getHeader();
65
66
                foreach ($this->requirementsProviderRegistry->all() as $requirementsProvider) {
67
                    if (!$requirementsProvider->supports($routeName)) {
68
                        continue;
69
                    }
70
                    $criteria = [];
71
                    $requirements = array_merge($requirements, $requirementsProvider->getRequirements($routeName));
0 ignored issues
show
Unused Code introduced by
The assignment to $requirements is dead and can be removed.
Loading history...
72
73
                    foreach ($requirementsProvider->getRequirements($routeName) as $requirement => $default) {
74
                        if (!key_exists($requirement, $test->getExtra())) {
75
                            continue;
76
                        }
77
                        $criteria[$requirement] = $test->getExtra()[$requirement];
78
                    }
79
80
                    $lol = $requirementsProvider->getRequirementsData($routeName, [
81
                        'where' => $criteria,
82
                    ]);
83
                    var_dump($criteria);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($criteria) looks like debug code. Are you sure you do not want to remove it?
Loading history...
84
85
                    foreach ($lol as $item) {
86
                        var_dump($item);
87
88
                    }
89
                    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...
90
                }
91
92
                $criteria = [];
93
//                var_dump($requirements);
94
//                die('ko');
95
96
                //$t = $this->urlProviderRegistry->match();
97
98
                foreach ($requirements as $requirement => $default) {
99
                    if (!key_exists($requirement, $test->getExtra())) {
100
                        continue;
101
                    }
102
                    $criteria[$requirement] = $test->getExtra()[$requirement];
103
                }
104
105
                var_dump($criteria);
106
                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...
107
//                $provider = $this->registry->get('default');
108
//                $provider->getRequirements($routeName, [
109
//                    'where' => '',
110
//                ]);
111
            } else {
112
                if (false === strpos($crawler->filter($selector)->text(), $content)) {
113
                    throw new Exception();
114
                }
115
            }
116
        }
117
    }
118
119
    /**
120
     * Return the unique name of the response handler.
121
     *
122
     * @return string
123
     */
124
    public function getName(): string
125
    {
126
        return 'html';
127
    }
128
129
    protected function isDynamicString(string $content)
130
    {
131
        if ('{{' !== substr($content, 0, 2)) {
132
            return false;
133
        }
134
135
        if ('}}' !== substr($content, -2)) {
136
            return false;
137
        }
138
139
        return true;
140
    }
141
}
142