Passed
Pull Request — master (#309)
by Jason
13:47
created

CustomerExtension   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
dl 0
loc 41
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A onBeforeWrite() 0 15 2
1
<?php
2
3
namespace Dynamic\FoxyStripe\ORM;
4
5
use Dynamic\FoxyStripe\Model\FoxyCart;
6
use Dynamic\FoxyStripe\Model\Order;
7
use SilverStripe\ORM\DataExtension;
8
9
class CustomerExtension extends DataExtension
10
{
11
    /**
12
     * @var array 
13
     */
14
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
15
        'Customer_ID' => 'Int'
16
    );
17
18
    /**
19
     * @var array
20
     */
21
    private static $has_many = array(
0 ignored issues
show
introduced by
The private property $has_many is not used, and could be removed.
Loading history...
22
        'Orders' => Order::class,
23
    );
24
25
    /**
26
     * @var array
27
     */
28
    private static $indexes = array(
0 ignored issues
show
introduced by
The private property $indexes is not used, and could be removed.
Loading history...
29
        'Customer_ID' => true // make unique
30
    );
31
32
    /**
33
     * @throws \Psr\Container\NotFoundExceptionInterface
34
     */
35
    public function onBeforeWrite()
36
    {
37
        parent::onBeforeWrite();
38
39
        // if Member data was imported from FoxyCart, PasswordEncryption will be set to 'none'.
40
        // Change to sh1_v2.4 to ensure SilverStripe is using the same hash as FoxyCart API 1.1
41
        $this->owner->PasswordEncryption = 'sha1_v2.4';
42
43
        // Send updated customer data to Foxy Cart via API
44
        $response = FoxyCart::putCustomer($this->owner);
45
46
        // Grab customer_id record from FoxyCart response, store in Member
47
        if ($response) {
48
            $foxyResponse = new \SimpleXMLElement($response);
49
            $this->owner->Customer_ID = (int) $foxyResponse->customer_id;
50
        }
51
    }
52
}
53