Test Failed
Push — feature/html-code-handler ( 1467c9...2b7167 )
by Arnaud
02:31
created

HtmlHandler::isDynamicString()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 11
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 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);
55
56
        foreach ($configuration as $selector => $content) {
57
            if ($this->isDynamicString($content)) {
58
                /** @var Response $response */
59
                $response = $client->getResponse();
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 = [];
70
                $t = $this->urlProviderRegistry->match()
71
72
                foreach ($requirements as $requirement) {
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_FOREACH on line 72 at column 16
Loading history...
73
74
                }
75
76
77
78
79
                var_dump($mapping, $client->getResponse());
80
                die;
81
//                $provider = $this->registry->get('default');
82
//                $provider->getRequirements($routeName, [
83
//                    'where' => '',
84
//                ]);
85
            } else {
86
                if (false === strpos($crawler->filter($selector)->text(), $content)) {
87
                    throw new Exception();
88
                }
89
            }
90
        }
91
    }
92
93
    /**
94
     * Return the unique name of the response handler.
95
     *
96
     * @return string
97
     */
98
    public function getName(): string
99
    {
100
        return 'html';
101
    }
102
103
    protected function isDynamicString(string $content)
104
    {
105
        if ('{{' !== substr($content, 0, 2)) {
106
            return false;
107
        }
108
109
        if ('}}' !== substr($content, -2)) {
110
            return false;
111
        }
112
113
        return true;
114
    }
115
}
116