Completed
Push — develop ( 1f8c39...fd8d97 )
by Carlo
03:09
created

ShippingAddress::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4286
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
3
namespace Magefix\Fixture\Builder\Helper;
4
5
6
use Mage_Sales_Model_Quote;
7
use Magefix\Exceptions\UnknownQuoteAddressType;
8
use Magefix\Fixture\Builder\AbstractBuilder;
9
10
class ShippingAddress
11
{
12
    /**
13
     * @var Mage_Sales_Model_Quote
14
     */
15
    private $_quote;
16
    /**
17
     * @var array
18
     */
19
    private $_data;
20
    /**
21
     * @var AbstractBuilder
22
     */
23
    private $_builder;
24
25
    public function __construct(AbstractBuilder $_builder, Mage_Sales_Model_Quote $_quote, array $_data)
26
    {
27
        $this->_quote   = $_quote;
28
        $this->_data    = $_data;
29
        $this->_builder = $_builder;
30
    }
31
32
    public function addToQuote()
33
    {
34
        foreach ($this->_data['fixture']['addresses'] as $addressType => $address) {
35
            switch ($addressType) {
36
                case 'billing_and_shipping':
37
                    $this->_setQuoteAddress($addressType, true);
38
                    break;
39
                case ('billing' || 'shipping'):
40
                    $this->_setQuoteAddress($addressType, false);
41
                    break;
42
                default:
43
                    throw new UnknownQuoteAddressType(
44
                        'Sales Order Fixture: Unknown quote address type. Check fixture yml.'
45
                    );
46
            }
47
        }
48
    }
49
50
    /**
51
     * @param $addressType
52
     *
53
     * @param $sameAsBilling
54
     *
55
     * @return array
56
     */
57
    protected function _setQuoteAddress($addressType, $sameAsBilling)
58
    {
59
        $address = $this->_builder->processFixtureAttributes($this->_data['fixture']['addresses'][$addressType]);
60
61
        if ($sameAsBilling === true) {
62
            $this->_quote->getBillingAddress()->addData($address);
63
            $this->_quote->getShippingAddress()->addData($address);
64
        }
65
66
        if ($addressType == 'billing' && $sameAsBilling === false) {
67
            $this->_quote->getBillingAddress()->addData($address);
68
        } elseif ($addressType == 'shipping' && $sameAsBilling === false) {
69
            $this->_quote->getShippingAddress()->addData($address);
70
        }
71
    }
72
}
73