Passed
Pull Request — master (#31)
by Nic
01:54
created

OrderFactory::getOrder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Dynamic\Foxy\Orders\Factory;
4
5
use Dynamic\Foxy\Orders\Foxy\Transaction;
6
use Dynamic\Foxy\Orders\Model\Order;
7
use SilverStripe\Core\Config\Configurable;
8
use SilverStripe\Core\Extensible;
9
use SilverStripe\Core\Injector\Injectable;
10
11
/**
12
 * Class OrderFactory
13
 * @package Dynamic\Foxy\Orders\Factory
14
 */
15
class OrderFactory
16
{
17
    use Configurable;
18
    use Extensible;
19
    use Injectable;
20
21
    /**
22
     * @var string The encrypted Foxy.io xml data feed response.
23
     */
24
    private $encrypted_order_data;
25
26
    /**
27
     * @var Order
28
     */
29
    private $order;
30
31
    /**
32
     * OrderFactory constructor.
33
     * @param null $encryptedOrderData
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $encryptedOrderData is correct as it would always require null to be passed?
Loading history...
34
     */
35
    public function __construct($encryptedOrderData = null)
36
    {
37
        if ($encryptedOrderData !== null) {
0 ignored issues
show
introduced by
The condition $encryptedOrderData !== null is always false.
Loading history...
38
            $this->setEncryptedOrderData($encryptedOrderData);
39
        }
40
    }
41
42
    /**
43
     * Set the encrypted Foxy.io xml data feed response.
44
     *
45
     * @param $encryptedData
46
     * @return $this
47
     */
48
    public function setEncryptedOrderData($encryptedData)
49
    {
50
        $this->encrypted_order_data = $encryptedData;
51
52
        return $this;
53
    }
54
55
    /**
56
     * Return the encrypted Foxy.io xml data feed response.
57
     *
58
     * @return string
59
     */
60
    protected function getEncryptedOrderData()
61
    {
62
        return $this->encrypted_order_data;
63
    }
64
65
    /**
66
     * Return the Order object from a given transaction data set.
67
     *
68
     * @return Order
69
     * @throws \SilverStripe\ORM\ValidationException
70
     */
71
    public function getOrder()
72
    {
73
        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...
74
            $this->setOrder();
75
        }
76
77
        return $this->order;
78
    }
79
80
    /**
81
     * Find and update, or create new Order record and set.
82
     *
83
     * @return $this
84
     * @throws \SilverStripe\ORM\ValidationException
85
     */
86
    protected function setOrder()
87
    {
88
        $transaction = Transaction::create($this->getEncryptedOrderData())->getParsedTransactionData();
89
90
        $order = (Order::get()->filter('OrderID', $transaction->transaction->id)->first())
91
            ?: Order::create();
92
93
        foreach ($this->config()->get('order_mapping') as $foxy => $ssFoxy) {
94
            $order->{$ssFoxy} = $transaction->transaction->{$foxy};
95
        }
96
97
        $order->write();
98
99
        $this->order = $order;
100
101
        return $this;
102
    }
103
}
104