Skip to content

Commit 0734e07

Browse files
authored
Merge pull request #25 from finix-payments/master
v1.0.2
2 parents f9e2f07 + 333d686 commit 0734e07

33 files changed

+549
-1100
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This file is for unifying the coding style for different editors and IDEs
2+
# editorconfig.org
3+
4+
# PHP PSR-2 Coding Standards
5+
# http://www.php-fig.org/psr/psr-2/
6+
7+
root = true
8+
9+
[*.php]
10+
charset = utf-8
11+
end_of_line = lf
12+
insert_final_newline = true
13+
trim_trailing_whitespace = true
14+
indent_style = space
15+
indent_size = 4

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ require_once("/path/to/Finix/Bootstrap.php");
3737

3838
```php
3939
require(__DIR__ . '/src/Finix/Settings.php');
40-
Finix\Settings::configure('http://b.papi.staging.finix.io', '$USERNAME', '$PASSWORD');
40+
Finix\Settings::configure('https://api-staging.finix.io/', '$USERNAME', '$PASSWORD');
4141
require(__DIR__ . '/src/Finix/Bootstrap.php');
4242
\Finix\Bootstrap::init();
4343
```
@@ -63,5 +63,3 @@ See `circle.yml` for more details.
6363
- Install [MITM Proxy](https://mitmproxy.org/)
6464
- `sudo mitmdump -P http://b.papi.staging.finix.io -a -vv -p 80`
6565
- Run the tests, see the request / response
66-
67-

bootstrap.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
<?php
22

33
require(__DIR__ . '/src/Finix/Settings.php');
4-
require(__DIR__ . '/tests/Finix/SampleData.php');
4+
require(__DIR__ . '/tests/Finix/Fixtures.php');
55
require(__DIR__ . '/src/Finix/Bootstrap.php');
66

7-
use \Finix\Tests\SampleData;
7+
use \Finix\Tests\Fixtures;
88
use \Finix\Settings;
99
use \Finix\Bootstrap;
1010

11-
Settings::configure(
12-
SampleData::$apiUrl,
13-
SampleData::$username,
14-
SampleData::$password
15-
);
11+
Settings::configure([
12+
"root_url" => Fixtures::$apiUrl
13+
]);
1614

17-
Bootstrap::init();
15+
Bootstrap::init();

circle.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ dependencies:
1414
test:
1515
override:
1616
- mkdir -p $CIRCLE_TEST_REPORTS/phpunit
17-
- ./vendor/bin/phpunit --log-junit $CIRCLE_TEST_REPORTS/phpunit/junit.xml
17+
- ./vendor/bin/phpunit --log-junit $CIRCLE_TEST_REPORTS/phpunit/junit.xml:
18+
timeout: 1200

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "finix/processing-php",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "A PHP HTTP Client conforming to the HAL hypermedia type for the Finix processing API",
55
"license": "Apache2",
66
"authors": [

src/Finix/Bootstrap.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Finix;
44
use Finix\Resources;
5+
use Finix\Http\Auth\BasicAuthentication;
56

67
/**
78
* Bootstrapper for Finix does autoloading and resource initialization.
@@ -53,11 +54,13 @@ private static function _autoload($base, $classname)
5354
*/
5455
private static function initializeResources()
5556
{
56-
if (self::$initialized)
57+
if (self::$initialized) {
5758
return;
59+
}
5860

5961
Resource::init();
6062

63+
Resources\User::init();
6164
Resources\Application::init();
6265
Resources\Identity::init();
6366
Resources\Processor::init();
@@ -74,4 +77,19 @@ private static function initializeResources()
7477

7578
self::$initialized = true;
7679
}
77-
}
80+
81+
public static function createClient()
82+
{
83+
if (Settings::$username == null || Settings::$password == null) {
84+
$client = new Hal\Client(Settings::$url_root, '/');
85+
}
86+
else {
87+
$client = new Hal\Client(
88+
Settings::$url_root,
89+
'/',
90+
null,
91+
new BasicAuthentication(Settings::$username, Settings::$password));
92+
}
93+
return $client;
94+
}
95+
}

src/Finix/Http/Auth/BasicAuthentication.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Finix\Http\Auth;
44

55
use Finix\Http;
6+
use Finix\Settings;
67
use Finix\Http\AbstractClient;
78
use GuzzleHttp\Message\RequestInterface;
89

@@ -86,7 +87,8 @@ private function isRequestAuthorized(RequestInterface $httpRequest)
8687
*/
8788
private function getCredentials()
8889
{
89-
$basic = base64_encode($this->user_id . ':' . $this->password);
90+
$basic = base64_encode(Settings::$username . ':' . Settings::$password);
91+
// $basic = base64_encode($this->user_id . ':' . $this->password);
9092
return $basic;
9193
}
9294
}

src/Finix/Pagination.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Finix;
4+
5+
6+
class Pagination extends Resource
7+
{
8+
9+
public function __construct(Hal\Resource $halResource, $class)
10+
{
11+
parent::__construct($halResource->getState(), $halResource->getAllLinks());
12+
$resources = $halResource->getAllEmbeddedResources();
13+
$resources = reset($resources);
14+
$items = array();
15+
foreach ($resources as $name => $resource) {
16+
array_push($items, new $class($resource->getState(), $resource->getAllLinks()));
17+
}
18+
$this->state->items = $items;
19+
}
20+
}

