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

ShippingMethod::addShippingDataToQuote()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 19
rs 9.2
cc 4
eloc 11
nc 8
nop 0
1
<?php
2
3
namespace Magefix\Fixture\Builder\Helper;
4
5
6
use Mage;
7
use Mage_Sales_Model_Quote;
8
9
/**
10
 * Class Shipping
11
 *
12
 * @package Magefix\Fixture\Builder\Helper
13
 * @author  Carlo Tasca <[email protected]>
14
 */
15
class ShippingMethod
16
{
17
    /**
18
     * @var array
19
     */
20
    private $_shippingData = [];
21
    /**
22
     * @var Mage_Sales_Model_Quote
23
     */
24
    private $_quote;
25
26
    public function __construct(Mage_Sales_Model_Quote $quote, array $data)
27
    {
28
        $this->_shippingData = $data;
29
        $this->_quote = $quote;
30
    }
31
32
    public function addShippingDataToQuote()
33
    {
34
        $shippingAddress = $this->_quote->getShippingAddress();
35
        $shippingAddress->setShippingMethod($this->_shippingData['method']);
36
        $shippingAddress->setShippingDescription($this->_shippingData['description']);
37
38
        if ($this->_shippingData['collect_shipping_rates']) {
39
            $shippingAddress->setCollectShippingRates(true);
40
            $shippingAddress->collectShippingRates();
41
        }
42
43
        if ($this->_shippingData['collect_totals']) {
44
            $this->_quote->collectTotals();
45
        }
46
47
        if ($this->_shippingData['free_shipping']) {
48
            $this->_quote->setFreeShipping(true);
49
        }
50
    }
51
52
    /**
53
     * Enable specified carrier. e.g. flatrate
54
     *
55
     * @param string $carrier
56
     */
57
    public static function enable($carrier)
58
    {
59
        self::_enableOrDisable($carrier, 1);
60
    }
61
62
    /**
63
     * Disable specified carrier. e.g. flatrate
64
     *
65
     * @param string $carrier
66
     */
67
    public static function disable($carrier)
68
    {
69
        self::_enableOrDisable($carrier, 0);
70
    }
71
72
    /**
73
     * @param $carrier
74
     *
75
     * @return boolean
76
     *
77
     */
78
    public static function isEnabled($carrier)
79
    {
80
        return Mage::getStoreConfig('carriers/' . $carrier . '/active');
81
    }
82
83
    /**
84
     * @param string $carrier
85
     * @param int    $enable
86
     */
87
    protected static function _enableOrDisable($carrier, $enable)
88
    {
89
        $carriers = Mage::getSingleton('shipping/config')->getActiveCarriers();
90
91
        if (array_key_exists($carrier, $carriers)) {
92
            Mage::app()->getStore()->setConfig('carriers/' . $carrier . '/active', $enable);
93
        }
94
    }
95
}
96