-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
521 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,5 @@ phpunit.xml | |
/build | ||
|
||
/.idea | ||
|
||
/src/Test.php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
namespace TestMonitor\Custify\Actions; | ||
|
||
use TestMonitor\Custify\Validator; | ||
use TestMonitor\Custify\Resources\Company; | ||
use TestMonitor\Custify\Exceptions\NotFoundException; | ||
use TestMonitor\Custify\Transforms\TransformsCompanies; | ||
|
||
trait ManagesCompanies | ||
{ | ||
use TransformsCompanies; | ||
|
||
/** | ||
* Get a list of of companies. | ||
* | ||
* @param int $page | ||
* @param int $limit | ||
* | ||
* @throws \TestMonitor\Custify\Exceptions\InvalidDataException | ||
* @return \TestMonitor\Custify\Resources\Company[] | ||
*/ | ||
public function companies($page = 1, $limit = 10): array | ||
{ | ||
$response = $this->get('company/all', [ | ||
'query' => ['itemsPerPage' => $limit, 'page' => $page], | ||
]); | ||
|
||
return $this->fromCustifyCompanies($response['companies']); | ||
} | ||
|
||
/** | ||
* Get a single company. | ||
* | ||
* @param string $id | ||
* | ||
* @throws \TestMonitor\Custify\Exceptions\NotFoundException | ||
* @throws \TestMonitor\Custify\Exceptions\InvalidDataException | ||
* @return \TestMonitor\Custify\Resources\Company | ||
*/ | ||
public function company(string $id): Company | ||
{ | ||
$response = $this->get("company/{$id}"); | ||
|
||
Validator::keysExists($response, ['companies']); | ||
|
||
// Simulate a not found response when the user_id does not exists. | ||
if (empty($response['companies'])) { | ||
throw new NotFoundException(); | ||
} | ||
|
||
return $this->fromCustifyCompany($response['companies'][0]); | ||
} | ||
|
||
/** | ||
* Get a single company by company id. | ||
* | ||
* @param string $companyId | ||
* | ||
* @throws \TestMonitor\Custify\Exceptions\InvalidDataException | ||
* @throws \TestMonitor\Custify\Exceptions\NotFoundException | ||
* @return \TestMonitor\Custify\Resources\Company | ||
*/ | ||
public function companyByCompanyId(string $companyId): Company | ||
{ | ||
$response = $this->get("company?company_id={$companyId}"); | ||
|
||
Validator::keysExists($response, ['companies']); | ||
|
||
// Simulate a not found response when the user_id does not exists. | ||
if (empty($response['companies'])) { | ||
throw new NotFoundException(); | ||
} | ||
|
||
return $this->fromCustifyCompany($response['companies'][0]); | ||
} | ||
|
||
/** | ||
* Create or update a company. | ||
* | ||
* @param \TestMonitor\Custify\Resources\Company $company | ||
* | ||
*@throws \TestMonitor\Custify\Exceptions\InvalidDataException | ||
* @return \TestMonitor\Custify\Resources\Company | ||
*/ | ||
public function createOrUpdateCompany(Company $company): Company | ||
{ | ||
$response = $this->post('company', ['json' => $this->toCustifyCompany($company)]); | ||
|
||
return $this->fromCustifyCompany($response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace TestMonitor\Custify\Resources; | ||
|
||
class CustomAttributes extends Resource | ||
{ | ||
/** | ||
* The attributes. | ||
* | ||
* @var array | ||
*/ | ||
public $attributes; | ||
|
||
/** | ||
* Create a new resource instance. | ||
* | ||
* @param array $attributes | ||
*/ | ||
public function __construct($attributes = []) | ||
{ | ||
$this->attributes = $attributes; | ||
} | ||
|
||
/** | ||
* Gets a custom attribute. | ||
* | ||
* @param $name | ||
* | ||
* @return mixed|null | ||
*/ | ||
public function __get($name) | ||
{ | ||
return $this->attributes[$name] ?? null; | ||
} | ||
|
||
/** | ||
* Sets a custom attribute. | ||
* | ||
* @param string $name | ||
* @param mixed $value | ||
*/ | ||
public function __set($name, $value) | ||
{ | ||
$this->attributes[$name] = $value; | ||
} | ||
|
||
/** | ||
* Returns the custom attributes as a key/value array. | ||
* | ||
* @return array | ||
*/ | ||
public function toArray(): array | ||
{ | ||
return $this->attributes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace TestMonitor\Custify\Transforms; | ||
|
||
use TestMonitor\Custify\Validator; | ||
use TestMonitor\Custify\Resources\Company; | ||
|
||
trait TransformsCompanies | ||
{ | ||
/** | ||
* @param array $companies | ||
* | ||
* @throws \TestMonitor\Custify\Exceptions\InvalidDataException | ||
* @return \TestMonitor\Custify\Resources\Company[] | ||
*/ | ||
protected function fromCustifyCompanies($companies): array | ||
{ | ||
Validator::isArray($companies); | ||
|
||
return array_map(function ($company) { | ||
return $this->fromCustifyCompany($company); | ||
}, $companies); | ||
} | ||
|
||
/** | ||
* @param array $company | ||
* | ||
* @throws \TestMonitor\Custify\Exceptions\InvalidDataException | ||
* @return \TestMonitor\Custify\Resources\Company | ||
*/ | ||
protected function fromCustifyCompany($company): Company | ||
{ | ||
Validator::keysExists($company, ['id', 'company_id']); | ||
|
||
return new Company([ | ||
'id' => $company['id'], | ||
'company_id' => $company['company_id'], | ||
'name' => $company['name'], | ||
]); | ||
} | ||
|
||
/** | ||
* @param Company $company | ||
* | ||
* @return array | ||
*/ | ||
protected function toCustifyCompany(Company $company): array | ||
{ | ||
return [ | ||
'company_id' => $company->company_id, | ||
'name' => $company->name, | ||
]; | ||
} | ||
} |
Oops, something went wrong.