Completed
Push — master ( 0dbe68...17d58d )
by Florian
04:25
created

Review::getCarrierName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
/**
4
 * PAYONE Magento 2 Connector is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * PAYONE Magento 2 Connector is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with PAYONE Magento 2 Connector. If not, see <http://www.gnu.org/licenses/>.
16
 *
17
 * PHP version 5
18
 *
19
 * @category  Payone
20
 * @package   Payone_Magento2_Plugin
21
 * @author    FATCHIP GmbH <[email protected]>
22
 * @copyright 2003 - 2017 Payone GmbH
23
 * @license   <http://www.gnu.org/licenses/> GNU Lesser General Public License
24
 * @link      http://www.payone.de
25
 */
26
27
namespace Payone\Core\Block\Onepage;
28
29
use Magento\Checkout\Model\Session;
30
use Magento\Framework\Pricing\PriceCurrencyInterface;
31
use Magento\Quote\Model\Quote\Address\Rate;
32
use Magento\Quote\Model\Quote\Address;
33
use Magento\Framework\DataObject;
34
35
/**
36
 * Paypal Express Onepage checkout block
37
 *
38
 * @author      Magento Core Team <[email protected]>
39
 */
40
class Review extends \Magento\Framework\View\Element\Template
41
{
42
    /**
43
     * @var \Magento\Quote\Model\Quote
44
     */
45
    protected $_quote;
46
47
    /**
48
     * @var \Magento\Quote\Model\Quote\Address
49
     */
50
    protected $_address;
51
52
    /**
53
     * @var \Magento\Customer\Model\Address\Config
54
     */
55
    protected $_addressConfig;
56
57
    /**
58
     * Currently selected shipping rate
59
     *
60
     * @var Rate
61
     */
62
    protected $_currentShippingRate = null;
63
64
    /**
65
     * Paypal controller path
66
     *
67
     * @var string
68
     */
69
    protected $_controllerPath = 'payone/onepage';
70
71
    /**
72
     * @var \Magento\Tax\Helper\Data
73
     */
74
    protected $_taxHelper;
75
76
    /**
77
     * @var PriceCurrencyInterface
78
     */
79
    protected $priceCurrency;
80
81
    /**
82
     * Checkout session object
83
     *
84
     * @var Session
85
     */
86
    protected $checkoutSession;
87
88
    /**
89
     * @param \Magento\Framework\View\Element\Template\Context $context
90
     * @param \Magento\Tax\Helper\Data $taxHelper
91
     * @param \Magento\Customer\Model\Address\Config $addressConfig
92
     * @param PriceCurrencyInterface $priceCurrency
93
     * @param Session $checkoutSession
94
     * @param array $data
95
     */
96
    public function __construct(
97
        \Magento\Framework\View\Element\Template\Context $context,
98
        \Magento\Tax\Helper\Data $taxHelper,
99
        \Magento\Customer\Model\Address\Config $addressConfig,
100
        PriceCurrencyInterface $priceCurrency,
101
        Session $checkoutSession,
102
        array $data = []
103
    ) {
104
        parent::__construct($context, $data);
105
        $this->priceCurrency = $priceCurrency;
106
        $this->_taxHelper = $taxHelper;
107
        $this->_addressConfig = $addressConfig;
108
        $this->checkoutSession = $checkoutSession;
109
        $this->getQuote(); // fill quote property
110
    }
111
112
    /**
113
     * Return checkout quote object
114
     *
115
     * @return \Magento\Quote\Model\Quote
116
     */
117
    protected function getQuote()
118
    {
119
        if (!$this->_quote) {
120
            $this->_quote = $this->checkoutSession->getQuote();
121
        }
122
        return $this->_quote;
123
    }
124
125
    /**
126
     * Return quote billing address
127
     *
128
     * @return Address
129
     */
130
    public function getBillingAddress()
131
    {
132
        return $this->_quote->getBillingAddress();
133
    }
134
135
    /**
136
     * Return quote shipping address
137
     *
138
     * @return false|Address
139
     */
140
    public function getShippingAddress()
141
    {
142
        if ($this->_quote->getIsVirtual()) {
143
            return false;
144
        }
145
        return $this->_quote->getShippingAddress();
146
    }
147
148
    /**
149
     * Get HTML output for specified address
150
     *
151
     * @param Address $address
152
     * @return string
153
     */
154
    public function renderAddress(Address $address)
155
    {
156
        /** @var \Magento\Customer\Block\Address\Renderer\RendererInterface $renderer */
157
        $renderer = $this->_addressConfig->getFormatByCode('html')->getRenderer();
158
        $addressData = \Magento\Framework\Convert\ConvertArray::toFlatArray($address->getData());
159
        return $renderer->renderArray($addressData);
160
    }
161
162
    /**
163
     * Return carrier name from config, base on carrier code
164
     *
165
     * @param string $carrierCode
166
     * @return string
167
     */
168
    public function getCarrierName($carrierCode)
169
    {
170
        if ($name = $this->_scopeConfig->getValue("carriers/{$carrierCode}/title", \Magento\Store\Model\ScopeInterface::SCOPE_STORE)) {
171
            return $name;
172
        }
173
        return $carrierCode;
174
    }
175
176
    /**
177
     * Get either shipping rate code or empty value on error
178
     *
179
     * @param DataObject $rate
180
     * @return string
181
     */
182
    public function renderShippingRateValue(DataObject $rate)
183
    {
184
        if ($rate->getErrorMessage()) {
185
            return '';
186
        }
187
        return $rate->getCode();
188
    }
189
190
    /**
191
     * Get shipping rate code title and its price or error message
192
     *
193
     * @param DataObject $rate
194
     * @param string $format
195
     * @param string $inclTaxFormat
196
     * @return string
197
     */
198
    public function renderShippingRateOption(DataObject $rate, $format = '%s - %s%s', $inclTaxFormat = ' (%s %s)')
199
    {
200
        $renderedInclTax = '';
201
        if ($rate->getErrorMessage()) {
202
            $price = $rate->getErrorMessage();
203
        } else {
204
            $price = $this->_getShippingPrice(
205
                $rate->getPrice(),
206
                $this->_taxHelper->displayShippingPriceIncludingTax()
207
            );
208
209
            $incl = $this->_getShippingPrice($rate->getPrice(), true);
210
            if ($incl != $price && $this->_taxHelper->displayShippingBothPrices()) {
211
                $renderedInclTax = sprintf($inclTaxFormat, $this->escapeHtml(__('Incl. Tax')), $incl);
212
            }
213
        }
214
        return sprintf($format, $this->escapeHtml($rate->getMethodTitle()), $price, $renderedInclTax);
215
    }
216
217
    /**
218
     * Getter for current shipping rate
219
     *
220
     * @return Rate
221
     */
222
    public function getCurrentShippingRate()
223
    {
224
        return $this->_currentShippingRate;
225
    }
226
227
    /**
228
     * Get quote email
229
     *
230
     * @return string
231
     */
232
    public function getEmail()
233
    {
234
        $billingAddress = $this->getBillingAddress();
235
        return $billingAddress ? $billingAddress->getEmail() : '';
236
    }
237
238
    /**
239
     * Return formatted shipping price
240
     *
241
     * @param float $price
242
     * @param bool $isInclTax
243
     * @return string
244
     */
245
    protected function _getShippingPrice($price, $isInclTax)
246
    {
247
        return $this->_formatPrice($this->_taxHelper->getShippingPrice($price, $isInclTax, $this->_address));
248
    }
249
250
    /**
251
     * Format price base on store convert price method
252
     *
253
     * @param float $price
254
     * @return string
255
     */
256
    protected function _formatPrice($price)
257
    {
258
        return $this->priceCurrency->convertAndFormat(
259
            $price,
260
            true,
261
            PriceCurrencyInterface::DEFAULT_PRECISION,
262
            $this->_quote->getStore()
263
        );
264
    }
265
266
    /**
267
     * Retrieve payment method and assign additional template values
268
     *
269
     * @return $this
270
     */
271
    protected function _beforeToHtml()
272
    {
273
        $methodInstance = $this->_quote->getPayment()->getMethodInstance();
274
        $this->setPaymentMethodTitle($methodInstance->getTitle());
0 ignored issues
show
Documentation Bug introduced by
The method setPaymentMethodTitle does not exist on object<Payone\Core\Block\Onepage\Review>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
275
276
        $this->setShippingRateRequired(true);
0 ignored issues
show
Documentation Bug introduced by
The method setShippingRateRequired does not exist on object<Payone\Core\Block\Onepage\Review>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
277
        if ($this->_quote->getIsVirtual()) {
278
            $this->setShippingRateRequired(false);
0 ignored issues
show
Documentation Bug introduced by
The method setShippingRateRequired does not exist on object<Payone\Core\Block\Onepage\Review>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
279
        } else {
280
            // prepare shipping rates
281
            $this->_address = $this->_quote->getShippingAddress();
282
            $groups = $this->_address->getGroupedAllShippingRates();
283
            if ($groups && $this->_address) {
284
                $this->setShippingRateGroups($groups);
0 ignored issues
show
Documentation Bug introduced by
The method setShippingRateGroups does not exist on object<Payone\Core\Block\Onepage\Review>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
285
                // determine current selected code & name
286
                foreach ($groups as $code => $rates) {
287
                    foreach ($rates as $rate) {
288
                        if ($this->_address->getShippingMethod() == $rate->getCode()) {
289
                            $this->_currentShippingRate = $rate;
290
                            break 2;
291
                        }
292
                    }
293
                }
294
            }
295
296
            // misc shipping parameters
297
            $this->setShippingMethodSubmitUrl(
0 ignored issues
show
Documentation Bug introduced by
The method setShippingMethodSubmitUrl does not exist on object<Payone\Core\Block\Onepage\Review>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
298
                $this->getUrl("{$this->_controllerPath}/review", ['_secure' => true])
299
            );
300
        }
301
302
        $this->setEditUrl(
0 ignored issues
show
Documentation Bug introduced by
The method setEditUrl does not exist on object<Payone\Core\Block\Onepage\Review>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
303
            $this->getUrl("{$this->_controllerPath}/edit")
304
        )->setPlaceOrderUrl(
305
            $this->getUrl("{$this->_controllerPath}/placeOrder", ['_secure' => true])
306
        );
307
308
        return parent::_beforeToHtml();
309
    }
310
}
311