Completed
Pull Request — master (#373)
by Nic
12:09
created

CustomerExtension::setDataFromTransaction()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
eloc 14
nc 8
nop 1
dl 0
loc 23
rs 8.4444
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 72
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
    /**
82
     * If the PasswordEncryption for the current membrer is different than the default, update to the default.
83
     */
84
    public function onAfterWrite()
85
    {
86
        parent::onAfterWrite();
87
88
        if ($this->owner->PasswordEncryption != Security::config()->get('password_encryption_algorithm')) {
89
            $this->resetPasswordEncryption();
90
        }
91
    }
92
93
    /**
94
     * Use the config setting for Member mapping the Foxy fields to the SilverStripe fields.
95
     *
96
     * @param $transaction
97
     */
98
    public function setDataFromTransaction($transaction)
99
    {
100
        foreach ($this->owner->config()->get('customer_map') as $type => $map) {
101
            switch ($type) {
102
                case 'int':
103
                    foreach ($map as $foxyField => $foxyStripeField) {
104
                        if ((int)$transaction->{$foxyField}) {
105
                            $this->owner->{$foxyStripeField} = (int)$transaction->{$foxyField};
106
                        }
107
                    }
108
                    break;
109
                case 'string':
110
                    foreach ($map as $foxyField => $foxyStripeField) {
111
                        if ((string)$transaction->{$foxyField}) {
112
                            $this->owner->{$foxyStripeField} = (string)$transaction->{$foxyField};
113
                        }
114
                    }
115
                    break;
116
            }
117
        }
118
        $this->owner->PasswordEncryption = 'none';
119
120
        return $this->owner;
121
    }
122
123
    /**
124
     * Reset the password encryption for the member to the config default.
125
     * Passwords from Foxy are encrypted so the encryption type is set to "none" when processing
126
     * the account from the data feed. Reseting is required for login to work on the website.
127
     */
128
    private function resetPasswordEncryption()
129
    {
130
        $defaultEncryption = Security::config()->get('password_encryption_algorithm');
131
        if ($this->owner->PasswordEncryption != $defaultEncryption) {
132
            DB::prepared_query(
133
                'UPDATE "Member" SET "PasswordEncryption" = ? WHERE ID = ?',
134
                [$defaultEncryption, $this->owner->ID]
135
            );
136
        }
137
    }
138
}
139