ShippingAddress::_setBillingAndShipping()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace Magefix\Fixture\Builder\Helper;
4
5
6
use Mage;
7
use Mage_Sales_Model_Quote;
8
use Magefix\Exceptions\UnknownQuoteAddressType;
9
use Magefix\Fixture\Builder\AbstractBuilder;
10
use Magefix\Fixture\Builder\Helper;
11
12
class ShippingAddress implements Helper
13
{
14
    /**
15
     * @var Mage_Sales_Model_Quote
16
     */
17
    private $_quote;
18
    /**
19
     * @var array
20
     */
21
    private $_data;
22
    /**
23
     * @var AbstractBuilder
24
     */
25
    private $_builder;
26
27
    public function __construct(AbstractBuilder $_builder, Mage_Sales_Model_Quote $_quote, array $_data)
28
    {
29
        $this->_quote = $_quote;
30
        $this->_data = $_data;
31
        $this->_builder = $_builder;
32
    }
33
34
    public function addToQuote()
35
    {
36
        $iterator = new \ArrayIterator($this->_data['fixture']['addresses']);
37
38
        while ($iterator->valid()) {
39
            if ($iterator->key() == 'billing_and_shipping') {
40
                $this->_setBillingAndShipping();
41
            } else {
42
                $this->_setBillingOrShipping($iterator->key());
43
            }
44
            $iterator->next();
45
        }
46
    }
47
48
    protected function _setBillingAndShipping()
49
    {
50
        list($address, $billing, $shipping) = $this->_getBillingAndShippingAddressesByType('billing_and_shipping');
51
52
        $billing->setData($address);
53
        $shipping->setData($address);
54
        $this->_quote->setBillingAddress($billing);
55
        $this->_quote->setShippingAddress($shipping);
56
    }
57
58
    /**
59
     * @param string $addressType
60
     */
61
    protected function _setBillingOrShipping($addressType)
62
    {
63
        list($address, $billing, $shipping) = $this->_getBillingAndShippingAddressesByType($addressType);
64
65
        if ($addressType == 'billing') {
66
            $billing->setData($address);
67
            $this->_quote->setBillingAddress($billing);
68
        } elseif ($addressType == 'shipping') {
69
            $shipping->setData($address);
70
            $this->_quote->setShippingAddress($shipping);
71
        }
72
    }
73
74
    /**
75
     * @param string $addressType
76
     * @throws UnknownQuoteAddressType
77
     */
78
    protected function _throwUnknownQuoteAddressTypeException($addressType)
79
    {
80
        if ($addressType !== 'billing_and_shipping' && $addressType !== 'billing' && $addressType !== 'shipping') {
81
            throw new UnknownQuoteAddressType(
82
                'Sales Order Fixture: Unknown quote address type. Check fixture yml.'
83
            );
84
        }
85
    }
86
87
    /**
88
     * @param $addressType
89
     * @return array
90
     */
91
    protected function _getBillingAndShippingAddressesByType($addressType)
92
    {
93
        $address = $this->_data['fixture']['addresses'][$addressType];
94
        $billing = Mage::getModel('sales/quote_address');
95
        $shipping = Mage::getModel('sales/quote_address');
96
        return array($address, $billing, $shipping);
97
    }
98
}
99