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