Skip to content

Commit 0d1ab3f

Browse files
Initial commit
1 parent f1a55c6 commit 0d1ab3f

20 files changed

+641
-0
lines changed

.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
nbproject
2+
*~
3+
._*
4+
.~lock.*
5+
*.log
6+
.buildpath
7+
.DS_Store
8+
.idea
9+
.project
10+
.settings
11+
vendor
12+
composer.lock
13+
examples/config.toml

composer.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"name": "mortgage-source/api",
3+
"description": "PHP Mortgage Source API Client",
4+
"type": "library",
5+
"license": "GPL-3.0-only",
6+
"homepage": "https://github.com/legalweb/mortgage-source-php-library/",
7+
"authors": [
8+
{
9+
"name": "Aaron Parker",
10+
"email": "[email protected]",
11+
"homepage": "http://www.legalweb.org.uk"
12+
}
13+
],
14+
"keywords": [
15+
"legalweb",
16+
"Mortgage",
17+
"Source",
18+
"API"
19+
],
20+
"require": {
21+
"php": ">=7.1",
22+
"ext-curl": "*",
23+
"ext-json": "*"
24+
},
25+
"require-dev": {
26+
"phpunit/phpunit": "~4.8",
27+
"friendsofphp/php-cs-fixer": "1.7.*",
28+
"ulrichsg/getopt-php": "^3.2",
29+
"yosymfony/toml": "^1.0"
30+
},
31+
"suggest": {
32+
33+
},
34+
"autoload": {
35+
"psr-4": {
36+
"Legalweb\\MortgageSourceClient\\": "src/"
37+
}
38+
},
39+
"autoload-dev": {
40+
"psr-4": {
41+
"Legalweb\\MortgageSourceClientTest\\": "tests/",
42+
"Legalweb\\MortgageSourceClientExample\\": "examples/"
43+
}
44+
}
45+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Legalweb\MortgageSourceClientExample\Lib\Commands;
4+
5+
use GetOpt\Command;
6+
use GetOpt\GetOpt;
7+
use GetOpt\Option;
8+
use Legalweb\MortgageSourceClient\MortgageSourceService;
9+
use Legalweb\MortgageSourceClient\Traits\Castable;
10+
use Legalweb\MortgageSourceClientExample\Lib\Traits\Configurable;
11+
12+
class GetClientToken extends Command {
13+
14+
use Castable;
15+
use Configurable;
16+
17+
public function __construct()
18+
{
19+
$options = [
20+
Option::create("u", "user", Getopt::REQUIRED_ARGUMENT)->setDescription("Specify user to act on behalf of"),
21+
];
22+
23+
parent::__construct('getclienttoken', [$this, 'handle'], $options);
24+
}
25+
26+
/**
27+
* @param GetOpt $opt
28+
*/
29+
public function handle(GetOpt $opt)
30+
{
31+
try {
32+
$c = $this->getConfig($opt);
33+
} catch (\Exception $exception) {
34+
trigger_error("Invalid configuration: " . $exception->getMessage());
35+
return;
36+
}
37+
38+
try {
39+
$cs = MortgageSourceService::NewMortgageSourceService($c, false, $opt->getOption("user"));
40+
$r = $cs->GetClientToken();
41+
42+
if ($r) {
43+
echo "\nToken: ", $r->Token, "\nVendor: ", $r->Vendor, "\nExpires: ", $r->Expires, "\n";
44+
} else {
45+
echo "\nNo token retrieved.\n";
46+
}
47+
} catch (\Exception $exception) {
48+
trigger_error("Unexpected error occurred: " . $exception->getMessage());
49+
}
50+
}
51+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace Legalweb\MortgageSourceClientExample\Lib\Traits;
4+
5+
use GetOpt\GetOpt;
6+
use Yosymfony\Toml\Toml;
7+
use Legalweb\MortgageSourceClient\Config;
8+
9+
trait Configurable {
10+
11+
/**
12+
* @param GetOpt $opt
13+
*
14+
* @return Config
15+
*/
16+
protected function getConfig(GetOpt $opt) {
17+
$c = new Config();
18+
19+
list($client, $secret, $endpoint, $verifyssl) = $this->getConfigOptions($opt);
20+
21+
$c->Name = "Example";
22+
$c->Client = $client;
23+
$c->Secret = $secret;
24+
$c->EndPoint = $endpoint;
25+
$c->VerifySSL = $verifyssl;
26+
27+
return $c;
28+
}
29+
30+
/**
31+
* @param GetOpt $opt
32+
*
33+
* @return array
34+
*/
35+
protected function getConfigOptions(GetOpt $opt) {
36+
$config = $opt->getOption('config');
37+
38+
$client = "";
39+
$secret = "";
40+
$endpoint = "";
41+
$verifyssl = false;
42+
43+
if (is_null($config)) {
44+
$client = $opt->getOption('client');
45+
$secret = $opt->getOption('secret');
46+
$endpoint = $opt->getOption('endpoint');
47+
$verifyssl = $opt->getOption('verifyssl');
48+
49+
if (!$client || !$secret || !$endpoint) {
50+
throw new \InvalidArgumentException("client, secret & endpoint must be set if not providing config file");
51+
}
52+
} else {
53+
$c = Toml::parseFile($config);
54+
55+
if (!$c['CLIENT'] || !$c['SECRET'] || !$c['ENDPOINT']) {
56+
throw new \InvalidArgumentException("client, secret & endpoint must be set if not providing config file");
57+
}
58+
59+
$client = $c['CLIENT'];
60+
$secret = $c['SECRET'];
61+
$endpoint = $c['ENDPOINT'];
62+
$verifyssl = $c['VERIFYSSL'];
63+
}
64+
65+
return [ $client, $secret, $endpoint, $verifyssl ];
66+
}
67+
68+
}

examples/cli.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace {
4+
require_once __DIR__ . '/../vendor/autoload.php';
5+
}
6+
7+
namespace {
8+
use GetOpt\GetOpt;
9+
use GetOpt\Option;
10+
use GetOpt\Command;
11+
use GetOpt\ArgumentException;
12+
use GetOpt\ArgumentException\Missing;
13+
use Legalweb\MortgageSourceClientExample\Lib\Commands\GetClientToken;
14+
15+
define('NAME', 'run');
16+
define('VERSION', '1.0-alpha');
17+
18+
$opt = new GetOpt();
19+
$opt->addOptions([
20+
Option::create(null, 'version', GetOpt::NO_ARGUMENT)->setDescription("Show version information and quit"),
21+
Option::create('?', 'help', GetOpt::NO_ARGUMENT)->setDescription("Show this help and quit"),
22+
Option::create('f', 'config', GetOpt::OPTIONAL_ARGUMENT)->setValidation('is_readable')->setDescription("Specify configuration file to use"),
23+
Option::create('c', 'client', GetOpt::OPTIONAL_ARGUMENT)->setDescription("Provide client login if no config file provided"),
24+
Option::create('s', 'secret', GetOpt::OPTIONAL_ARGUMENT)->setDescription("Provide secret if no config file provided"),
25+
Option::create('e', 'endpoint', GetOpt::OPTIONAL_ARGUMENT)->setDescription("Provide endpoint if no config file provided"),
26+
]);
27+
28+
$opt->addCommand(Command::create('test-setup', function() {
29+
echo "When you see this message the setup works." . PHP_EOL;
30+
})->setDescription("Check if setup works"));
31+
32+
$opt->addCommand(new GetClientToken());
33+
34+
try {
35+
try {
36+
$opt->process();
37+
} catch(Missing $exception) {
38+
if ($opt->getOption('help')) {
39+
throw $exception;
40+
}
41+
}
42+
} catch(ArgumentException $exception) {
43+
file_put_contents('php://stderr', $exception->getMessage() . PHP_EOL);
44+
echo PHP_EOL . $opt->getHelpText();
45+
exit;
46+
}
47+
48+
// show version and quit
49+
if ($opt->getOption('version')) {
50+
echo sprintf('%s: %s' . PHP_EOL, NAME, VERSION);
51+
exit;
52+
}
53+
54+
// show help and quit
55+
$command = $opt->getCommand();
56+
if (!$command || $opt->getOption('help')) {
57+
echo $opt->getHelpText();
58+
exit;
59+
}
60+
61+
// call the requested command
62+
call_user_func($command->getHandler(), $opt);
63+
}

examples/config.example.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CLIENT="API_PROVIDER_CLIENT_NAME"
2+
ENDPOINT="https://API_PROVIDER_HOSTNAME/API_PATH"
3+
SECRET="API_PROVIDER_SECRET_KEY"
4+
VERIFYSSL=true

src/ClientToken.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Legalweb\MortgageSourceClient;
4+
5+
use Legalweb\MortgageSourceClient\Traits\Castable;
6+
7+
class ClientToken
8+
{
9+
use Castable;
10+
11+
var $Expires;
12+
var $Token;
13+
var $Vendor;
14+
15+
/**
16+
* @param \stdClass $s
17+
*
18+
* @return ClientToken
19+
*/
20+
public static function FromStdClass(\stdClass $s)
21+
{
22+
$d = new ClientToken();
23+
24+
$d = self::Cast($s, $d);
25+
26+
return $d;
27+
}
28+
}

src/Config.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Legalweb\MortgageSourceClient;
4+
5+
class Config
6+
{
7+
var $Name;
8+
var $Client;
9+
var $Secret;
10+
var $EndPoint;
11+
var $VerifySSL;
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Legalweb\MortgageSourceClient\Exceptions;
4+
5+
/**
6+
* Class NotConfiguredException
7+
*/
8+
class APIRequestException extends \Exception {}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Legalweb\MortgageSourceClient\Exceptions;
4+
5+
/**
6+
* Class NotConfiguredException
7+
*/
8+
class APIUnavailableException extends \Exception {}

0 commit comments

Comments
 (0)