Passed
Pull Request — master (#373)
by Nic
05:57
created

CustomerExtension::onBeforeWrite()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5.024

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 3
nop 0
dl 0
loc 16
ccs 6
cts 10
cp 0.6
crap 5.024
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace Dynamic\FoxyStripe\ORM;
4
5
use Dynamic\FoxyStripe\API\Client\CustomerClient;
6
use Dynamic\FoxyStripe\Model\FoxyCart;
7
use Dynamic\FoxyStripe\Model\FoxyStripeSetting;
8
use Dynamic\FoxyStripe\Model\Order;
9
use SilverStripe\Forms\FieldList;
10
use SilverStripe\Forms\TextField;
11
use SilverStripe\ORM\DataExtension;
12
use SilverStripe\Security\Member;
13
14
/**
15
 * Class CustomerExtension
16
 * @package Dynamic\FoxyStripe\ORM
17
 *
18
 * @property Member $owner
19
 * @property \SilverStripe\ORM\FieldType\DBInt Customer_ID
20
 */
21
class CustomerExtension extends DataExtension
22
{
23
    /**
24
     * @var array
25
     */
26
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
27
        'Customer_ID' => 'Int',
28
    ];
29
30
    /**
31
     * @var array
32
     */
33
    private static $has_many = [
0 ignored issues
show
introduced by
The private property $has_many is not used, and could be removed.
Loading history...
34
        'Orders' => Order::class,
35
    ];
36
37
    /**
38
     * @var array
39
     */
40
    private static $indexes = [
0 ignored issues
show
introduced by
The private property $indexes is not used, and could be removed.
Loading history...
41
        'Customer_ID' => true, // make unique
42
    ];
43
44
    /**
45
     * @param FieldList $fields
46
     */
47
    public function updateCMSFields(FieldList $fields)
48
    {
49
        $fields->replaceField('Customer_ID', TextField::create('Customer_ID')->performReadonlyTransformation());
50
    }
51
52
    /**
53
     * @throws \SilverStripe\ORM\ValidationException
54
     */
55 49
    public function onBeforeWrite()
56
    {
57 49
        parent::onBeforeWrite();
58
59 49
        $client = CustomerClient::create($this->owner);
60
61 49
        if (!$this->owner->Customer_ID && FoxyStripeSetting::current_foxystripe_setting()->UseSingleSignOn) {
62
            $data = $client->putCustomer();
63
64
            $parts = explode('/', $data['_links']['self']['href']);
65
66
            $customerID = $parts[count($parts) - 1];
67
68
            $this->owner->Customer_ID = $customerID;
69 49
        } elseif ($this->owner->isChanged()) {
70 49
            $response = $client->putCustomer();
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
71
        }
72
    }
73
}
74