Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #12

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
],
"lint": [
"vendor/bin/parallel-lint --no-colors --exclude vendor .",
"vendor/bin/phpcs --ignore=data,vendor --standard=PSR2 ."
"vendor/bin/phpcs --ignore=data,vendor --standard=PSR12 ."
],
"phpstan": [
"vendor/bin/phpstan analyse --memory-limit=2G src"
Expand Down
29 changes: 29 additions & 0 deletions data/iterator/max_page_length.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

return [
[
[
'body' => json_encode(['token' => 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIyMTQ4MzAxZC04Y2MzLTQyNjgtODY5MC00MmI4M2MzNzU4NGUiLCJlbWFpbF92ZXJpZmllZCI6dHJ1ZSwiaXNzIjoiaHR0cHM6Ly9jb2duaXRvLWlkcC5ldS13ZXN0LTEuYW1hem9uYXdzLmNvbS8iLCJjb2duaXRvOnVzZXJuYW1lIjoiZXhhbXBsZUBleGFtcGxlLm9yZyIsImN1c3RvbTpjc1VzZXJJZCI6IjEyMzQ1NiIsImF1ZCI6IjEyMzQ1NiIsImN1c3RvbTpjc1VzZXJDb3VudHJ5Q29kZSI6IlVLIiwiZXZlbnRfaWQiOiIyMTQ4MzAxZC04Y2MzLTQyNjgtODY5MC00MmI4M2MzNzU4NGYiLCJ0b2tlbl91c2UiOiJpZCIsImF1dGhfdGltZSI6MTU0NDQzNjE4MiwiZXhwIjoxNTQ0NDM5OTU4LCJpYXQiOjE1NDQ0MzYxODIsImVtYWlsIjoiZXhhbXBsZUBleGFtcGxlLm9yZyIsImp0aSI6ImY1NTBhN2I4LTA2OTItNDk1Ny04ZGFiLTFhMjUzN2M3ZDU3MyJ9.xZ_aCeEEq9zweOzm8FqCIhddQ0dWi_c7XyfxOWchSPg']),
],
[
'body' => json_encode([
'totalSize' => 32000,
'companies' => [
[],
[],
],
]),
'code' => 200,
],
[
'body' => json_encode([
'totalSize' => 32000,
'companies' => [
[],
[],
],
]),
'code' => 200,
],
],
];
116 changes: 60 additions & 56 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@

use Lcobucci\JWT\Parser;
use Lcobucci\JWT\Token;
use GuzzleHttp\Psr7;

/**
* Client Class used to store data relating to the client created
* Client Class used to store data relating to the client created.
*/
class Client
{

protected $http_client;

/**
Expand Down Expand Up @@ -40,7 +38,8 @@ class Client
protected $monitor;

/**
* construct function that builds the client class
* construct function that builds the client class.
*
* @param array $config creditsafe configuration
*/
public function __construct(array $config = [])
Expand All @@ -58,18 +57,18 @@ public function __construct(array $config = [])
}

/**
* This function is used to authenticate a username and password
* @return void
* This function is used to authenticate a username and password.
*
* @throws Exception\Unauthorized if the provided authentication details are not valid
*/
public function authenticate() : void
public function authenticate(): void
{
try {
$authenticate = $this->http_client->request('POST', 'authenticate', [
'json'=> [
'username'=> $this->config['username'],
'password' => $this->config['password']
]
'json' => [
'username' => $this->config['username'],
'password' => $this->config['password'],
],
]);
} catch (\GuzzleHttp\Exception\ClientException $e) {
$message = 'There was a problem authenticating with the Creditsafe API';
Expand All @@ -87,31 +86,27 @@ public function authenticate() : void
}

/**
* set token
* set token.
*
* @param string $token Token must be set to have access
* to the api and is only valid for an hour
* @return void
* to the api and is only valid for an hour
*/
public function setToken(string $token) : void
public function setToken(string $token): void
{
$this->token = (new Parser())->parse($token);
}

/**
* @return Token
*/
public function getToken() : Token
public function getToken(): Token
{
return $this->token;
}

/**
* Checks if token is valid
* @return void
* Checks if token is valid.
*/
public function checkToken() : void
public function checkToken(): void
{
/**
/*
* argument required as of lcobucci/jwt 3.4+
* https://github.com/lcobucci/jwt/commit/2cfa548b3f26176ab3e56a1ff3503b4d0db7ae7c
* https://github.com/lcobucci/jwt/issues/550#issuecomment-733557709
Expand All @@ -122,20 +117,21 @@ public function checkToken() : void
}

/**
* This request function handles all requests for the api
* @param string $type Sets the type of HTTP Request e.g GET ,POST
* @param string $endpoint Sets the endpoint
* @param array $params Sets params for a endpoint
* @return array Returns the results of the endpoint
* This request function handles all requests for the api.
*
* @param string $type Sets the type of HTTP Request e.g GET ,POST
* @param string $endpoint Sets the endpoint
* @param array $params Sets params for a endpoint
*
* @return array Returns the results of the endpoint
*/
public function request(string $type, string $endpoint, array $params = []) : array
public function request(string $type, string $endpoint, array $params = []): array
{

$this->checkToken();

$guzzleArgs = [
$guzzleArgs = [
'headers' => [
'Authorization' => (string) $this->token
'Authorization' => (string) $this->token,
],
];

Expand Down Expand Up @@ -179,91 +175,99 @@ public function request(string $type, string $endpoint, array $params = []) : ar
throw $exception;
}

$res = (array)json_decode((string) $res->getBody(), true);
$res = (array) json_decode((string) $res->getBody(), true);

return $res;
}

/**
* A function that handles the creation of a GET Request
* @param string $endpoint An endpoint used to create an request
* @param array $params Sets params for a endpoint
* @return array Returns the results of the endpoint
* A function that handles the creation of a GET Request.
*
* @param string $endpoint An endpoint used to create an request
* @param array $params Sets params for a endpoint
*
* @return array Returns the results of the endpoint
*/
public function get(string $endpoint, array $params = []) : array
public function get(string $endpoint, array $params = []): array
{
return $this->request('GET', $endpoint, $params);
}

/**
* Get Company Events
* Get Company Events.
*
* @return Service\CompanyEventService Returns Company Events
*/
public function monitoring() : Service\CompanyEventService
public function monitoring(): Service\CompanyEventService
{
if (!isset($this->monitor)) {
$this->monitor = new Service\CompanyEventService($this);
}

return $this->monitor;
}

/**
* Get company services
* Get company services.
*
* @return Service\CompanyService Returns Company Services
*/
public function companies() : Service\CompanyService
public function companies(): Service\CompanyService
{
if (!isset($this->company)) {
$this->company = new Service\CompanyService($this);
}

return $this->company;
}

/**
* Get Countries
* @return Service\CountryService Returns the Countries
* Get Countries.
*
* @return Service\CountryService Returns the Countries
*/
public function countries() : Service\CountryService
public function countries(): Service\CountryService
{
if (!isset($this->countries)) {
$this->countries = new Service\CountryService($this);
}

return $this->countries;
}

/**
* @return array
*/
public function getDefaultConfig() : array
public function getDefaultConfig(): array
{
return [
'apiURI' => 'https://connect.creditsafe.com/',
];
}

/**
* Function gets the base url for the api by concatenating the API URL and the API version
* Function gets the base url for the api by concatenating the API URL and the API version.
*
* @return string Returns a string containing the base url
*/
public function getBaseURL() : string
public function getBaseURL(): string
{
return $this->getApiURL().$this->getApiVersion().'/';
return $this->getApiURL() . $this->getApiVersion() . '/';
}

/**
* Function gets the Api url
* Function gets the Api url.
*
* @return string Returns a string containing the api url
*/
protected function getApiURL() : string
protected function getApiURL(): string
{
return $this->config['apiURI'];
}

/**
* Function gets the Api Version
* Function gets the Api Version.
*
* @return string Returns a string containing the Api Version
*/
protected function getApiVersion() : string
protected function getApiVersion(): string
{
return 'v1';
}
Expand Down
12 changes: 7 additions & 5 deletions src/Exception/APIException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,28 @@
namespace SynergiTech\Creditsafe\Exception;

/**
* This class is used to handle API Expections
* This class is used to handle API Expections.
*/
class APIException extends \RuntimeException
{
protected $correlationID = null;

/**
* Sets the CorrelationID
* Sets the CorrelationID.
*
* @param string $correlationID Contains the CorrelationID
*/
public function setCorrelationID(string $correlationID) : void
public function setCorrelationID(string $correlationID): void
{
$this->correlationID = $correlationID;
}

/**
* Get CorrelationID
* Get CorrelationID.
*
* @return ?string Contains the CorrelationID
*/
public function getCorrelationID() : ?string
public function getCorrelationID(): ?string
{
return $this->correlationID;
}
Expand Down
Loading