1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Foxy\SingleSignOn\Extension; |
4
|
|
|
|
5
|
|
|
use Dynamic\Foxy\API\Client\APIClient; |
6
|
|
|
use Dynamic\Foxy\SingleSignOn\Client\CustomerClient; |
7
|
|
|
use SilverStripe\Forms\FieldList; |
8
|
|
|
use SilverStripe\Forms\FormAction; |
9
|
|
|
use SilverStripe\ORM\DataExtension; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class CustomerExtension |
13
|
|
|
* @package Dynamic\Foxy\SingleSignOn\Extension |
14
|
|
|
*/ |
15
|
|
|
class CustomerExtension extends DataExtension |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
private static $db = [ |
|
|
|
|
21
|
|
|
'Customer_ID' => 'Int', |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
/*private static $indexes = [ |
28
|
|
|
'foxy-single-sign-on' => [ |
29
|
|
|
'type' => 'unique', |
30
|
|
|
'columns' => ['Customer_ID'], |
31
|
|
|
], |
32
|
|
|
];*/ |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param FieldList $actions |
36
|
|
|
*/ |
37
|
|
|
public function updateCMSActions(FieldList $actions) |
38
|
|
|
{ |
39
|
|
|
$actions->push(FormAction::create('syncFromFoxy')->setTitle('Sync Customer From Foxy')); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
44
|
|
|
*/ |
45
|
|
|
public function onBeforeWrite() |
46
|
|
|
{ |
47
|
|
|
parent::onBeforeWrite(); |
48
|
|
|
|
49
|
|
|
if ($this->isValidAPI()) { |
50
|
|
|
$client = CustomerClient::create($this->owner); |
51
|
|
|
$data = $client->putCustomer(); |
52
|
|
|
|
53
|
|
|
if (isset($data) && !$this->owner->Customer_ID) { |
54
|
|
|
if (isset($data['_links'])) { |
55
|
|
|
$parts = explode('/', $data['_links']['self']['href']); |
56
|
|
|
|
57
|
|
|
$customerID = $parts[count($parts) - 1]; |
58
|
|
|
|
59
|
|
|
$this->owner->Customer_ID = $customerID; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return bool |
67
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
68
|
|
|
*/ |
69
|
|
|
protected function isValidAPI() |
70
|
|
|
{ |
71
|
|
|
return APIClient::config()->get('enable_api') |
72
|
|
|
&& CustomerClient::config()->get('foxy_sso_enabled') |
73
|
|
|
&& APIClient::is_valid(); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|