Passed
Pull Request — master (#373)
by Nic
13:50
created

CustomerExtension   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 111
rs 10
c 0
b 0
f 0
ccs 6
cts 9
cp 0.6667
wmc 18

5 Methods

Rating   Name   Duplication   Size   Complexity  
A onBeforeWrite() 0 17 5
A updateCMSFields() 0 3 1
B setDataFromTransaction() 0 23 8
A onAfterWrite() 0 6 2
A resetPasswordEncryption() 0 7 2
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\ORM\DB;
13
use SilverStripe\Security\Member;
14
use SilverStripe\Security\Security;
15
16
/**
17
 * Class CustomerExtension
18
 * @package Dynamic\FoxyStripe\ORM
19
 *
20
 * @property Member $owner
21
 * @property \SilverStripe\ORM\FieldType\DBInt Customer_ID
22
 */
23
class CustomerExtension extends DataExtension
24
{
25
    /**
26
     * @var array
27
     */
28
    private static $db = [
29
        'Customer_ID' => 'Int',
30
    ];
31
32
    /**
33
     * @var array
34
     */
35
    private static $has_many = [
36
        'Orders' => Order::class,
37
    ];
38
39
    /**
40
     * @var array
41
     */
42
    private static $indexes = [
43 49
        'Customer_ID' => true, // make unique
44
    ];
45 49
46
    /**
47
     * @param FieldList $fields
48
     */
49 49
    public function updateCMSFields(FieldList $fields)
50 49
    {
51
        $fields->replaceField('Customer_ID', TextField::create('Customer_ID')->performReadonlyTransformation());
52
    }
53
54
    /**
55
     * If
56 49
     *
57
     * @throws \SilverStripe\ORM\ValidationException
58
     */
59 49
    public function onBeforeWrite()
60
    {
61
        parent::onBeforeWrite();
62
63
        if (!$this->owner->FromDataFeed) {
64
            $client = CustomerClient::create($this->owner);
65
66
            if ($this->owner->isChanged() && FoxyStripeSetting::current_foxystripe_setting()->UseSingleSignOn) {
67
                $data = $client->putCustomer();
68
69
                $parts = explode('/', $data['_links']['self']['href']);
70
71
                $customerID = $parts[count($parts) - 1];
72
73
                $this->owner->Customer_ID = $customerID;
74
            } elseif ($this->owner->isChanged()) {
75
                $response = $client->putCustomer();
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
76
            }
77
        }
78
    }
79
80
    /**
81
     * If the PasswordEncryption for the current membrer is different than the default, update to the default.
82
     */
83
    public function onAfterWrite()
84
    {
85
        parent::onAfterWrite();
86
87
        if ($this->owner->PasswordEncryption != Security::config()->get('password_encryption_algorithm')) {
88
            $this->resetPasswordEncryption();
89
        }
90
    }
91
92
    /**
93
     * Use the config setting for Member mapping the Foxy fields to the SilverStripe fields.
94
     *
95
     * @param $transaction
96
     */
97
    public function setDataFromTransaction($transaction)
98
    {
99
        foreach ($this->owner->config()->get('customer_map') as $type => $map) {
100
            switch ($type) {
101
                case 'int':
102
                    foreach ($map as $foxyField => $foxyStripeField) {
103
                        if ((int)$transaction->{$foxyField}) {
104
                            $this->owner->{$foxyStripeField} = (int)$transaction->{$foxyField};
105
                        }
106
                    }
107
                    break;
108
                case 'string':
109
                    foreach ($map as $foxyField => $foxyStripeField) {
110
                        if ((string)$transaction->{$foxyField}) {
111
                            $this->owner->{$foxyStripeField} = (string)$transaction->{$foxyField};
112
                        }
113
                    }
114
                    break;
115
            }
116
        }
117
        $this->owner->PasswordEncryption = 'none';
118
119
        return $this->owner;
120
    }
121
122
    /**
123
     * Reset the password encryption for the member to the config default.
124
     * Passwords from Foxy are encrypted so the encryption type is set to "none" when processing
125
     * the account from the data feed. Reseting is required for login to work on the website.
126
     */
127
    private function resetPasswordEncryption()
128
    {
129
        $defaultEncryption = Security::config()->get('password_encryption_algorithm');
130
        if ($this->owner->PasswordEncryption != $defaultEncryption) {
131
            DB::prepared_query(
132
                'UPDATE "Member" SET "PasswordEncryption" = ? WHERE ID = ?',
133
                [$defaultEncryption, $this->owner->ID]
134
            );
135
        }
136
    }
137
}
138