Passed
Push — master ( 2588e6...b542a3 )
by Jason
02:04
created

Order::getDecryptedResponse()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Dynamic\Foxy\Model;
4
5
use Dynamic\FoxyStripe\Foxy\Transaction;
6
use SilverStripe\Forms\DateField;
7
use SilverStripe\ORM\DataObject;
8
use SilverStripe\ORM\FieldType\DBHTMLVarchar;
9
use SilverStripe\ORM\ValidationException;
10
use SilverStripe\Security\Member;
11
12
/**
13
 * Class Order
14
 * @package Dynamic\Foxy\Model
15
 *
16
 * @property \SilverStripe\ORM\FieldType\DBInt StoreID
17
 * @property \SilverStripe\ORM\FieldType\DBInt OrderID
18
 * @property \SilverStripe\ORM\FieldType\DBVarchar Email
19
 * @property \SilverStripe\ORM\FieldType\DBDatetime TransactionDate
20
 * @property \SilverStripe\ORM\FieldType\DBCurrency ProductTotal
21
 * @property \SilverStripe\ORM\FieldType\DBCurrency TaxTotal
22
 * @property \SilverStripe\ORM\FieldType\DBCurrency ShippingTotal
23
 * @property \SilverStripe\ORM\FieldType\DBCurrency OrderTotal
24
 * @property \SilverStripe\ORM\FieldType\DBVarchar ReceiptURL
25
 * @property \SilverStripe\ORM\FieldType\DBVarchar OrderStatus
26
 * @property \SilverStripe\ORM\FieldType\DBText Response
27
 *
28
 * @property int MemberID
29
 * @method Member Member
30
 */
31
class Order extends DataObject
32
{
33
    /**
34
     * @var array
35
     */
36
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
37
        'StoreID' => 'Int',
38
        'OrderID' => 'Int',
39
        'Email' => 'Varchar(255)',
40
        'TransactionDate' => 'DBDatetime',
41
        'ProductTotal' => 'Currency',
42
        'TaxTotal' => 'Currency',
43
        'ShippingTotal' => 'Currency',
44
        'OrderTotal' => 'Currency',
45
        'ReceiptURL' => 'Varchar(255)',
46
        'OrderStatus' => 'Varchar(255)',
47
        'Response' => 'Text',
48
    ];
49
50
    /**
51
     * @var array
52
     */
53
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
54
        'Member' => Member::class,
55
    ];
56
57
    /**
58
     * @var string
59
     */
60
    private static $singular_name = 'Order';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
61
62
    /**
63
     * @var string
64
     */
65
    private static $plural_name = 'Orders';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
66
67
    /**
68
     * @var string
69
     */
70
    private static $description = 'Orders from FoxyCart Datafeed';
0 ignored issues
show
introduced by
The private property $description is not used, and could be removed.
Loading history...
71
72
    /**
73
     * @var string
74
     */
75
    private static $default_sort = 'TransactionDate DESC, ID DESC';
0 ignored issues
show
introduced by
The private property $default_sort is not used, and could be removed.
Loading history...
76
77
    /**
78
     * @var array
79
     */
80
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
81
        'OrderID',
82
        'TransactionDate.Nice',
83
        'Email',
84
        'ProductTotal.Nice',
85
        'ShippingTotal.Nice',
86
        'TaxTotal.Nice',
87
        'OrderTotal.Nice',
88
        'ReceiptLink',
89
    ];
90
91
    /**
92
     * @var array
93
     */
94
    private static $searchable_fields = [
0 ignored issues
show
introduced by
The private property $searchable_fields is not used, and could be removed.
Loading history...
95
        'OrderID',
96
        'TransactionDate' => [
97
            'field' => DateField::class,
98
            'filter' => 'PartialMatchFilter',
99
        ],
100
        'OrderTotal'
101
    ];
102
103
    /**
104
     * @var array
105
     */
106
    private static $casting = [
0 ignored issues
show
introduced by
The private property $casting is not used, and could be removed.
Loading history...
107
        'ReceiptLink' => 'HTMLVarchar',
108
    ];
109
110
    /**
111
     * @var array
112
     */
113
    private static $indexes = [
0 ignored issues
show
introduced by
The private property $indexes is not used, and could be removed.
Loading history...
114
        'OrderID' => true, // make unique
115
    ];
116
117
    /**
118
     * @var string
119
     */
120
    private static $table_name = 'FoxyOrder';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
121
122
    /**
123
     * @var
124
     */
125
    private $transaction;
126
127
    /**
128
     * @return mixed
129
     */
130
    protected function getTransaction()
131
    {
132
        if (!$this->transaction) {
133
            $this->setTransaction();
134
        }
135
        return $this->transaction;
136
    }
137
138
    /**
139
     * @return $this
140
     */
141
    protected function setTransaction()
142
    {
143
        if ($this->Response) {
144
            $this->transaction = Transaction::create($this->OrderID, urldecode($this->Response));
145
        } else {
146
            $this->transaction = false;
147
        }
148
        return $this;
149
    }
150
151
    /**
152
     * @param bool $includerelations
153
     *
154
     * @return array|string
155
     */
156
    public function fieldLabels($includerelations = true)