src/Finix/Resource.php

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Finix\Http\Auth\BasicAuthentication;
55
use Finix\Http\JsonBody;
66
use Finix\Http\Request;
7+
use Finix\Resources\Verification;
78
use Finix\Utils\ArrayProxy;
89
use \stdClass;
910

@@ -15,7 +16,8 @@ abstract class Resource
1516
protected $state;
1617

1718
protected static $href;
18-
protected static $client;
19+
protected $client;
20+
// protected static $client;
1921
protected static $registry;
2022

2123
/**
@@ -24,10 +26,10 @@ abstract class Resource
2426
*/
2527
public static function getHrefSpec($resource = null)
2628
{
27-
if(is_null($resource)){
29+
if (is_null($resource)) {
2830
$resource = get_called_class();
2931
}
30-
if(!is_string($resource)) {
32+
if (!is_string($resource)) {
3133
$resource = get_class($resource);
3234
}
3335
return self::getRegistry()->getHrefSpecForResource($resource);
@@ -36,9 +38,9 @@ public static function getHrefSpec($resource = null)
3638
/**
3739
* @return Hal\Client
3840
*/
39-
public static function getClient()
41+
public function getClient()
4042
{
41-
return self::$client;
43+
return $this->client;
4244
}
4345

4446
/**
@@ -51,23 +53,18 @@ public static function getRegistry()
5153

5254
public static function init()
5355
{
54-
self::$client = new Hal\Client(
55-
Settings::$url_root,
56-
'/',
57-
null,
58-
new BasicAuthentication(Settings::$username, Settings::$password));
5956
self::$registry = new Registry();
6057
}
6158

6259
public function __construct(array $state = null, array $links = null)
6360
{
61+
$this->client = Bootstrap::createClient();
6462
$this->setResource(new Hal\Resource($state, $links));
6563
}
6664

6765
public function __get($name)
6866
{
69-
if($this->state->has_key($name))
70-
{
67+
if ($this->state->has_key($name)) {
7168
return $this->state[$name];
7269
}
7370

@@ -92,8 +89,8 @@ public function __set($name, $value)
9289
public function __isset($name)
9390
{
9491
if (array_key_exists($name, $this->resource->getAllLinks()) ||
95-
array_key_exists($name, $this->resource->getState()))
96-
{
92+
array_key_exists($name, $this->resource->getState())
93+
) {
9794
return true;
9895
}
9996

@@ -111,11 +108,28 @@ public function __isset($name)
111108
public static function retrieve($id)
112109
{
113110
$uri = self::getHrefSpec()->collection_uri . '/' . $id;
114-
$resource = self::getClient()->sendRequest(new Request($uri));
111+
$resource = Bootstrap::createClient()->sendRequest(new Request($uri));
115112
$class = get_called_class();
116113
return new $class($resource->getState(), $resource->getAllLinks());
117114
}
118115

116+
public static function getPagination($href)
117+
{
118+
$resource = Bootstrap::createClient()->sendRequest(new Request($href));
119+
return new Pagination($resource, get_called_class());
120+
}
121+
122+
public function refresh()
123+
{
124+
$request = new Request(
125+
$this->resource->getLink("self")->getHref(),
126+
'GET'
127+
);
128+
$resource = $this->getClient()->sendRequest($request);
129+
$this->setResource($resource);
130+
return $this;
131+
}
132+
119133
/**
120134
* @return \Finix\Resource
121135
* @throws Hal\Exception\HalClientErrorException
@@ -128,12 +142,11 @@ public static function retrieve($id)
128142
public function save()
129143
{
130144
if (empty($this->state["tags"])) {
131-
$this->state["tags"] = new stdClass();
145+
$this->state["tags"] = new stdClass();
132146
}
133147

134148
$payload = new JsonBody(iterator_to_array($this->state));
135-
if($this->isUpdate())
136-
{
149+
if ($this->isUpdate()) {
137150
$request = new Request(
138151
$this->resource->getLink("self")->getHref(),
139152
'PUT',
@@ -183,11 +196,12 @@ private function isUpdate()
183196

184197

185198
/**
199+
* @param string $rel
186200
* @return string
187201
*/
188-
public function getHref()
202+
public function getHref($rel = "self")
189203
{
190-
return $this->resource->getLink("self")->getHref();
204+
return $this->resource->getLink($rel)->getHref();
191205
}
192206

193207
/**
@@ -199,5 +213,9 @@ private function setResource($resource)
199213
$this->state = new ArrayProxy($resource->getState());
200214
}
201215

202-
203-
}
216+
public function verifyOn(Verification $verification)
217+
{
218+
$verifyLink = $this->resource->getLink("verifications")->getHref();
219+
return $verification->create($verifyLink);
220+
}
221+
}

src/Finix/Resources/Application.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,13 @@ public static function init()
1111
self::getRegistry()->add(get_called_class(), new HrefSpec('applications', 'id', '/'));
1212
}
1313

14-
}
14+
public function createPartnerUser(User $user)
15+
{
16+
return $user->create($this->resource->getLink("users")->getHref());
17+
}
18+
19+
public function createProcessor(Processor $processor)
20+
{
21+
return $processor->create($this->resource->getLink("processors")->getHref());
22+
}
23+
}

0 commit comments

Comments
 (0)