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

CurrentPageResolver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
c 1
b 0
f 1
lcom 1
cbo 3
dl 0
loc 42
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getCurrentPageWithForm() 0 14 3
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