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

CurrentProductPageResolver   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 73
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getCurrentPageWithForm() 0 20 3
A getSimplePage() 0 10 3
A getConfigurablePage() 0 10 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 Sylius\Behat\Page\Admin\Crud\UpdatePageInterface;
15
use Sylius\Behat\Page\Admin\Product\UpdateConfigurableProductPageInterface;
16
use Sylius\Behat\Page\Admin\Product\UpdateSimpleProductPageInterface;
17
use Sylius\Component\Product\Model\ProductInterface;
18
use Webmozart\Assert\Assert;
19
20
/**
21
 * @author Łukasz Chruściel <[email protected]>
22
 */
23
final class CurrentProductPageResolver implements CurrentProductPageResolverInterface
24
{
25
    /**
26
     * @var CurrentPageResolverInterface
27
     */
28
    private $currentPageResolver;
29
30
    /**
31
     * @param CurrentPageResolverInterface $currentPageResolver
32
     */
33
    public function __construct(CurrentPageResolverInterface $currentPageResolver)
34
    {
35
        $this->currentPageResolver = $currentPageResolver;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     * 
41
     * @throws \LogicException
42
     */
43
    public function getCurrentPageWithForm(array $pages, ProductInterface $product = null)
44
    {
45
        $resolvedPage = $this->currentPageResolver->getCurrentPageWithForm($pages);
46
47
        if (!$resolvedPage instanceof UpdatePageInterface) {
48
            return $resolvedPage;
49
        }
50
51
        Assert::notNull($product, 'It is not possible to determine a product edit page without product provided.');
52
53
        if ($product->isSimple()) {
54
            $resolvedPage = $this->getSimplePage($pages);
55
        } else {
56
            $resolvedPage = $this->getConfigurablePage($pages);
57
        }
58
59
        Assert::notNull($resolvedPage, 'Route name could not be matched to provided pages.');
60
61
        return $resolvedPage;
62
    }
63
64
    /**
65
     * @param array $pages
66
     *
67
     * @return UpdateSimpleProductPageInterface|null
68
     */
69
    private function getSimplePage(array $pages)
70
    {
71
        foreach ($pages as $page) {
72
            if ($page instanceof UpdateSimpleProductPageInterface) {
73
                return $page;
74
            }
75
        }
76
77
        return null;
78
    }
79
80
    /**
81
     * @param array $pages
82
     *
83
     * @return UpdateConfigurableProductPageInterface|null
84
     */
85
    private function getConfigurablePage(array $pages)
86
    {
87
        foreach ($pages as $page) {
88
            if ($page instanceof UpdateConfigurableProductPageInterface) {
89
                return $page;
90
            }
91
        }
92
93
        return null;
94
    }
95
}
96