Passed
Push — master ( c6afeb...a80cbd )
by Nic
02:33
created

OrderDetailFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 51
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getOrderDetails() 0 7 2
A setOrderDetails() 0 27 4
1
<?php
2
3
namespace Dynamic\Foxy\Orders\Factory;
4
5
use Dynamic\Foxy\Orders\Model\OrderDetail;
6
use SilverStripe\ORM\ArrayList;
7
use SilverStripe\View\ArrayData;
8
9
/**
10
 * Class OrderDetailFactory
11
 * @package Dynamic\Foxy\Orders\Factory
12
 */
13
class OrderDetailFactory extends FoxyFactory
14
{
15
    /**
16
     * @var ArrayList
17
     */
18
    private $order_details;
19
20
    /**
21
     * @return $this
22
     * @throws \SilverStripe\ORM\ValidationException
23
     */
24
    protected function setOrderDetails()
25
    {
26
        $details = ArrayList::create();
27
28
        /** @var ArrayList $products */
29
        $products = $this->getTransaction()->getParsedTransactionData()->products;
30
31
        /** @var ArrayData $detail */
32
        foreach ($products as $detail) {
33
            $orderDetail = OrderDetail::create();
34
35
            foreach ($this->config()->get('order_detail_mapping') as $foxy => $ssFoxy) {
36
                if ($detail->hasField($foxy)) {
37
                    $orderDetail->{$ssFoxy} = $detail->getField($foxy);
38
                }
39
            }
40
41
            $orderDetail->write();
42
43
            $orderDetail->OrderOptions()->addMany(OrderOptionFactory::create($detail)->getOrderOptions());
44
45
            $details->push($orderDetail);
46
        }
47
48
        $this->order_details = $details;
49
50
        return $this;
51
    }
52
53
    /**
54
     * @return ArrayList
55
     * @throws \SilverStripe\ORM\ValidationException
56
     */
57
    public function getOrderDetails()
58
    {
59
        if (!$this->order_details) {
60
            $this->setOrderDetails();
61
        }
62
63
        return $this->order_details;
64
    }
65
}
66