Passed
Push — master ( 03c0bd...d9a7d5 )
by
unknown
05:08
created

Cart::parseResults()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 54
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 39
c 0
b 0
f 0
dl 0
loc 54
rs 8.9848
cc 5
nc 3
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*************************************************************************************/
3
/*      This file is part of the Thelia package.                                     */
4
/*                                                                                   */
5
/*      Copyright (c) OpenStudio                                                     */
6
/*      email : [email protected]                                                       */
7
/*      web : http://www.thelia.net                                                  */
8
/*                                                                                   */
9
/*      For the full copyright and license information, please view the LICENSE.txt  */
10
/*      file that was distributed with this source code.                             */
11
/*************************************************************************************/
12
13
namespace Thelia\Core\Template\Loop;
14
15
use Thelia\Core\Template\Element\ArraySearchLoopInterface;
16
use Thelia\Core\Template\Element\BaseLoop;
17
use Thelia\Core\Template\Element\LoopResult;
18
use Thelia\Core\Template\Element\LoopResultRow;
19
use Thelia\Core\Template\Loop\Argument\Argument;
20
use Thelia\Core\Template\Loop\Argument\ArgumentCollection;
21
use Thelia\Model\CartItem as CartItemModel;
22
use Thelia\Model\ConfigQuery;
23
use Thelia\Model\Cart as CartModel;
24
use Thelia\Type;
25
26
/**
27
 *
28
 * Cart Loop
29
 *
30
 *
31
 * Class Cart
32
 * @package Thelia\Core\Template\Loop
33
 *
34
 * {@inheritdoc}
35
 * @method string[] getOrder()
36
 */
37
class Cart extends BaseLoop implements ArraySearchLoopInterface
38
{
39
    /**
40
     *
41
     * @return \Thelia\Core\Template\Loop\Argument\ArgumentCollection
42
     */
43
    protected function getArgDefinitions()
44
    {
45
        return new ArgumentCollection(
46
            new Argument(
47
                'order',
48
                new Type\TypeCollection(
49
                    new Type\EnumListType(array('normal', 'reverse'))
50
                ),
51
                'normal'
52
            )
53
        );
54
    }
55
56
    public function buildArray()
57
    {
58
        /** @var CartModel $cart */
59
        $cart = $this->getCurrentRequest()->getSession()->getSessionCart($this->getDispatcher());
60
61
        if (null === $cart) {
62
            return array();
63
        }
64
65
        $returnArray = iterator_to_array($cart->getCartItems());
66
67
        $orders  = $this->getOrder();
68
69
        foreach ($orders as $order) {
70
            switch ($order) {
71
                case "reverse":
72
                    $returnArray = array_reverse($returnArray, false);
73
                    break;
74
            }
75
        }
76
77
        return $returnArray;
78
    }
79
80
    /**
81
     * @param LoopResult $loopResult
82
     * @return LoopResult
83
     * @throws \Propel\Runtime\Exception\PropelException
84
     */
85
    public function parseResults(LoopResult $loopResult)
86
    {
87
        $taxCountry = $this->container->get('thelia.taxEngine')->getDeliveryCountry();
88
        $locale = $this->getCurrentRequest()->getSession()->getLang()->getLocale();
89
        $checkAvailability = ConfigQuery::checkAvailableStock();
90
        $defaultAvailability = \intval(ConfigQuery::read('default-available-stock', 100));
91
92
        /** @var CartItemModel $cartItem */
93
        foreach ($loopResult->getResultDataCollection() as $cartItem) {
94
            $product = $cartItem->getProduct(null, $locale);
95
            $productSaleElement = $cartItem->getProductSaleElements();
96
97
            $loopResultRow = new LoopResultRow($cartItem);
98
99
            $loopResultRow->set("ITEM_ID", $cartItem->getId());
100
            $loopResultRow->set("TITLE", $product->getTitle());
101
            $loopResultRow->set("REF", $product->getRef());
102
            $loopResultRow->set("QUANTITY", $cartItem->getQuantity());
103
            $loopResultRow->set("PRODUCT_ID", $product->getId());
104
            $loopResultRow->set("PRODUCT_URL", $product->getUrl($this->getCurrentRequest()->getSession()->getLang()->getLocale()));
105
            if (!$checkAvailability || $product->getVirtual() === 1) {
106
                $loopResultRow->set("STOCK", $defaultAvailability);
107
            } else {
108
                $loopResultRow->set("STOCK", $productSaleElement->getQuantity());
109
            }
110
            $loopResultRow
111
                ->set("PRICE", $cartItem->getPrice())
112
                ->set("PROMO_PRICE", $cartItem->getPromoPrice())
113
                ->set("TAXED_PRICE", $cartItem->getTaxedPrice($taxCountry))
114
                ->set("PROMO_TAXED_PRICE", $cartItem->getTaxedPromoPrice($taxCountry))
115
                ->set("IS_PROMO", $cartItem->getPromo() === 1 ? 1 : 0)
116
            ;
117
118
            $loopResultRow
119
                ->set("TOTAL_PRICE", $cartItem->getTotalPrice())
120
                ->set("TOTAL_PROMO_PRICE", $cartItem->getTotalPromoPrice())
121
                ->set("TOTAL_TAXED_PRICE", $cartItem->getTotalTaxedPrice($taxCountry))
122
                ->set("TOTAL_PROMO_TAXED_PRICE", $cartItem->getTotalTaxedPromoPrice($taxCountry))
123
            ;
124
125
            $loopResultRow
126
                ->set("REAL_PRICE", $cartItem->getRealPrice())
127
                ->set("REAL_TAXED_PRICE", $cartItem->getRealTaxedPrice($taxCountry))
128
                ->set("REAL_TOTAL_PRICE", $cartItem->getTotalRealPrice($taxCountry))
129
                ->set("REAL_TOTAL_TAXED_PRICE", $cartItem->getTotalRealTaxedPrice($taxCountry))
130
            ;
131
132
            $loopResultRow->set("PRODUCT_SALE_ELEMENTS_ID", $productSaleElement->getId());
133
            $loopResultRow->set("PRODUCT_SALE_ELEMENTS_REF", $productSaleElement->getRef());
134
            $this->addOutputFields($loopResultRow, $cartItem);
135
            $loopResult->addRow($loopResultRow);
136
        }
137
138
        return $loopResult;
139
    }
140
141
    /**
142
     * Return the event dispatcher,
143
     *
144
     * @return \Symfony\Component\EventDispatcher\EventDispatcher
145
     */
146
    public function getDispatcher()
147
    {
148
        return $this->dispatcher;
149
    }
150
}
151