Skip to content

Commit 4a3c9b0

Browse files
authored
Use ParameterTrait (#184)
1 parent 66b7934 commit 4a3c9b0

File tree

6 files changed

+96
-149
lines changed

6 files changed

+96
-149
lines changed

src/Common/AbstractGateway.php

+1-33
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,7 @@
4444
*/
4545
abstract class AbstractGateway implements GatewayInterface
4646
{
47-
/**
48-
* @var \Symfony\Component\HttpFoundation\ParameterBag
49-
*/
50-
protected $parameters;
47+
use ParametersTrait;
5148

5249
/**
5350
* @var ClientInterface
@@ -114,35 +111,6 @@ public function getDefaultParameters()
114111
return array();
115112
}
116113

117-
/**
118-
* @return array
119-
*/
120-
public function getParameters()
121-
{
122-
return $this->parameters->all();
123-
}
124-
125-
/**
126-
* @param string $key
127-
* @return mixed
128-
*/
129-
public function getParameter($key)
130-
{
131-
return $this->parameters->get($key);
132-
}
133-
134-
/**
135-
* @param string $key
136-
* @param mixed $value
137-
* @return $this
138-
*/
139-
public function setParameter($key, $value)
140-
{
141-
$this->parameters->set($key, $value);
142-
143-
return $this;
144-
}
145-
146114
/**
147115
* @return boolean
148116
*/

src/Common/CreditCard.php

+4-43
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@
9393
*/
9494
class CreditCard
9595
{
96+
use ParametersTrait;
97+
9698
const BRAND_VISA = 'visa';
9799
const BRAND_MASTERCARD = 'mastercard';
98100
const BRAND_DISCOVER = 'discover';
@@ -133,13 +135,6 @@ class CreditCard
133135
self::BRAND_LASER => '/^(6304|6706|6709|6771(?!89))\d{8}(\d{4}|\d{6,7})?$/',
134136
);
135137

136-
/**
137-
* Internal storage of all of the card parameters.
138-
*
139-
* @var \Symfony\Component\HttpFoundation\ParameterBag
140-
*/
141-
protected $parameters;
142-
143138
/**
144139
* Create a new CreditCard object using the specified parameters
145140
*
@@ -205,40 +200,6 @@ public function initialize(array $parameters = null)
205200
return $this;
206201
}
207202

208-
/**
209-
* Get all parameters.
210-
*
211-
* @return array An associative array of parameters.
212-
*/
213-
public function getParameters()
214-
{
215-
return $this->parameters->all();
216-
}
217-
218-
/**
219-
* Get one parameter.
220-
*
221-
* @return mixed A single parameter value.
222-
*/
223-
protected function getParameter($key)
224-
{
225-
return $this->parameters->get($key);
226-
}
227-
228-
/**
229-
* Set one parameter.
230-
*
231-
* @param string $key Parameter key
232-
* @param mixed $value Parameter value
233-
* @return $this
234-
*/
235-
protected function setParameter($key, $value)
236-
{
237-
$this->parameters->set($key, $value);
238-
239-
return $this;
240-
}
241-
242203
/**
243204
* Set the credit card year.
244205
*
@@ -269,8 +230,9 @@ protected function setYearParameter($key, $value)
269230
* Generally if you want to validate the credit card yourself with custom error
270231
* messages, you should use your framework's validation library, not this method.
271232
*
272-
* @throws InvalidCreditCardException
273233
* @return void
234+
* @throws Exception\InvalidRequestException
235+
* @throws InvalidCreditCardException
274236
*/
275237
public function validate()
276238
{
@@ -298,7 +260,6 @@ public function validate()
298260
throw new InvalidCreditCardException('Card number should have 12 to 19 digits');
299261
}
300262
}
301-
302263
/**
303264
* Get Card Title.
304265
*

src/Common/Item.php

+1-24
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616
*/
1717
class Item implements ItemInterface
1818
{
19-
/**
20-
* @var \Symfony\Component\HttpFoundation\ParameterBag
21-
*/
22-
protected $parameters;
19+
use ParametersTrait;
2320

2421
/**
2522
* Create a new item with the specified parameters
@@ -46,26 +43,6 @@ public function initialize(array $parameters = null)
4643
return $this;
4744
}
4845

49-
/**
50-
* @return array
51-
*/
52-
public function getParameters()
53-
{
54-
return $this->parameters->all();
55-
}
56-
57-
protected function getParameter($key)
58-
{
59-
return $this->parameters->get($key);
60-
}
61-
62-
protected function setParameter($key, $value)
63-
{
64-
$this->parameters->set($key, $value);
65-
66-
return $this;
67-
}
68-
6946
/**
7047
* {@inheritDoc}
7148
*/

src/Common/Message/AbstractRequest.php

+5-49
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Omnipay\Common\Http\Client;
1919
use Omnipay\Common\Http\ClientInterface;
2020
use Omnipay\Common\ItemBag;
21+
use Omnipay\Common\ParametersTrait;
2122
use Symfony\Component\HttpFoundation\ParameterBag;
2223
use Symfony\Component\HttpFoundation\Request as HttpRequest;
2324

@@ -66,12 +67,9 @@
6667
*/
6768
abstract class AbstractRequest implements RequestInterface
6869
{
69-
/**
70-
* The request parameters
71-
*
72-
* @var \Symfony\Component\HttpFoundation\ParameterBag
73-
*/
74-
protected $parameters;
70+
use ParametersTrait {
71+
setParameter as traitSetParameter;
72+
}
7573

7674
/**
7775
* The request client.
@@ -145,27 +143,6 @@ public function initialize(array $parameters = array())
145143
return $this;
146144
}
147145

148-
/**
149-
* Get all parameters as an associative array.
150-
*
151-
* @return array
152-
*/
153-
public function getParameters()
154-
{
155-
return $this->parameters->all();
156-
}
157-
158-
/**
159-
* Get a single parameter.
160-
*
161-
* @param string $key The parameter key
162-
* @return mixed
163-
*/
164-
protected function getParameter($key)
165-
{
166-
return $this->parameters->get($key);
167-
}
168-
169146
/**
170147
* Set a single parameter
171148
*
@@ -180,9 +157,7 @@ protected function setParameter($key, $value)
180157
throw new RuntimeException('Request cannot be modified after it has been sent!');
181158
}
182159

183-
$this->parameters->set($key, $value);
184-
185-
return $this;
160+
return $this->traitSetParameter($key, $value);
186161
}
187162

188163
/**
@@ -206,25 +181,6 @@ public function setTestMode($value)
206181
return $this->setParameter('testMode', $value);
207182
}
208183

209-
/**
210-
* Validate the request.
211-
*
212-
* This method is called internally by gateways to avoid wasting time with an API call
213-
* when the request is clearly invalid.
214-
*
215-
* @param string ... a variable length list of required parameters
216-
* @throws InvalidRequestException
217-
*/
218-
public function validate()
219-
{
220-
foreach (func_get_args() as $key) {
221-
$value = $this->parameters->get($key);
222-
if (! isset($value)) {
223-
throw new InvalidRequestException("The $key parameter is required");
224-
}
225-
}
226-
}
227-
228184
/**
229185
* Get the card.
230186
*

src/Common/ParametersTrait.php

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Omnipay\Common;
4+
5+
use Omnipay\Common\Exception\InvalidRequestException;
6+
use Symfony\Component\HttpFoundation\ParameterBag;
7+
8+
trait ParametersTrait
9+
{
10+
/**
11+
* Internal storage of all of the parameters.
12+
*
13+
* @var ParameterBag
14+
*/
15+
protected $parameters;
16+
17+
/**
18+
* Set one parameter.
19+
*
20+
* @param string $key Parameter key
21+
* @param mixed $value Parameter value
22+
* @return $this
23+
*/
24+
protected function setParameter($key, $value)
25+
{
26+
$this->parameters->set($key, $value);
27+
28+
return $this;
29+
}
30+
31+
/**
32+
* Get one parameter.
33+
*
34+
* @return mixed A single parameter value.
35+
*/
36+
protected function getParameter($key)
37+
{
38+
return $this->parameters->get($key);
39+
}
40+
41+
/**
42+
* Get all parameters.
43+
*
44+
* @return array An associative array of parameters.
45+
*/
46+
public function getParameters()
47+
{
48+
return $this->parameters->all();
49+
}
50+
51+
/**
52+
* Initialize the object with parameters.
53+
*
54+
* If any unknown parameters passed, they will be ignored.
55+
*
56+
* @param array $parameters An associative array of parameters
57+
* @return $this.
58+
*/
59+
public function initialize(array $parameters = [])
60+
{
61+
$this->parameters = new ParameterBag;
62+
Helper::initialize($this, $parameters);
63+
return $this;
64+
}
65+
66+
/**
67+
* Validate the request.
68+
*
69+
* This method is called internally by gateways to avoid wasting time with an API call
70+
* when the request is clearly invalid.
71+
*
72+
* @param string ... a variable length list of required parameters
73+
* @throws InvalidRequestException
74+
*/
75+
public function validate(...$args)
76+
{
77+
foreach ($args as $key) {
78+
$value = $this->parameters->get($key);
79+
if (! isset($value)) {
80+
throw new InvalidRequestException("The $key parameter is required");
81+
}
82+
}
83+
}
84+
}

tests/Omnipay/Common/CreditCardTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Omnipay\Common;
44

5+
use Omnipay\Common\Exception\InvalidRequestException;
56
use Omnipay\Tests\TestCase;
67

78
class CreditCardTest extends TestCase

0 commit comments

Comments
 (0)