diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3a9875b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/vendor/ +composer.lock diff --git a/README.md b/README.md new file mode 100644 index 0000000..5e5b365 --- /dev/null +++ b/README.md @@ -0,0 +1,47 @@ +# Creditsafe API +``` +composer require synergitech/creditsafe-connect +``` + +## Usage + +### Setting up Client +```php +$config = [ + 'username' => 'username', + 'password' => 'password' +]; + +$creditsafe = new \SynergiTech\Creditsafe\Client($config); +``` + +### Access countries and their codes +```php +$creditsafe->countries()->access(); +``` + +### Search criteria using country code +```php + +$creditsafe->companies()->searchCriteria(['countries' => 'GB']); + + +``` +### Company search pagination +```php +$search = $creditsafe->companies()->search(['countries' => 'GB', 'name' => 'GOOGLE UK LIMITED']); +$search->setPageSize(100); +foreach ($search as $result) { + $company = $result->get(); +} +``` + +### Get company report +```php +$creditsafe->companies()->get('GB001-0-03977902'); +``` + +## Running tests +``` +vendor/bin/phpunit tests +``` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..f531a4f --- /dev/null +++ b/composer.json @@ -0,0 +1,25 @@ +{ + "name": "synergitech/creditsafe-connect", + "type": "library", + "require": { + "php": ">=7.1", + "guzzlehttp/guzzle": "^6.3", + "lcobucci/jwt": "^3.2.5" + }, + "license": "MIT", + "authors": [ + { + "name": "Sean Lane", + "email": "sean@synergitech.co.uk" + } + ], + "autoload": { + "psr-4": { + "SynergiTech\\Creditsafe\\" : "src/", + "SynergiTech\\Creditsafe\\Tests\\" : "tests/" + } + }, + "require-dev": { + "phpunit/phpunit": "^7.5" + } +} diff --git a/data/authorization/invalid_token.php b/data/authorization/invalid_token.php new file mode 100644 index 0000000..b8cc191 --- /dev/null +++ b/data/authorization/invalid_token.php @@ -0,0 +1,10 @@ + json_encode(['message' => 'authentication failed']), + 'code' => 400, + ], + ] +]; diff --git a/data/authorization/valid_token.php b/data/authorization/valid_token.php new file mode 100644 index 0000000..04c320b --- /dev/null +++ b/data/authorization/valid_token.php @@ -0,0 +1,9 @@ + json_encode(['token' => 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIyMTQ4MzAxZC04Y2MzLTQyNjgtODY5MC00MmI4M2MzNzU4NGUiLCJlbWFpbF92ZXJpZmllZCI6dHJ1ZSwiaXNzIjoiaHR0cHM6Ly9jb2duaXRvLWlkcC5ldS13ZXN0LTEuYW1hem9uYXdzLmNvbS8iLCJjb2duaXRvOnVzZXJuYW1lIjoiZXhhbXBsZUBleGFtcGxlLm9yZyIsImN1c3RvbTpjc1VzZXJJZCI6IjEyMzQ1NiIsImF1ZCI6IjEyMzQ1NiIsImN1c3RvbTpjc1VzZXJDb3VudHJ5Q29kZSI6IlVLIiwiZXZlbnRfaWQiOiIyMTQ4MzAxZC04Y2MzLTQyNjgtODY5MC00MmI4M2MzNzU4NGYiLCJ0b2tlbl91c2UiOiJpZCIsImF1dGhfdGltZSI6MTU0NDQzNjE4MiwiZXhwIjoxNTQ0NDM5OTU4LCJpYXQiOjE1NDQ0MzYxODIsImVtYWlsIjoiZXhhbXBsZUBleGFtcGxlLm9yZyIsImp0aSI6ImY1NTBhN2I4LTA2OTItNDk1Ny04ZGFiLTFhMjUzN2M3ZDU3MyJ9.xZ_aCeEEq9zweOzm8FqCIhddQ0dWi_c7XyfxOWchSPg']), + ], + ] +]; diff --git a/src/Client.php b/src/Client.php new file mode 100644 index 0000000..78557ad --- /dev/null +++ b/src/Client.php @@ -0,0 +1,234 @@ +config = array_merge($this->getDefaultConfig(), $config); + + if (isset($this->config['http_client'])) { + $this->http_client = $this->config['http_client']; + } else { + $this->http_client = new \GuzzleHttp\Client([ + 'base_uri' => $this->getBaseURL(), + ]); + } + } + + /** + * This function is used to authenticate a username and password + * @return void + * @throws Exception\Unauthorized if the provided authentication details are not valid + */ + public function authenticate() : void + { + try { + $authenticate = $this->http_client->request('POST', 'authenticate', [ + 'json'=> [ + 'username'=> $this->config['username'], + 'password' => $this->config['password'] + ] + ]); + } catch (\GuzzleHttp\Exception\ClientException $e) { + $message = 'There was a problem authenticating with the Creditsafe API'; + if ($e->hasResponse()) { + $body = $e->getResponse()->getBody(); + $details = json_decode($body, true); + if (json_last_error() === JSON_ERROR_NONE && isset($details['message'])) { + $message = $details['message']; + } + } + throw new Exception\Unauthorized($message, 400, $e); + } + $decode = json_decode((string) $authenticate->getBody(), true); + $this->setToken($decode['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 + */ + public function setToken(string $token) : void + { + $this->token = (new Parser())->parse($token); + } + + public function getToken() : Token + { + return $this->token; + } + + /** + * Checks if token is valid + * @return void + */ + public function checkToken() : void + { + if ($this->token === null || $this->token->isExpired()) { + $this->authenticate(); + } + } + + /** + * 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 + { + + $this->checkToken(); + + $guzzleArgs = [ + 'headers' => [ + 'Authorization' => (string) $this->token + ], + ]; + + if ($type == 'GET') { + $guzzleArgs['query'] = $params; + } else { + $guzzleArgs['json'] = $params; + } + + try { + $res = $this->http_client->request($type, $endpoint, $guzzleArgs); + } catch (\GuzzleHttp\Exception\ClientException $e) { + $message = $e->getMessage(); + $correlationID = null; + + if ($e->hasResponse()) { + $body = $e->getResponse()->getBody(); + $details = json_decode($body, true); + + if (json_last_error() === JSON_ERROR_NONE) { + $apiMessage = []; + if (isset($details['message'])) { + $apiMessage[] = $details['message']; + } + if (isset($details['details'])) { + $apiMessage[] = $details['details']; + } + if (!empty($apiMessage)) { + $message = implode(': ', $apiMessage); + } + if (isset($details['correlationId'])) { + $correlationID = $details['correlationId']; + } + } + } + + $exception = new Exception\APIException($message, 400, $e); + if ($correlationID) { + $exception->setCorrelationID($correlationID); + } + throw $exception; + } + + $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 + */ + public function get(string $endpoint, array $params = []) : array + { + return $this->request('GET', $endpoint, $params); + } + + /** + * Get Company Events + * @return Service\CompanyEventService Returns Company Events + */ + public function monitoring() : Service\CompanyEventService + { + if (!isset($this->monitor)) { + $this->monitor = new Service\CompanyEventService($this); + } + return $this->monitor; + } + + /** + * Get company services + * @return Service\CompanyService Returns Company Services + */ + 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 + */ + public function countries() : Service\CountryService + { + if (!isset($this->countries)) { + $this->countries = new Service\CountryService($this); + } + return $this->countries; + } + + 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 + * @return string Returns a string containing the base url + */ + public function getBaseURL() : string + { + return $this->getApiURL().$this->getApiVersion().'/'; + } + + /** + * Function gets the Api url + * @return string Returns a string containing the api url + */ + private function getApiURL() : string + { + return $this->config['apiURI']; + } + + /** + * Function gets the Api Version + * @return string Returns a string containing the Api Version + */ + private function getApiVersion() : string + { + return 'v1'; + } +} diff --git a/src/Exception/APIException.php b/src/Exception/APIException.php new file mode 100644 index 0000000..b86bc90 --- /dev/null +++ b/src/Exception/APIException.php @@ -0,0 +1,29 @@ +correlationID = $correlationID; + } + + /** + * Get CorrelationID + * @return ?string Contains the CorrelationID + */ + public function getCorrelationID() : ?string + { + return $this->correlationID; + } +} diff --git a/src/Exception/Unauthorized.php b/src/Exception/Unauthorized.php new file mode 100644 index 0000000..9df2aec --- /dev/null +++ b/src/Exception/Unauthorized.php @@ -0,0 +1,7 @@ +client = $client; + $this->targetClass = $targetClass; + $this->endpoint = $endpoint; + $this->params = $params; + $this->page(1); + } + + /** + * Set PageSize + * @param int $size Stores the PageSize + * @return self + */ + public function setPageSize(int $size) : self + { + $this->pageSize = $size; + $this->rewind(); + return $this; + } + + /** + * Get Params + * @return array Return Params + */ + private function getParams() : array + { + $pageParams = ['page' => $this->currentPagePos]; + if ($this->pageSize !== null) { + $pageParams['pageSize'] = $this->pageSize; + } + return array_merge($this->params, $pageParams); + } + + /** + * Get Page + * @param int $num Passes the page number + * @return array Returns the current Record Set + */ + public function page(int $num) : array + { + if ($num === $this->currentPagePos) { + return $this->currentRecordSet; + } + + $this->currentPagePos = $num; + $this->currentRecordPos = $this->pageSize * ($this->currentPagePos - 1); + $this->minPageRecord = $this->currentRecordPos; + $this->currentRecordSet = $this->fetchPage(); + $this->maxPageRecord = $this->currentRecordPos + $this->pageSize - 1; + + return $this->currentRecordSet; + } + + /** + * fetch Page + * @return array Returns the results for a page + */ + private function fetchPage() : array + { + $results = []; + $resultSet = $this->client->get($this->endpoint, $this->getParams()); + foreach ($resultSet['companies'] as $company) { + $results[] = new $this->targetClass($this->client, $company); + } + + return $results; + } + + /** + * Rewind to the first page + * @return void + */ + public function rewind() : void + { + $this->page(1); + } + + /** + * Get the current page + * @return array + */ + public function current() + { + return $this->currentRecordSet[$this->key()]; + } + + /** + * Get the next page + * @return void + */ + public function next() : void + { + $this->currentRecordPos++; + if ($this->currentRecordPos > $this->maxPageRecord) { + $this->page($this->currentPagePos+1); + } + } + + /** + * Get the key of a position + * @return int + */ + public function key() : int + { + return $this->currentRecordPos - ($this->pageSize * ($this->currentPagePos-1)); + } + + /** + * Check if a position is valid + * @return bool + */ + public function valid() : bool + { + return !($this->key() >= count($this->currentRecordSet) && count($this->currentRecordSet) != $this->pageSize); + } +} diff --git a/src/Models/Company.php b/src/Models/Company.php new file mode 100644 index 0000000..eb35607 --- /dev/null +++ b/src/Models/Company.php @@ -0,0 +1,240 @@ +client = $client; + $this->companyID = $companyDetails['report']['companyId']; + $this->businessName = $companyDetails['report']['companyIdentification']['basicInformation']['businessName'] ?? null; + $this->registeredCompanyName = $companyDetails['report']['companyIdentification']['basicInformation']['registeredCompanyName'] ?? null; + $this->companyRegistrationNumber = $companyDetails['report']['companyIdentification']['basicInformation']['companyRegistrationNumber'] ?? null; + $this->country = $companyDetails['report']['companyIdentification']['basicInformation']['country'] ?? null; + $this->companyRegistrationDate = null; + if (isset($companyDetails['report']['companyIdentification']['basicInformation']['companyRegistrationDate'])) { + $this->companyRegistrationDate = new \DateTime($companyDetails['report']['companyIdentification']['basicInformation']['companyRegistrationDate']); + } + + $this->mainAddress = $companyDetails['report']['contactInformation']['mainAddress'] ?? []; + $this->otherAddresses = $companyDetails['report']['contactInformation']['otherAddresses'] ?? []; + + $this->currentDirectors = array_map(function ($director) { + return new Company\Director($this, $director, true); + }, $companyDetails['report']['directors']['currentDirectors'] ?? []); + + $this->previousDirectors = array_map(function ($director) { + return new Company\Director($this, $director, false); + }, $companyDetails['report']['directors']['previousDirectors'] ?? []); + + $this->financialStatements = array_map(function ($statement) { + return new Company\FinancialStatement($this, $statement); + }, $companyDetails['report']['financialStatements'] ?? []); + + $this->history = array_map(function ($history) { + $history['date'] = new \DateTime($history['date']); + return $history; + }, $companyDetails['report']['additionalInformation']['companyHistory'] ?? []); + + $this->commentaries = $companyDetails['report']['additionalInformation']['commentaries'] ?? []; + $this->creditScore = new Company\CreditScore($this, $companyDetails['report']['creditScore'] ?? []); + $this->mortgageSummary = $companyDetails['report']['additionalInformation']['mortgageSummary'] ?? []; + $this->mortgages = $companyDetails['report']['additionalInformation']['mortgageDetails'] ?? null; + $this->negativeInfo = $companyDetails['report']['negativeInformation'] ?? []; + $this->rawDetails = $companyDetails; + } + + /** + * + * @return string Return Company ID + */ + public function getCompanyID() : string + { + return $this->companyID; + } + + /** + * + * @return string Return Business Name + */ + public function getBusinessName() : string + { + return $this->businessName; + } + + /** + * + * @return string Return Registered Company Name + */ + public function getRegisteredCompanyName() : string + { + return $this->registeredCompanyName; + } + + /** + * + * @return string Return Company Registeration Number + */ + public function getCompanyRegistrationNumber() : string + { + return $this->companyRegistrationNumber; + } + + /** + * + * @return string Return Country + */ + public function getCountry() : string + { + return $this->country; + } + + /** + * + * @return DateTime Return Company Registration Date + */ + public function getCompanyRegistrationDate() : \DateTime + { + return new \DateTime($this->companyRegistrationDate); + } + + /** + * + * @return array Return Main Address + */ + public function getMainAddress() : array + { + return $this->mainAddress; + } + + /** + * + * @return array Return otherAddresses + */ + public function getOtherAddresses() : array + { + return $this->otherAddresses; + } + + /** + * + * @return array Return an array of current directors + */ + public function getCurrentDirectors() : ?array + { + return $this->currentDirectors; + } + + /** + * + * @return array Return an array of previous directors + */ + public function getPreviousDirectors() : array + { + return $this->previousDirectors; + } + + /** + * + * @return array Return an array of the whole company request + */ + public function getRawDetails() : array + { + return $this->rawDetails; + } + + /** + * + * @return CreditScore Returns the CreditScore of the company + */ + public function getCreditScore() : CreditScore + { + return $this->creditScore; + } + + /** + * + * @return array Returns the commentaries of the company + */ + public function getCommentaries() : array + { + return $this->commentaries; + } + + /** + * + * @return array Return the history of the company + */ + public function getHistory() : array + { + return $this->history; + } + + /** + * + * @return array Return the mortgages summary + */ + public function getMortgageSummary() : array + { + return $this->mortgageSummary; + } + + /** + * + * @return array Return all company mortgages + */ + public function getMortgages() : ?array + { + return $this->mortgages; + } + + /** + * + * @return array Get Negative Information + */ + public function getNegativeInformation() : array + { + return $this->negativeInfo; + } + + /** + * Gets financialStatements + * @return array Returns an array of financial statements + */ + public function getFinancialStatements() : array + { + return $this->financialStatements; + } +} diff --git a/src/Models/Company/CreditScore.php b/src/Models/Company/CreditScore.php new file mode 100644 index 0000000..313f2ef --- /dev/null +++ b/src/Models/Company/CreditScore.php @@ -0,0 +1,70 @@ +company = $company; + $this->currentCreditRating = $creditScore['currentCreditRating'] ?? null; + $this->currentContractLimit = $creditScore['currentContractLimit'] ?? null; + $this->previousCreditRating = $creditScore['previousCreditRating'] ?? null; + $this->latestRatingChangeDate = null; + if (isset($creditScore['latestRatingChangeDate'])) { + $this->latestRatingChangeDate = new \DateTime($creditScore['latestRatingChangeDate']); + } + } + + /** + * + * @return array Returns the currentCreditRating data as an array + */ + public function getCurrentCreditRating() : ?array + { + return $this->currentCreditRating; + } + + /** + * + * @return array Returns the currentContractLimit data as an array + */ + public function getCurrentContractLimit() : ?array + { + return $this->currentContractLimit; + } + + /** + * + * @return array Returns the previousCreditRating data as an array + */ + public function getPreviousCreditRating() : ?array + { + return $this->previousCreditRating; + } + + /** + * + * @return \DateTime Returns the LastestRatingChangeDate in DateTime + */ + public function getLatestRatingChangeDate() : ?\DateTime + { + return $this->latestRatingChangeDate; + } +} diff --git a/src/Models/Company/Director.php b/src/Models/Company/Director.php new file mode 100644 index 0000000..04b3678 --- /dev/null +++ b/src/Models/Company/Director.php @@ -0,0 +1,112 @@ +company = $company; + $this->directorDetails = $directorDetails; + if (isset($this->directorDetails['dateOfBirth'])) { + $this->directorDetails['dateOfBirth'] = new \DateTime($this->directorDetails['dateOfBirth']); + } + + $this->directorDetails['positions'] = array_map(function ($position) { + //Add if statement to check if position exists + if (isset($position['dateAppointed'])) { + //Can't assign datetime due to previousDirectors not having dateAppointed + $position['dateAppointed'] = new \DateTime($position['dateAppointed']); + } + return $position; + }, $this->directorDetails['positions']); + $this->current = $current; + } + + /** + * Checks if the director is a current director + * @return boolean Returns if the current variable is true + */ + public function isCurrent() : bool + { + return $this->current === true; + } + + /** + * Checks if the director is a previous director + * @return boolean Return if the previous variable is not true + */ + public function isPrevious() : bool + { + return $this->current !== true; + } + + /** + * + * @return string Returns the Directors ID + */ + public function getID() : string + { + return $this->directorDetails['id']; + } + + /** + * + * @return string Returns the gender of the Director + */ + public function getGender() : string + { + return $this->directorDetails['gender']; + } + + /** + * + * @return DateTime Returns the Date Of Birth of the Director + */ + public function getDateOfBirth() : \DateTime + { + return $this->directorDetails['dateOfBirth']; + } + + /** + * + * @return array Returns an array containing the position and the date appointed + */ + public function getPositions() : array + { + return $this->directorDetails['positions']; + } + + /** + * + * @return string Returns the directors name + */ + public function getName() : string + { + return $this->directorDetails['name']; + } + + /** + * + * @return array Returns the directors address in an array + */ + public function getAddress() : array + { + return $this->directorDetails['address']; + } +} diff --git a/src/Models/Company/FinancialStatement.php b/src/Models/Company/FinancialStatement.php new file mode 100644 index 0000000..0e00a97 --- /dev/null +++ b/src/Models/Company/FinancialStatement.php @@ -0,0 +1,125 @@ +company = $company; + $this->statementDetails = $statementDetails; + $this->statementDetails['yearEndDate'] = new \DateTime($this->statementDetails['yearEndDate']); + } + + /** + * + * @return array Returns an array of profile and loss variables + */ + public function getProfitAndLoss() : array + { + return $this->statementDetails['profitAndLoss']; + } + + /** + * + * @return array Returns an array of variables relating to a balanceSheet + */ + public function getBalanceSheet() : array + { + return $this->statementDetails['balanceSheet']; + } + + /** + * @return string Return contingentLiabilities + */ + public function getContingentLiabilities() : string + { + return $this->statementDetails['otherFinancials']['contingentLiabilities']; + } + + /** + * + * @return float Return workingCapital + */ + public function getWorkingCapital() : float + { + return $this->statementDetails['otherFinancials']['workingCapital']; + } + + /** + * + * @return float Return net worth + */ + public function getNetWorth() : float + { + return $this->statementDetails['otherFinancials']['netWorth']; + } + + /** + * + * @return array Get Ratio statistics like currentratio , currentdebtratio etc + */ + public function getRatios() : array + { + return $this->statementDetails['ratios']; + } + + /** + * + * @return \DateTime Get the Financial Year End Date + */ + public function getYearEndDate() : \DateTime + { + return $this->statementDetails['yearEndDate']; + } + + /** + * + * @return int Gets the number of accountable weeks + */ + public function getNumberOfWeeks() : int + { + return $this->statementDetails['numberOfWeeks']; + } + + /** + * + * @return string Returns the Currency as a string + */ + public function getCurrency() : string + { + return $this->statementDetails['currency']; + } + + /** + * + * @return bool Returns true or false if consolidated Accounts + */ + public function getConsolidatedAccounts() : bool + { + return $this->statementDetails['consolidatedAccounts']; + } + + /** + * + * @return string Return Type + */ + public function getType() : string + { + return $this->statementDetails['type']; + } +} diff --git a/src/Models/CompanySearchResult.php b/src/Models/CompanySearchResult.php new file mode 100644 index 0000000..396a9ea --- /dev/null +++ b/src/Models/CompanySearchResult.php @@ -0,0 +1,129 @@ +client = $client; + $this->id = $companyDetails['id']; + $this->country = $companyDetails['country'] ?? null; + $this->regNo = $companyDetails['regNo'] ?? null; + $this->safeNo = $companyDetails['safeNo'] ?? null; + $this->name = $companyDetails['name'] ?? null; + $this->address = $companyDetails['address'] ?? null; + $this->status = $companyDetails['status'] ?? null; + $this->type = $companyDetails['type'] ?? null; + $this->dateOfLatestChange = $companyDetails['dateOfLatestChange'] ?? null; + } + + /** + * + * @return string Get ID + */ + public function getID() : string + { + return $this->id; + } + + /** + * Get Country + * @return string Get Country + */ + public function getCountry() : string + { + return $this->country; + } + + /** + * + * @return string Get Reference Number + */ + public function getRefNo() : string + { + return $this->regNo; + } + + /** + * + * @return string Get Safe Number + */ + public function getSafeNo() : string + { + return $this->safeNo; + } + + /** + * + * @return string Get Company Name + */ + public function getName() : string + { + return $this->name; + } + + /** + * + * @return array Get Address + */ + public function getAddress() : array + { + return $this->address; + } + + /** + * + * @return string Get Status + */ + public function getStatus() : string + { + return $this->status; + } + + /** + * + * @return string Get Type + */ + public function getType() : string + { + return $this->type; + } + + /** + * + * @return DateTime Get Date Of Latest Change + */ + public function getDateOfLatestChange() : \DateTime + { + return new \DateTime($this->dateOfLatestChange); + } + /** + * Gets the company from a company searches result + * @return Company Returns the Company Report + */ + public function get() : Company + { + return $this->client->companies()->get($this->getID()); + } +} diff --git a/src/Service/CompanyEventService.php b/src/Service/CompanyEventService.php new file mode 100644 index 0000000..a14c14c --- /dev/null +++ b/src/Service/CompanyEventService.php @@ -0,0 +1,34 @@ +client = $client; + } + + /** + * This function gets the companies Events endpoint + * @param string $id The ID of the company events we are searching for + * @param array $params Params for the endpoint + * @return array Returns the results of the endpoint + */ + public function companyEvents(string $id, array $params) : array + { + return $this->client->get('monitoring/companies/'.$id.'/events', $params); + } +} diff --git a/src/Service/CompanyService.php b/src/Service/CompanyService.php new file mode 100644 index 0000000..8388d37 --- /dev/null +++ b/src/Service/CompanyService.php @@ -0,0 +1,57 @@ +client = $client; + } + + /** + * This function is used to call the endpoint that searchs for companies + * @param array $params Contains params that can be passed to the endpoint + * @return array Returns the results of the search endpoint + */ + public function search(array $params) : ListResult + { + $list = new ListResult($this->client, CompanySearchResult::class, 'companies', $params); + return $list; + } + + /** + * This function is used to call the endpoint that gets the company report + * @param string $id The ID of the given company that you want to get a report for + * @return array Returns the results of the get endpoint + */ + public function get(string $id) : Company + { + return new Company($this->client, $this->client->get('companies/'.$id)); + } + + /** + * This function is used to call the endpoint that gets the search Criteria + * for companies based on the country code given + * @param array $params Contains params that can be passed to the endpoint + * @return array Returns the results of the searchCriteria endpoint + */ + public function searchCriteria(array $params) :array + { + return $this->client->get('companies/searchcriteria', $params); + } +} diff --git a/src/Service/CountryService.php b/src/Service/CountryService.php new file mode 100644 index 0000000..8e47a30 --- /dev/null +++ b/src/Service/CountryService.php @@ -0,0 +1,32 @@ +client = $client; + } + + /** + * This access function is called by the client to get the access countries endpoint + * @return array Returns the results of the endpoint in an array + */ + public function access() + { + return $this->client->get('access/countries', []); + } +} diff --git a/tests/Base.php b/tests/Base.php new file mode 100644 index 0000000..9ec004a --- /dev/null +++ b/tests/Base.php @@ -0,0 +1,36 @@ + $handler]), + ]; + }, $data); + } +} diff --git a/tests/ClientTest.php b/tests/ClientTest.php new file mode 100644 index 0000000..3a88474 --- /dev/null +++ b/tests/ClientTest.php @@ -0,0 +1,52 @@ + $guzzle, + 'username' => '', + 'password' => '', + ]); + $client->authenticate(); + + $this->assertNotNull($client->getToken()); + $this->assertSame('example@example.org', $client->getToken()->getClaim('email')); + } + + public function providerAuthorizationToken() + { + return $this->dataToGuzzleMock(require 'data/authorization/valid_token.php'); + } + + /** + * @dataProvider providerInvalidAuthorizationToken + */ + public function testAuthorizationError($guzzle) + { + $client = new Client([ + 'http_client' => $guzzle, + 'username' => '', + 'password' => '', + ]); + + $this->expectException(\SynergiTech\Creditsafe\Exception\Unauthorized::class); + $this->expectExceptionCode(400); + $this->expectExceptionMessage('authentication failed'); + + $client->authenticate(); + } + + public function providerInvalidAuthorizationToken() + { + return $this->dataToGuzzleMock(require 'data/authorization/invalid_token.php'); + } +}