157
    {
158
        $labels = parent::fieldLabels();
159
        $labels['StoreID'] = _t(__CLASS__ . '.StoreID', 'Store ID#');
160
        $labels['OrderID'] = _t(__CLASS__ . '.OrderID', 'Order ID#');
161
        $labels['TransactionDate'] = _t(__CLASS__ . '.TransactionDate', 'Date');
162
        $labels['TransactionDate.NiceUS'] = _t(__CLASS__ . '.TransactionDate', 'Date');
163
        $labels['Email'] = _t(__CLASS__ . '.Email', 'Email');
164
        $labels['ProductTotal.Nice'] = _t(__CLASS__ . '.ProductTotal', 'Sub Total');
165
        $labels['TaxTotal.Nice'] = _t(__CLASS__ . '.TaxTotal', 'Tax');
166
        $labels['ShippingTotal.Nice'] = _t(__CLASS__ . '.ShippingTotal', 'Shipping');
167
        $labels['OrderTotal'] = _t(__CLASS__ . '.OrderTotal', 'Total');
168
        $labels['OrderTotal.Nice'] = _t(__CLASS__ . '.OrderTotal', 'Total');
169
        $labels['ReceiptLink'] = _t(__CLASS__ . '.ReceiptLink', 'Invoice');
170
        $labels['Details.ProductID'] = _t(__CLASS__ . '.Details.ProductID', 'Product');
171
        return $labels;
172
    }
173
174
    /**
175
     * @return mixed
176
     */
177
    public function ReceiptLink()
178
    {
179
        return $this->getReceiptLink();
180
    }
181
182
    /**
183
     * @return mixed
184
     */
185
    public function getReceiptLink()
186
    {
187
        $obj = DBHTMLVarchar::create();
188
        $obj->setValue(
189
            '<a href="' . $this->ReceiptURL . '" target="_blank" class="cms-panel-link action external-link">view</a>'
190
        );
191
        return $obj;
192
    }
193
194
    /**
195
     * @throws \SilverStripe\ORM\ValidationException
196
     */
197
    protected function onBeforeWrite()
198
    {
199
        parent::onBeforeWrite();
200
        $this->parseOrder();
201
    }
202
203
    /**
204
     * @return bool
205
     *
206
     * @throws ValidationException
207
     */
208
    public function parseOrder()
209
    {
210
        if ($this->getTransaction() && $this->getTransaction()->exists()) {
211
            $this->parseOrderInfo();
212
        }
213
214
        $this->extend('updateParseOrder', $this);
215
    }
216
217
    /**
218
     * @param $response
219
     */
220
    public function parseOrderInfo()
221
    {
222
        $transaction = $this->getTransaction()->getTransaction();
223
224
        // Record transaction data from FoxyCart Datafeed:
225
        $this->StoreID = (int)$transaction->store_id;
0 ignored issues
show
Documentation Bug introduced by
It seems like (int)$transaction->store_id of type integer is incompatible with the declared type SilverStripe\ORM\FieldType\DBInt of property $StoreID.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
226
        $this->Email = (string)$transaction->customer_email;
0 ignored issues
show
Documentation Bug introduced by
It seems like (string)$transaction->customer_email of type string is incompatible with the declared type SilverStripe\ORM\FieldType\DBVarchar of property $Email.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
227
        $this->TransactionDate = (string)$transaction->transaction_date;
0 ignored issues
show
Documentation Bug introduced by
It seems like (string)$transaction->transaction_date of type string is incompatible with the declared type SilverStripe\ORM\FieldType\DBDatetime of property $TransactionDate.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
228
        $this->ProductTotal = (float)$transaction->product_total;
0 ignored issues
show
Documentation Bug introduced by
It seems like (double)$transaction->product_total of type double is incompatible with the declared type SilverStripe\ORM\FieldType\DBCurrency of property $ProductTotal.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
229
        $this->TaxTotal = (float)$transaction->tax_total;
0 ignored issues
show
Documentation Bug introduced by
It seems like (double)$transaction->tax_total of type double is incompatible with the declared type SilverStripe\ORM\FieldType\DBCurrency of property $TaxTotal.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
230
        $this->ShippingTotal = (float)$transaction->shipping_total;
0 ignored issues
show
Documentation Bug introduced by
It seems like (double)$transaction->shipping_total of type double is incompatible with the declared type SilverStripe\ORM\FieldType\DBCurrency of property $ShippingTotal.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
231
        $this->OrderTotal = (float)$transaction->order_total;
0 ignored issues
show
Documentation Bug introduced by
It seems like (double)$transaction->order_total of type double is incompatible with the declared type SilverStripe\ORM\FieldType\DBCurrency of property $OrderTotal.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
232
        $this->ReceiptURL = (string)$transaction->receipt_url;
0 ignored issues
show
Documentation Bug introduced by
It seems like (string)$transaction->receipt_url of type string is incompatible with the declared type SilverStripe\ORM\FieldType\DBVarchar of property $ReceiptURL.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
233
        $this->OrderStatus = (string)$transaction->status;
0 ignored issues
show
Documentation Bug introduced by
It seems like (string)$transaction->status of type string is incompatible with the declared type SilverStripe\ORM\FieldType\DBVarchar of property $OrderStatus.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
234
235
        $this->extend('handleOrderInfo', $order, $response);
236
    }
237
}
238