Passed
Pull Request — master (#57)
by Jason
02:01
created

OrderFactory::setOrder()   C

Complexity

Conditions 16
Paths 48

Size

Total Lines 57
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 16
eloc 29
c 3
b 0
f 0
nc 48
nop 0
dl 0
loc 57
rs 5.5666

How to fix   Long Method    Complexity   

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
namespace Dynamic\Foxy\Orders\Factory;
4
5
use Dynamic\Foxy\Model\FoxyHelper;
6
use Dynamic\Foxy\Model\Variation;
7
use Dynamic\Foxy\Orders\Model\Order;
8
use SilverStripe\Core\Config\Configurable;
9
use SilverStripe\Core\Extensible;
10
use SilverStripe\Core\Injector\Injectable;
11
use SilverStripe\Security\Member;
12
use SilverStripe\View\ArrayData;
13
14
/**
15
 * Class OrderFactory
16
 * @package Dynamic\Foxy\Orders\Factory
17
 */
18
class OrderFactory extends FoxyFactory
19
{
20
    use Configurable;
21
    use Extensible;
22
    use Injectable;
23
24
    /**
25
     * @var Order
26
     */
27
    private $order;
28
29
    /**
30
     * Return the Order object from a given transaction data set.
31
     *
32
     * @return Order
33
     * @throws \SilverStripe\ORM\ValidationException
34
     */
35
    public function getOrder()
36
    {
37
        if (!$this->order instanceof Order) {
0 ignored issues
show
introduced by
$this->order is always a sub-type of Dynamic\Foxy\Orders\Model\Order.
Loading history...
38
            $this->setOrder();
39
        }
40
41
        return $this->order;
42
    }
43
44
    /**
45
     * Find and update, or create new Order record and set.
46
     *
47
     * @return $this
48
     * @throws \SilverStripe\ORM\ValidationException
49
     */
50
    protected function setOrder()
51
    {
52
        /** @var ArrayData $transaction */
53
        $transaction = $this->getTransaction()->getParsedTransactionData()->getField('transaction');
54
55
        /** @var $order Order */
56
        if ($transaction->hasField('id')
57
            && !($order = Order::get()->filter('OrderID', $transaction->getField('id'))->first())) {
58
            $order = Order::create();
59
        }
60
61
        if ($order->exists()) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $order does not seem to be defined for all execution paths leading up to this point.
Loading history...
62
            $this->cleanRelatedOrderData($order);
63
        }
64
65
        foreach ($this->config()->get('order_mapping') as $foxy => $ssFoxy) {
66
            if ($transaction->hasField($foxy)) {
67
                $order->{$ssFoxy} = $transaction->getField($foxy);
68
            }
69
        }
70
71
        if ($member = Member::get()->filter('Email', $order->Email)->first()) {
72
            $order->MemberID = $member->ID;
73
        }
74
75
        $order->Response = urlencode($this->getTransaction()->getEncryptedData());
76
77
        $order->write();
78
79
        $order->Details()->addMany(OrderDetailFactory::create($this->getTransaction())->getOrderDetails());
80
81
        $this->order = Order::get()->byID($order->ID);
82
83
        if ($order->Details()->count()) {
84
            foreach ($order->Details() as $detail) {
85
                if ($product = FoxyHelper::singleton()->getProducts()->filter('ID', $detail->ProductID)->first()) {
86
                    if ($product->hasMethod('getNumberPurchasedUpdate')) {
87
                        $product->NumberPurchased = $product->getNumberPurchasedUpdate();
88
                        $product->write();
89
                    }
90
                }
91
                if ($detail->OrderVariations()->count()) {
92
                    foreach ($detail->OrderVariations() as $orderVariation) {
93
                        if ($orderVariation->VariationID) {
94
                            if ($variaton = Variation::get()->filter('ID', $orderVariation->VariationID)->first()) {
95
                                if ($variaton->hasMethod('getNumberPurchasedUpdate')) {
96
                                    $variaton->NumberPurchased = $variaton->getNumberPurchasedUpdate();
97
                                    $variaton->write();
98
                                }
99
                            }
100
                        }
101
                    }
102
                }
103
            }
104
        }
105
106
        return $this;
107
    }
108
109
    /**
110
     * @param Order $order
111
     */
112
    private function cleanRelatedOrderData(Order $order)
113
    {
114
        $order->Details()->removeAll();
115
    }
116
}
117