|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dynamic\Foxy\Orders\Factory; |
|
4
|
|
|
|
|
5
|
|
|
use Dynamic\Foxy\Parser\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 Transaction |
|
23
|
|
|
*/ |
|
24
|
|
|
private $transaction; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var Order |
|
28
|
|
|
*/ |
|
29
|
|
|
private $order; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* OrderFactory constructor. |
|
33
|
|
|
* @param Transaction|null $transaction |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(Transaction $transaction = null) |
|
36
|
|
|
{ |
|
37
|
|
|
if ($transaction !== null && $transaction instanceof Transaction) { |
|
38
|
|
|
$this->setTransaction($transaction); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param Transaction $transaction |
|
44
|
|
|
* @return $this |
|
45
|
|
|
*/ |
|
46
|
|
|
public function setTransaction(Transaction $transaction) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->transaction = $transaction; |
|
49
|
|
|
|
|
50
|
|
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return Transaction |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function getTransaction() |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->transaction; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Return the Order object from a given transaction data set. |
|
63
|
|
|
* |
|
64
|
|
|
* @return Order |
|
65
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getOrder() |
|
68
|
|
|
{ |
|
69
|
|
|
if (!$this->order instanceof Order) { |
|
|
|
|
|
|
70
|
|
|
$this->setOrder(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $this->order; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Find and update, or create new Order record and set. |
|
78
|
|
|
* |
|
79
|
|
|
* @return $this |
|
80
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function setOrder() |
|
83
|
|
|
{ |
|
84
|
|
|
$transaction = $this->getTransaction()->getParsedTransactionData(); |
|
85
|
|
|
|
|
86
|
|
|
$order = (Order::get()->filter('OrderID', $transaction->transaction->id)->first()) |
|
87
|
|
|
?: Order::create(); |
|
88
|
|
|
|
|
89
|
|
|
if ($order->exists()) { |
|
90
|
|
|
$this->cleanRelatedOrderData($order); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
foreach ($this->config()->get('order_mapping') as $foxy => $ssFoxy) { |
|
94
|
|
|
$order->{$ssFoxy} = $transaction->transaction->{$foxy}; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$order->write(); |
|
98
|
|
|
|
|
99
|
|
|
$order->Details()->addMany(OrderDetailFactory::create($this->getTransaction())->getOrderDetails()); |
|
100
|
|
|
|
|
101
|
|
|
$this->order = Order::get()->byID($order->ID); |
|
102
|
|
|
|
|
103
|
|
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param Order $order |
|
108
|
|
|
*/ |
|
109
|
|
|
private function cleanRelatedOrderData(Order $order) |
|
110
|
|
|
{ |
|
111
|
|
|
$order->Details()->removeAll(); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|