Skip to content

Commit 85d8182

Browse files
authored
Merge pull request #21 from paynl/feature/PLUG-4352
PLUG-4352 - Added filter arguments for TerminalsBrowseRequest
2 parents 8a93369 + 0e818a4 commit 85d8182

5 files changed

Lines changed: 132 additions & 70 deletions

File tree

Tests/Unit/TerminalsBrowseRequestTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,60 @@ public function testStartWrongConfig()
4949
$this->assertEquals('Something went wrong', $e->getFriendlyMessage());
5050
}
5151
}
52+
53+
/**
54+
* @return void
55+
*/
56+
public function testPathParametersWithMerchantCode()
57+
{
58+
$request = new TerminalsBrowseRequest();
59+
$request->setMerchantCode('M-1111-2222');
60+
61+
$params = $request->getPathParameters();
62+
63+
$this->assertArrayHasKey('merchant[eq]', $params);
64+
$this->assertEquals('M-1111-2222', $params['merchant[eq]']);
65+
$this->assertArrayNotHasKey('merchant[neq]', $params);
66+
}
67+
68+
/**
69+
* @return void
70+
*/
71+
public function testPathParametersWithExcludeMerchantCode()
72+
{
73+
$request = new TerminalsBrowseRequest();
74+
$request->setExcludeMerchantCode('M-9999-8888');
75+
76+
$params = $request->getPathParameters();
77+
78+
$this->assertArrayHasKey('merchant[neq]', $params);
79+
$this->assertEquals('M-9999-8888', $params['merchant[neq]']);
80+
$this->assertArrayNotHasKey('merchant[eq]', $params);
81+
}
82+
83+
/**
84+
* @return void
85+
*/
86+
public function testPathParametersWithBoth()
87+
{
88+
$request = new TerminalsBrowseRequest();
89+
$request
90+
->setMerchantCode('M-1111-2222')
91+
->setExcludeMerchantCode('M-9999-8888');
92+
93+
$params = $request->getPathParameters();
94+
95+
$this->assertEquals('M-1111-2222', $params['merchant[eq]']);
96+
$this->assertEquals('M-9999-8888', $params['merchant[neq]']);
97+
}
98+
99+
/**
100+
* @return void
101+
*/
102+
public function testPathParametersEmpty()
103+
{
104+
$request = new TerminalsBrowseRequest();
105+
$params = $request->getPathParameters();
106+
$this->assertEmpty($params);
107+
}
52108
}

src/Mapper/Manager.php

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,12 @@ class Manager extends AbstractPluginManager
3434
*/
3535
protected $mapping = [];
3636

37+
3738
/**
38-
* @inheritDoc
39-
*
40-
* @throws ServiceNotCreatedException
41-
* @throws ServiceNotFoundException
42-
* @throws MapperSourceServiceNotFoundException
43-
* @throws MapperTargetServiceNotFoundException
44-
*
45-
* @SuppressWarnings(PHPMD.StaticAccess)
39+
* @param array $config
40+
* @return ServiceManager
41+
* @throws \Psr\Container\ContainerExceptionInterface
42+
* @throws \Psr\Container\NotFoundExceptionInterface
4643
*/
4744
public function configure(array $config): ServiceManager
4845
{
@@ -71,7 +68,7 @@ public function configure(array $config): ServiceManager
7168
$mappingConfig[$mapper] = $map;
7269
unset($mappingConfig[$mapperAlias]);
7370

74-
// determine the necessary managers
71+
# Determine the necessary managers
7572
preg_match_all('/((?:^|[A-Z])[a-z]+)/', Misc::getClassNameByFQN($mapper), $matches);
7673
if (2 > count($matches[1])) {
7774
throw new ServiceNotFoundException(
@@ -82,7 +79,7 @@ public function configure(array $config): ServiceManager
8279
);
8380
}
8481

85-
// determine the name of the source and target
82+
# Determine the name of the source and target
8683
$sourceManagerName = lcfirst(current($matches[1]));
8784
next($matches[1]);
8885
$targetManagerName = lcfirst(current($matches[1]));
@@ -93,22 +90,10 @@ public function configure(array $config): ServiceManager
9390
# Check the map
9491
foreach ($map as $source => $target) {
9592
if ($sourceManager->has($source) === false) {
96-
throw new MapperSourceServiceNotFoundException(
97-
sprintf(
98-
'Mapping source service with name "%s" not found in %s',
99-
$source,
100-
$sourceManagerName
101-
)
102-
);
93+
throw new MapperSourceServiceNotFoundException(sprintf('Mapping source service with name "%s" not found in %s', $source, $sourceManagerName));
10394
}
10495
if ($targetManager->has($target) === false) {
105-
throw new MapperTargetServiceNotFoundException(
106-
sprintf(
107-
'Mapping target service with name "%s" not found in %s',
108-
$target,
109-
$targetManagerName
110-
)
111-
);
96+
throw new MapperTargetServiceNotFoundException(sprintf('Mapping target service with name "%s" not found in %s', $target, $targetManagerName));
11297
}
11398
}
11499

@@ -121,7 +106,8 @@ public function configure(array $config): ServiceManager
121106
}
122107

