Passed
Pull Request — master (#31)
by Nic
02:03
created

OrderDetailFactory::setOrderDetails()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
nc 3
nop 0
dl 0
loc 21
rs 9.9332
c 1
b 0
f 0
1
<?php
2
3
namespace Dynamic\Foxy\Orders\Factory;
4
5
use Dynamic\Foxy\Orders\Foxy\Transaction;
6
use Dynamic\Foxy\Orders\Model\OrderDetail;
7
use SilverStripe\Core\Config\Configurable;
8
use SilverStripe\Core\Injector\Injectable;
9
use SilverStripe\ORM\ArrayList;
10
11
/**
12
 * Class OrderDetailFactory
13
 * @package Dynamic\Foxy\Orders\Factory
14
 */
15
class OrderDetailFactory
16
{
17
    use Configurable;
18
    use Injectable;
19
20
    /**
21
     * @var Transaction
22
     */
23
    private $transaction;
24
25
    /**
26
     * @var ArrayList
27
     */
28
    private $order_details;
29
30
    /**
31
     * OrderDetailFactory constructor.
32
     * @param Transaction|null $transaction
33
     */
34
    public function __construct(Transaction $transaction = null)
35
    {
36
        if ($transaction instanceof Transaction && $transaction !== null) {
37
            $this->setTransaction($transaction);
38
        }
39
    }
40
41
    /**
42
     * @param Transaction $transaction
43
     * @return $this
44
     */
45
    public function setTransaction(Transaction $transaction)
46
    {
47
        $this->transaction = $transaction;
48
49
        return $this;
50
    }
51
52
    /**
53
     * @return Transaction
54
     */
55
    protected function getTransaction()
56
    {
57
        return $this->transaction;
58
    }
59
60
    /**
61
     * @return $this
62
     * @throws \SilverStripe\ORM\ValidationException
63
     */
64
    protected function setOrderDetails()
65
    {
66
        $details = ArrayList::create();
67
68
        $products = $this->getTransaction()->getParsedTransactionData()->products;
69
70
        foreach ($products as $detail) {
71
            $orderDetail = OrderDetail::create();
72
73
            foreach ($this->config()->get('order_detail_mapping') as $foxy => $ssFoxy) {
74
                $orderDetail->{$ssFoxy} = $detail->{$foxy};
75
            }
76
77
            $orderDetail->write();
78
79
            $details->push($orderDetail);
80
        }
81
82
        $this->order_details = $details;
83
84
        return $this;
85
    }
86
87
    /**
88
     * @return ArrayList
89
     * @throws \SilverStripe\ORM\ValidationException
90
     */
91
    public function getOrderDetails()
92
    {
93
        if (!$this->order_details) {
94
            $this->setOrderDetails();
95
        }
96
97
        return $this->order_details;
98
    }
99
}
100