Completed
Branch feature/MGFX-01-register-check... (7126c8)
by Carlo
03:37
created

ShippingMethod::addShippingDataToQuote()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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