|
1
|
|
|
<?php namespace Bedard\Shop\Classes; |
|
2
|
|
|
|
|
3
|
|
|
use Exception; |
|
4
|
|
|
use System\Classes\PluginManager; |
|
5
|
|
|
|
|
6
|
|
|
class DriverManager |
|
7
|
|
|
{ |
|
8
|
|
|
use \October\Rain\Support\Traits\Singleton; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @var array List of plugins. |
|
12
|
|
|
*/ |
|
13
|
|
|
private $plugins; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var System\Classes\PluginManager |
|
17
|
|
|
*/ |
|
18
|
|
|
private $pluginManager; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var array List of payment drivers. |
|
22
|
|
|
*/ |
|
23
|
|
|
private $paymentDrivers; |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array List of shipping drivers. |
|
27
|
|
|
*/ |
|
28
|
|
|
private $shippingDrivers; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Initialize this singleton. |
|
32
|
|
|
*/ |
|
33
|
|
|
protected function init() |
|
34
|
|
|
{ |
|
35
|
|
|
$this->pluginManager = PluginManager::instance(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Return all plugins. |
|
40
|
|
|
* |
|
41
|
|
|
* @return array |
|
42
|
|
|
*/ |
|
43
|
|
|
protected function getPlugins() |
|
44
|
|
|
{ |
|
45
|
|
|
if (! $this->plugins) { |
|
|
|
|
|
|
46
|
|
|
$this->plugins = $this->pluginManager->getPlugins(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $this->plugins; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Return a list of shipping drivers. |
|
54
|
|
|
* |
|
55
|
|
|
* @return array |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getShippingDrivers() |
|
58
|
|
|
{ |
|
59
|
|
|
if (! $this->shippingDrivers) { |
|
|
|
|
|
|
60
|
|
|
$this->registerShippingDrivers(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $this->shippingDrivers; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Register all shipping drivers. |
|
68
|
|
|
* |
|
69
|
|
|
* @return void |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function registerShippingDrivers() |
|
72
|
|
|
{ |
|
73
|
|
|
$plugins = $this->getPlugins(); |
|
74
|
|
|
|
|
75
|
|
|
foreach ($plugins as $id => $plugin) { |
|
76
|
|
|
if (! method_exists($plugin, 'registerShippingDrivers')) { |
|
77
|
|
|
continue; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$pluginDrivers = $plugin->registerShippingDrivers(); |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
foreach ($plugin->registerShippingDrivers() as $driverClass) { |
|
83
|
|
|
$driver = new $driverClass; |
|
84
|
|
|
$details = $driver->driverDetails(); |
|
85
|
|
|
$this->validateDriverDetails($details, $driverClass); |
|
86
|
|
|
|
|
87
|
|
|
$drivers[] = $driver; |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$this->shippingDrivers = $drivers; |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Validate driver details. |
|
96
|
|
|
* |
|
97
|
|
|
* @param array $details |
|
98
|
|
|
* @param string $driverClass |
|
99
|
|
|
* @throws \Exception |
|
100
|
|
|
* @return void |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function validateDriverDetails($details, $driverClass) |
|
103
|
|
|
{ |
|
104
|
|
|
if (! is_array($details)) { |
|
105
|
|
|
throw new Exception('An array must be returned from the driverDetails() method in '. $driverClass . '.'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
if (! array_key_exists('name', $details) || ! is_string($details['name'])) { |
|
109
|
|
|
throw new Exception('A valid name must be returned from the driverDetails() method in ' . $driverClass . '.'); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.