1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\Payments\Gateways\Providers\AbstractGateway\Message\Traits; |
4
|
|
|
|
5
|
|
|
use ByTIC\Payments\Models\Purchase\Traits\IsPurchasableModelTrait; |
6
|
|
|
use Nip\Records\AbstractModels\Record; |
7
|
|
|
use Nip\Records\AbstractModels\RecordManager; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class HasModelRequest |
11
|
|
|
* @package ByTIC\Common\Payments\Gateways\Providers\AbstractGateway\Message\Traits |
12
|
|
|
* |
13
|
|
|
* @property \Symfony\Component\HttpFoundation\Request $httpRequest |
14
|
|
|
*/ |
15
|
|
|
trait HasModelRequest |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param string $value |
20
|
|
|
* @return mixed |
21
|
|
|
*/ |
22
|
14 |
|
public function setModelManager($value) |
23
|
|
|
{ |
24
|
14 |
|
return $this->setParameter('modelManager', $value); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return bool |
29
|
|
|
*/ |
30
|
9 |
|
protected function validateModel() |
31
|
|
|
{ |
32
|
9 |
|
$model = $this->generateModelFromRequest(); |
33
|
9 |
|
if ($this->isValidModel($model)) { |
34
|
9 |
|
$this->setModel($model); |
35
|
9 |
|
return true; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return false; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return bool|IsPurchasableModelTrait|Record |
43
|
|
|
*/ |
44
|
9 |
|
protected function generateModelFromRequest() |
45
|
|
|
{ |
46
|
9 |
|
$model = $this->generateModelFromRequestBody(); |
47
|
9 |
|
if ($this->isValidModel($model)) { |
48
|
3 |
|
return $model; |
49
|
|
|
} else { |
50
|
6 |
|
return $this->generateModelFromRequestQuery(); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return bool|IsPurchasableModelTrait|Record |
56
|
|
|
*/ |
57
|
9 |
|
protected function generateModelFromRequestBody() |
58
|
|
|
{ |
59
|
9 |
|
$idModel = $this->getModelIdFromRequest(); |
60
|
9 |
|
$id = intval($idModel); |
61
|
9 |
|
if (strlen($idModel) === strlen($id) && is_int($id) && $id > 0) { |
62
|
3 |
|
return $this->findModel($idModel); |
63
|
|
|
} |
64
|
6 |
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Returns ID if it has it |
69
|
|
|
* @return int |
70
|
|
|
*/ |
71
|
4 |
|
public function getModelIdFromRequest() |
72
|
|
|
{ |
73
|
4 |
|
if (is_callable('parent::getModelIdFromRequest')) { |
74
|
|
|
/** @noinspection PhpUndefinedClassInspection */ |
75
|
|
|
return parent::getModelIdFromRequest(); |
76
|
|
|
} |
77
|
4 |
|
return false; |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param $id |
82
|
|
|
* @return IsPurchasableModelTrait|Record |
83
|
|
|
*/ |
84
|
7 |
|
protected function findModel($id) |
85
|
|
|
{ |
86
|
7 |
|
return $this->getModelManager()->findOne($id); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return RecordManager |
91
|
|
|
*/ |
92
|
9 |
|
public function getModelManager() |
93
|
|
|
{ |
94
|
9 |
|
return $this->getParameter('modelManager'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param $model |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
9 |
|
protected function isValidModel($model) |
102
|
|
|
{ |
103
|
9 |
|
return is_object($model); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return bool|IsPurchasableModelTrait |
108
|
|
|
*/ |
109
|
6 |
|
protected function generateModelFromRequestQuery() |
110
|
|
|
{ |
111
|
6 |
|
$pkValue = $this->getModelPKFromRequestQuery(); |
112
|
6 |
|
$field = $this->getModelUrlPkRequestKey(); |
113
|
6 |
|
if ($pkValue) { |
114
|
6 |
|
return $this->findModelByField($field, $pkValue); |
|
|
|
|
115
|
|
|
} |
116
|
|
|
return false; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* returns key in confirm URL Query |
121
|
|
|
* @return int |
122
|
|
|
*/ |
123
|
6 |
|
public function getModelPKFromRequestQuery() |
124
|
|
|
{ |
125
|
6 |
|
$modelKey = $this->getModelUrlPkRequestKey(); |
126
|
|
|
|
127
|
6 |
|
return $this->getHttpRequest()->query->get($modelKey); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
6 |
|
public function getModelUrlPkRequestKey() |
134
|
|
|
{ |
135
|
6 |
|
$modelIdMethod = 'getPaymentsUrlPK'; |
136
|
6 |
|
if (method_exists($this->getModelManager(), $modelIdMethod)) { |
137
|
6 |
|
return $this->getModelManager()->$modelIdMethod(); |
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return 'id'; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return \Symfony\Component\HttpFoundation\Request |
145
|
|
|
*/ |
146
|
6 |
|
public function getHttpRequest() |
147
|
|
|
{ |
148
|
6 |
|
return $this->httpRequest; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param $field |
153
|
|
|
* @param $value |
154
|
|
|
* @return IsPurchasableModelTrait |
155
|
|
|
*/ |
156
|
6 |
|
protected function findModelByField($field, $value) |
157
|
|
|
{ |
158
|
6 |
|
if ($field == 'id') { |
159
|
4 |
|
return $this->findModel($value); |
|
|
|
|
160
|
|
|
} |
161
|
2 |
|
$method = 'findOneBy' . ucfirst($field); |
162
|
2 |
|
return $this->getModelManager()->$method($value); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param $model |
167
|
|
|
* @return $this |
168
|
|
|
*/ |
169
|
9 |
|
protected function setModel($model) |
170
|
|
|
{ |
171
|
9 |
|
$this->setParameter('id', $model->id); |
172
|
9 |
|
$this->setParameter('model', $model); |
173
|
9 |
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param $idModel |
178
|
|
|
* @return bool |
179
|
|
|
*/ |
180
|
|
|
protected function setModelFromId($idModel) |
181
|
|
|
{ |
182
|
|
|
$model = $this->findModel($idModel); |
183
|
|
|
if ($model) { |
|
|
|
|
184
|
|
|
$this->setModel($model); |
185
|
|
|
return true; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return false; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @return mixed |
193
|
|
|
*/ |
194
|
9 |
|
protected function getModel() |
195
|
|
|
{ |
196
|
9 |
|
return $this->getParameter('model'); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @inheritDoc |
201
|
|
|
*/ |
202
|
|
|
abstract protected function setParameter($key, $value); |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @inheritDoc |
206
|
|
|
*/ |
207
|
|
|
abstract protected function getParameter($key); |
208
|
|
|
} |
209
|
|
|
|