1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nexy\PayboxDirect\Request; |
4
|
|
|
|
5
|
|
|
use Greg0ire\Enum\Bridge\Symfony\Validator\Constraint\Enum; |
6
|
|
|
use Nexy\PayboxDirect\Enum\Activity; |
7
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Sullivan Senechal <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
abstract class AbstractRequest implements RequestInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var int |
16
|
|
|
* |
17
|
|
|
* @Assert\NotBlank |
18
|
|
|
* @Enum(class="Nexy\PayboxDirect\Enum\Activity", showKeys=true) |
19
|
|
|
*/ |
20
|
|
|
private $activity = Activity::WEB_REQUEST; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var \DateTime |
24
|
|
|
* |
25
|
|
|
* @Assert\Type("\DateTime") |
26
|
|
|
*/ |
27
|
|
|
private $date = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var bool |
31
|
|
|
* |
32
|
|
|
* @Assert\Type("bool") |
33
|
|
|
*/ |
34
|
|
|
private $showCountry = false; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var bool |
38
|
|
|
* |
39
|
|
|
* @Assert\Type("bool") |
40
|
|
|
*/ |
41
|
|
|
private $showSha1 = false; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var bool |
45
|
|
|
* |
46
|
|
|
* @Assert\Type("bool") |
47
|
|
|
*/ |
48
|
|
|
private $showCardType = false; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string|null |
52
|
|
|
* |
53
|
|
|
* @Assert\Type("string") |
54
|
|
|
* @Assert\Length(min=1, max=250) |
55
|
|
|
*/ |
56
|
|
|
private $subscriberRef = null; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param null|string $subscriberRef |
60
|
|
|
*/ |
61
|
|
|
public function __construct($subscriberRef = null) |
62
|
|
|
{ |
63
|
|
|
$this->subscriberRef = $subscriberRef; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param int $activity |
68
|
|
|
* |
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
|
|
final public function setActivity($activity) |
72
|
|
|
{ |
73
|
|
|
$this->activity = $activity; |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param \DateTime|null $date |
80
|
|
|
* |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
|
|
final public function setDate(\DateTime $date = null) |
84
|
|
|
{ |
85
|
|
|
$this->date = $date; |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param bool $showCountry |
92
|
|
|
* |
93
|
|
|
* @return $this |
94
|
|
|
*/ |
95
|
|
|
final public function setShowCountry($showCountry) |
96
|
|
|
{ |
97
|
|
|
$this->showCountry = $showCountry; |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param bool $showSha1 |
104
|
|
|
* |
105
|
|
|
* @return $this |
106
|
|
|
*/ |
107
|
|
|
final public function setShowSha1($showSha1) |
108
|
|
|
{ |
109
|
|
|
$this->showSha1 = $showSha1; |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param bool $showCardType |
116
|
|
|
* |
117
|
|
|
* @return $this |
118
|
|
|
*/ |
119
|
|
|
final public function setShowCardType($showCardType) |
120
|
|
|
{ |
121
|
|
|
$this->showCardType = $showCardType; |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
|
|
final protected function hasSubscriberRef() |
130
|
|
|
{ |
131
|
|
|
return !empty($this->subscriberRef); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return null|string |
136
|
|
|
*/ |
137
|
|
|
final protected function getSubscriberRef() |
138
|
|
|
{ |
139
|
|
|
return $this->subscriberRef; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
|
|
public function getParameters() |
146
|
|
|
{ |
147
|
|
|
$parameters = [ |
148
|
|
|
'ACTIVITE' => $this->activity, |
149
|
|
|
'DATEQ' => $this->date instanceof \DateTime ? $this->date->format('dmYHis') : null, |
150
|
|
|
]; |
151
|
|
|
|
152
|
|
|
if ($this->showCountry) { |
153
|
|
|
$parameters['PAYS'] = ''; |
154
|
|
|
} |
155
|
|
|
if ($this->showSha1) { |
156
|
|
|
$parameters['SHA-1'] = ''; |
157
|
|
|
} |
158
|
|
|
if ($this->showCardType) { |
159
|
|
|
$parameters['TYPECARTE'] = ''; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
if (method_exists($this, 'getTransactionNumber')) { |
163
|
|
|
$parameters['NUMTRANS'] = $this->getTransactionNumber(); |
|
|
|
|
164
|
|
|
} |
165
|
|
|
if (method_exists($this, 'getCallNumber')) { |
166
|
|
|
$parameters['NUMAPPEL'] = $this->getCallNumber(); |
|
|
|
|
167
|
|
|
} |
168
|
|
|
if (method_exists($this, 'hasAuthorization') && $this->hasAuthorization()) { |
|
|
|
|
169
|
|
|
$parameters['AUTORISATION'] = $this->getAuthorization(); |
|
|
|
|
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
// Direct Plus requests special case. |
173
|
|
|
if ($this->hasSubscriberRef()) { |
174
|
|
|
$parameters['REFABONNE'] = $this->getSubscriberRef(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return $parameters; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: