Completed
Push — master ( b42639...616c5a )
by Paweł
24:03 queued 09:34
created

SummaryPage::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 1
eloc 7
nc 1
nop 4
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\Page\Shop\Checkout;
13
14
use Behat\Mink\Session;
15
use Sylius\Behat\Page\SymfonyPage;
16
use Sylius\Behat\Service\Accessor\TableAccessorInterface;
17
use Sylius\Component\Core\Model\AddressInterface;
18
use Symfony\Component\Intl\Intl;
19
use Symfony\Component\Routing\RouterInterface;
20
21
/**
22
 * @author Mateusz Zalewski <[email protected]>
23
 */
24
class SummaryPage extends SymfonyPage implements SummaryPageInterface
25
{
26
    /**
27
     * @var TableAccessorInterface
28
     */
29
    private $tableAccessor;
30
31
    /**
32
     * @param Session $session
33
     * @param array $parameters
34
     * @param RouterInterface $router
35
     * @param TableAccessorInterface $tableAccessor
36
     */
37
    public function __construct(
38
        Session $session,
39
        array $parameters,
40
        RouterInterface $router,
41
        TableAccessorInterface $tableAccessor
42
    ) {
43
        parent::__construct($session, $parameters, $router);
44
45
        $this->tableAccessor = $tableAccessor;
46
    }
47
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function getRouteName()
53
    {
54
        return 'sylius_shop_checkout_summary';
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function hasItemWithProductAndQuantity($productName, $quantity)
61
    {
62
        $table = $this->getElement('items_table');
63
64
        try {
65
            $this->tableAccessor->getRowWithFields($table, ['item' => $productName, 'qty' => $quantity]);
66
        } catch (\InvalidArgumentException $exception) {
67
            return false;
68
        }
69
70
        return true;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function hasShippingAddress(AddressInterface $address)
77
    {
78
        $shippingAddress = $this->getElement('shipping_address')->getText();
79
80
        return $this->isAddressValid($shippingAddress, $address);
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function hasBillingAddress(AddressInterface $address)
87
    {
88
        $billingAddress = $this->getElement('billing_address')->getText();
89
90
        return $this->isAddressValid($billingAddress, $address);
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    protected function getDefinedElements()
97
    {
98
        return array_merge(parent::getDefinedElements(), [
99
            'billing_address' => '#addresses div:contains("Billing address") address',
100
            'shipping_address' => '#addresses div:contains("Shipping address") address',
101
            'items_table' => '#items table',
102
        ]);
103
    }
104
105
    /**
106
     * @param string $displayedAddress
107
     * @param AddressInterface $address
108
     *
109
     * @return bool
110
     */
111
    private function isAddressValid($displayedAddress, AddressInterface $address)
112
    {
113
        return
114
            $this->hasAddressPart($displayedAddress, $address->getCompany(), true) &&
115
            $this->hasAddressPart($displayedAddress, $address->getFirstName()) &&
116
            $this->hasAddressPart($displayedAddress, $address->getLastName()) &&
117
            $this->hasAddressPart($displayedAddress, $address->getPhoneNumber(), true) &&
118
            $this->hasAddressPart($displayedAddress, $address->getStreet()) &&
119
            $this->hasAddressPart($displayedAddress, $address->getCity()) &&
120
            $this->hasAddressPart($displayedAddress, $address->getProvinceCode(), true) &&
121
            $this->hasAddressPart($displayedAddress, $this->getCountryName($address->getCountryCode())) &&
122
            $this->hasAddressPart($displayedAddress, $address->getPostcode())
123
        ;
124
    }
125
126
    /**
127
     * @param string $address
128
     * @param string $addressPart
129
     *
130
     * @return bool
131
     */
132
    private function hasAddressPart($address, $addressPart, $optional = false)
133
    {
134
        if ($optional && null === $addressPart) {
135
            return true;
136
        }
137
138
        return false !== strpos($address, $addressPart);
139
    }
140
141
    /**
142
     * @param string $countryCode
143
     *
144
     * @return string
145
     */
146
    private function getCountryName($countryCode)
147
    {
148
        return strtoupper(Intl::getRegionBundle()->getCountryName($countryCode, 'en'));
149
    }
150
}
151