Completed
Push — master ( 1ecbe4...2ec63c )
by Kamil
21:52
created

CurrentPageResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Behat\Service\Resolver;
13
14
use Behat\Mink\Session;
15
use Sylius\Behat\Page\SymfonyPageInterface;
16
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
17
use Webmozart\Assert\Assert;
18
19
/**
20
 * @author Łukasz Chruściel <[email protected]>
21
 */
22
final class CurrentPageResolver implements CurrentPageResolverInterface
23
{
24
    /**
25
     * @var Session
26
     */
27
    private $session;
28
29
    /**
30
     * @var UrlMatcherInterface
31
     */
32
    private $urlMatcher;
33
34
    /**
35
     * @param Session $session
36
     * @param UrlMatcherInterface $urlMatcher
37
     */
38
    public function __construct(Session $session, UrlMatcherInterface $urlMatcher)
39
    {
40
        $this->session = $session;
41
        $this->urlMatcher = $urlMatcher;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     * 
47
     * @throws \LogicException
48
     */
49
    public function getCurrentPageWithForm(array $pages)
50
    {
51
        $routeParameters = $this->urlMatcher->match(parse_url($this->session->getCurrentUrl(), PHP_URL_PATH));
52
        
53
        Assert::allIsInstanceOf($pages, SymfonyPageInterface::class);
54
55
        foreach ($pages as $page) {
56
            if ($routeParameters['_route'] === $page->getRouteName()) {
57
                return $page;
58
            }
59
        }
60
61
        throw new \LogicException('Route name could not be matched to provided pages.');
62
    }
63
}
64