Completed
Push — master ( 8b479a...c1c0ed )
by Kamil
18:11
created

PageObjectFactory::createPage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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\PageObjectExtension\Factory;
13
14
use Behat\Mink\Mink;
15
use SensioLabs\Behat\PageObjectExtension\PageObject\Factory;
16
use SensioLabs\Behat\PageObjectExtension\PageObject\Factory\ClassNameResolver;
17
use Sylius\Behat\PageObjectExtension\Page\SymfonyPage;
18
use Symfony\Cmf\Component\Routing\ChainRouterInterface;
19
20
/**
21
 * @author Arkadiusz Krakowiak <[email protected]>
22
 */
23
final class PageObjectFactory implements Factory
24
{
25
    /**
26
     * @var Factory
27
     */
28
    private $decoratedFactory;
29
30
    /**
31
     * @var Mink
32
     */
33
    private $mink;
34
35
    /**
36
     * @var ClassNameResolver
37
     */
38
    private $classNameResolver;
39
40
    /**
41
     * @var array
42
     */
43
    private $pageParameters = [];
44
45
    /**
46
     * @var ChainRouterInterface
47
     */
48
    private $router;
49
50
    /**
51
     * @param Factory $decoratedFactory
52
     * @param Mink $mink
53
     * @param ClassNameResolver $classNameResolver
54
     * @param array $pageParameters
55
     * @param ChainRouterInterface $router
56
     */
57
    public function __construct(
58
        Factory $decoratedFactory,
59
        Mink $mink,
60
        ClassNameResolver $classNameResolver,
61
        array $pageParameters,
62
        ChainRouterInterface $router
63
    ) {
64
        $this->decoratedFactory = $decoratedFactory;
65
        $this->mink = $mink;
66
        $this->classNameResolver = $classNameResolver;
67
        $this->pageParameters = $pageParameters;
68
        $this->router = $router;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function createPage($name)
75
    {
76
        $pageClass = $this->classNameResolver->resolvePage($name);
77
78
        return $this->create($pageClass);
79
    }
80
81
    /**
82
    * {@inheritdoc}
83
    */
84
    public function createElement($name)
85
    {
86
        return $this->decoratedFactory->createElement($name);
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function createInlineElement($selector)
93
    {
94
        return $this->decoratedFactory->createInlineElement($selector);
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function create($pageObjectClass)
101
    {
102
        try {
103
            return $this->instantiatePage($pageObjectClass);
104
        } catch (\InvalidArgumentException $exception) {
105
            return $this->decoratedFactory->create($pageObjectClass);
106
        }
107
    }
108
109
    /**
110
     * @param string $pageClass
111
     *
112
     * @return SymfonyPage
113
     */
114
    private function instantiatePage($pageClass)
115
    {
116
        if (!is_subclass_of($pageClass, SymfonyPage::class)) {
117
            throw new \InvalidArgumentException(sprintf('Invalid page object class: %s, to use this factory you need to extend SymfonyPage', $pageClass));
118
        }
119
120
        return new $pageClass($this->mink->getSession(), $this, $this->pageParameters, $this->router);
121
    }
122
}
123