123108
/**
124-
* @inheritDoc
109+
* @param array $config
110+
* @return void
125111
*/
126112
protected function validateOverrides(array $config): void
127113
{

src/Model/Request/TerminalsBrowseRequest.php

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,55 @@
1616
*/
1717
class TerminalsBrowseRequest extends RequestData
1818
{
19-
public function __construct()
19+
protected string $merchantCode;
20+
protected string $excludeMerchantCode;
21+
22+
/**
23+
* @param string $merchantCode
24+
* @param string $excludeMerchantCode
25+
*/
26+
public function __construct(string $merchantCode = '', string $excludeMerchantCode = '')
2027
{
28+
$this->setMerchantCode($merchantCode);
29+
$this->setExcludeMerchantCode($excludeMerchantCode);
2130
parent::__construct('TerminalsBrowse', '/terminals', RequestInterface::METHOD_GET);
2231
}
2332

2433
/**
25-
* @return array
34+
* @param string $merchantCode
35+
* @return $this
36+
*/
37+
public function setMerchantCode(string $merchantCode): self
38+
{
39+
$this->merchantCode = $merchantCode;
40+
return $this;
41+
}
42+
43+
/**
44+
* @param string $excludeMerchantCode
45+
* @return $this
46+
*/
47+
public function setExcludeMerchantCode(string $excludeMerchantCode): self
48+
{
49+
$this->excludeMerchantCode = $excludeMerchantCode;
50+
return $this;
51+
}
52+
53+
/**
54+
* @return array|null[]
2655
*/
2756
public function getPathParameters(): array
2857
{
29-
return [];
58+
$parameters = [];
59+
60+
if (!empty($this->merchantCode)) {
61+
$parameters['merchant[eq]'] = $this->merchantCode;
62+
}
63+
if (!empty($this->excludeMerchantCode)) {
64+
$parameters['merchant[neq]'] = $this->excludeMerchantCode;
65+
}
66+
67+
return $parameters;
3068
}
3169

3270
/**
@@ -47,4 +85,4 @@ public function start(): Terminals
4785
$this->config->setVersion(2);
4886
return parent::start();
4987
}
50-
}
88+
}

src/Request/AbstractRequest.php

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -160,44 +160,31 @@ public function hasParam($name): bool
160160

161161
/**
162162
* @param array $params
163-
*
164-
* @throws MissingParamException
165-
* @throws InvalidArgumentException
166-
*
167-
* @return static
163+
* @return $this
168164
*/
169165
public function setParams(array $params): self
170166
{
171167
$this->params = $params;
172168

173-
foreach ($this->getRequiredParams() as $paramName => $paramDefinition) {
174-
if (false === $this->hasParam($paramName)) {
175-
throw new MissingParamException(sprintf('Missing param "%s"', $paramName));
176-
}
177-
178-
if (true === is_string($paramDefinition) && '' !== $paramDefinition && 1 !== preg_match("/^{$paramDefinition}$/", $this->getParam($paramName))) {
179-
throw new InvalidArgumentException(sprintf('Required param %s is not valid. It must match "%s"', $paramName, $paramDefinition));
180-
}
169+
$queryParams = [];
170+
$uri = $this->getUri();
181171

182-
# Set it in the array
183-
$this->setUri(str_replace("%{$paramName}%", $this->getParam($paramName), $this->getUri()));
184-
}
172+
foreach ($params as $key => $value) {
173+
$placeholder = "%{$key}%";
185174

186-
$optionalParams = [];
187-
foreach ($this->getOptionalParams() as $paramName => $paramDefinition) {
188-
# If optional paramater is provided...
189-
if (isset($params[$paramName])) {
190-
if (true === is_string($paramDefinition) && '' !== $paramDefinition && 1 !== preg_match("/^{$paramDefinition}$/", $this->getParam($paramName))) {
191-
throw new InvalidArgumentException(sprintf('Optional param %s is not valid. It must match "%s"', $paramName, $paramDefinition));
192-
}
193-
$optionalParams[$paramName] = $this->params[$paramName];
175+
if (strpos($uri, $placeholder) !== false) {
176+
$uri = str_replace($placeholder, $value, $uri);
177+
} else {
178+
$queryParams[$key] = $value;
194179
}
195180
}
196181

197-
if (!empty($optionalParams)) {
198-
$this->setUri($this->getUri() . '?' . http_build_query($optionalParams));
182+
if (!empty($queryParams)) {
183+
$uri .= '?' . http_build_query($queryParams);
199184
}
200185

186+
$this->setUri($uri);
187+
201188
return $this;
202189
}
203190

src/Request/ConfigProvider.php

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
class ConfigProvider implements ConfigProviderInterface
1818
{
1919
/**
20-
* @inheritDoc
20+
* @return array
2121
*/
2222
public function __invoke(): array
2323
{
@@ -38,7 +38,7 @@ public function __invoke(): array
3838
}
3939

4040
/**
41-
* @inheritDoc
41+
* @return array
4242
*/
4343
public function getDependencyConfig(): array
4444
{
@@ -65,10 +65,10 @@ public function getRequestConfig(): array
6565
DebugAwareInitializer::class,
6666
],
6767
'services' => array_merge(
68-
$this->getIsPayServicesConfig(),
69-
$this->getPinServicesConfig(),
70-
$this->getServiceServicesConfig(),
71-
$this->getTransactionServicesConfig(),
68+
$this->getIsPayServicesConfig(),
69+
$this->getPinServicesConfig(),
70+
$this->getServiceServicesConfig(),
71+
$this->getTransactionServicesConfig(),
7272
),
7373
'factories' => [
7474
Request::class => Factory::class,
@@ -108,11 +108,7 @@ protected function getPinServicesConfig(): array
108108
'method' => RequestInterface::METHOD_GET,
109109
'requiredParams' => ['terminalCode' => ''],
110110
],
111-
'TerminalsBrowse' => [
112-
'uri' => '/terminals',
113-
'method' => RequestInterface::METHOD_GET,
114-
'requiredParams' => [],
115-
],
111+
'TerminalsBrowse' => [],
116112
'ConfirmTerminalTransaction' => [
117113
'uri' => '/pin/%terminalTransactionId%/confirm',
118114
'method' => RequestInterface::METHOD_PATCH,
@@ -206,13 +202,13 @@ protected function getTransactionServicesConfig(): array
206202
'transactionId' => '',
207203
],
208204
],
209-
'OrderUpdate' => [
205+
'OrderUpdate' => [
210206
'uri' => '/',
211207
'method' => RequestInterface::METHOD_PATCH,
212208
'requiredParams' => [
213209
'transactionId' => '',
214210
],
215-
],
211+
],
216212
'OrderCapture' => [
217213
'uri' => '',
218214
'requiredParams' => [
@@ -241,7 +237,7 @@ protected function getTransactionServicesConfig(): array
241237
'method' => RequestInterface::METHOD_GET,
242238
'requiredParams' => [
243239
'transactionId' => '',
244-
],
240+
],
245241
],
246242
'OrderStatus' => [
247243
'uri' => '/transactions/%transactionId%/status',
@@ -280,5 +276,4 @@ protected function getTransactionServicesConfig(): array
280276
]
281277
];
282278
}
283-
284279
}

0 commit comments

Comments
 (0)