1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DoS\UserBundle\Confirmation; |
4
|
|
|
|
5
|
|
|
use Sylius\Bundle\SettingsBundle\Manager\SettingsManagerInterface; |
6
|
|
|
|
7
|
|
|
class ConfirmationFactory |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var array |
11
|
|
|
*/ |
12
|
|
|
protected $types = array(); |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected $options = array(); |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var null|string |
21
|
|
|
*/ |
22
|
|
|
protected $activedService = null; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var SettingsManagerInterface |
26
|
|
|
*/ |
27
|
|
|
protected $settingsManager; |
28
|
|
|
|
29
|
|
|
public function __construct(SettingsManagerInterface $settingsManager) |
30
|
|
|
{ |
31
|
|
|
$this->settingsManager = $settingsManager; |
32
|
|
|
// TODO: apply options from settings |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Load settings parameter for given namespace and name. |
37
|
|
|
* |
38
|
|
|
* @param string $name |
39
|
|
|
* |
40
|
|
|
* @return mixed |
41
|
|
|
* |
42
|
|
|
* @throws \InvalidArgumentException |
43
|
|
|
*/ |
44
|
|
|
protected function getSettingsParameter($name) |
45
|
|
|
{ |
46
|
|
|
if (false === strpos($name, '.')) { |
47
|
|
|
throw new \InvalidArgumentException( |
48
|
|
|
sprintf('Parameter must be in format "namespace.name", "%s" given.', $name) |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
list($namespace, $name) = explode('.', $name); |
53
|
|
|
|
54
|
|
|
$settings = $this->settingsManager->load($namespace); |
55
|
|
|
|
56
|
|
|
return $settings->get($name); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param $name |
61
|
|
|
* |
62
|
|
|
* @throws \Exception |
63
|
|
|
*/ |
64
|
|
|
public function setActivedService($name) |
65
|
|
|
{ |
66
|
|
|
if ($name && !$this->has($name)) { |
67
|
|
|
throw new \Exception(sprintf('Cannot set unregistered confirmation type "%s".', $name)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->activedService = $name; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param ConfirmationInterface $confirmation |
75
|
|
|
* @param array $options |
76
|
|
|
*/ |
77
|
|
|
public function add(ConfirmationInterface $confirmation, array $options = array()) |
78
|
|
|
{ |
79
|
|
|
$this->types[$confirmation->getType()] = $confirmation; |
80
|
|
|
|
81
|
|
|
if (!empty($options)) { |
82
|
|
|
$this->options[$confirmation->getType()] = $options; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param $name |
88
|
|
|
* |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
public function has($name) |
92
|
|
|
{ |
93
|
|
|
return array_key_exists($name, $this->types); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param $name |
98
|
|
|
* |
99
|
|
|
* @return ConfirmationInterface |
100
|
|
|
* |
101
|
|
|
* @throws \Exception |
102
|
|
|
*/ |
103
|
|
|
public function create($name) |
104
|
|
|
{ |
105
|
|
|
if (!$this->has($name)) { |
106
|
|
|
throw new \Exception(sprintf('Not found confirmation type "%s".', $name)); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** @var ConfirmationInterface $instance */ |
110
|
|
|
$instance = $this->types[$name]; |
111
|
|
|
|
112
|
|
|
if (array_key_exists($name, $this->options)) { |
113
|
|
|
$instance->resetOptions($this->options[$name]); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $instance; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param bool $invalidException |
121
|
|
|
* |
122
|
|
|
* @return ConfirmationInterface|null |
123
|
|
|
* |
124
|
|
|
* @throws \Exception |
125
|
|
|
*/ |
126
|
|
|
public function createActivedConfirmation($invalidException = false) |
127
|
|
|
{ |
128
|
|
|
try { |
129
|
|
|
return $this->create($this->activedService); |
130
|
|
|
} catch (\Exception $e) { |
131
|
|
|
if ($invalidException) { |
132
|
|
|
throw $e; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|