Skip to content

Commit

Permalink
Added companies
Browse files Browse the repository at this point in the history
  • Loading branch information
thijskok committed Mar 1, 2021
1 parent fa92b25 commit 3715328
Show file tree
Hide file tree
Showing 12 changed files with 521 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ phpunit.xml
/build

/.idea

/src/Test.php
3 changes: 2 additions & 1 deletion .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setUsingCache(true)
->setCacheFile(__DIR__.'/.php_cs.cache')
->setRules(array(
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
Expand Down Expand Up @@ -31,4 +32,4 @@ return PhpCsFixer\Config::create()
PhpCsFixer\Finder::create()
->in(__DIR__)
)
;
;
92 changes: 92 additions & 0 deletions src/Actions/ManagesCompanies.php
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);
}
}
83 changes: 81 additions & 2 deletions src/Actions/ManagesPeople.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace TestMonitor\Custify\Actions;

use TestMonitor\Custify\Validator;
use TestMonitor\Custify\Resources\Person;
use TestMonitor\Custify\Transforms\TransformsPeople;
use TestMonitor\Custify\Exceptions\NotFoundException;

trait ManagesPeople
{
Expand All @@ -17,12 +20,88 @@ trait ManagesPeople
* @throws \TestMonitor\Custify\Exceptions\InvalidDataException
* @return \TestMonitor\Custify\Resources\Person[]
*/
public function people($page = 1, $limit = 10)
public function people($page = 1, $limit = 10): array
{
$response = $this->get('people/all', [
'query' => ['itemsPerPage' => $limit, 'page' => $page],
]);

return $this->fromCustifyPeople($response);
return $this->fromCustifyPeople($response[0]['people']);
}

/**
* Get a single person.
*
* @param string $id
*
* @throws \TestMonitor\Custify\Exceptions\InvalidDataException
* @return \TestMonitor\Custify\Resources\Person
*/
public function person(string $id): Person
{
$response = $this->get("people/{$id}");

return $this->fromCustifyPerson($response);
}

/**
* Get a single person by user id.
*
* @param string $userId
*
* @throws \TestMonitor\Custify\Exceptions\NotFoundException
* @throws \TestMonitor\Custify\Exceptions\InvalidDataException
* @return \TestMonitor\Custify\Resources\Person
*/
public function personByUserId(string $userId): Person
{
$response = $this->get("people?user_id={$userId}");

Validator::keysExists($response[0], ['people']);

// Simulate a not found response when the user_id does not exists.
if (empty($response[0]['people'])) {
throw new NotFoundException();
}

return $this->fromCustifyPerson($response[0]['people'][0]);
}

/**
* Get a single person by email.
*
* @param string $email
*
* @throws \TestMonitor\Custify\Exceptions\NotFoundException
* @throws \TestMonitor\Custify\Exceptions\InvalidDataException
* @return \TestMonitor\Custify\Resources\Person
*/
public function personByEmail(string $email): Person
{
$response = $this->get("people?email={$email}");

Validator::keysExists($response[0], ['people']);

// Simulate a not found response when the user_id does not exists.
if (empty($response[0]['people'])) {
throw new NotFoundException();
}

return $this->fromCustifyPerson($response[0]['people'][0]);
}

/**
* Create or update a person.
*
* @param \TestMonitor\Custify\Resources\Person $person
*
* @throws \TestMonitor\Custify\Exceptions\InvalidDataException
* @return \TestMonitor\Custify\Resources\Person
*/
public function createOrUpdatePerson(Person $person): Person
{
$response = $this->post('people', ['json' => $this->toCustifyPerson($person)]);

return $this->fromCustifyPerson($response);
}
}
3 changes: 2 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

class Client
{
use Actions\ManagesPeople;
use Actions\ManagesPeople,
Actions\ManagesCompanies;

/**
* @var string
Expand Down
6 changes: 3 additions & 3 deletions src/Resources/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class Company extends Resource
*/
public function __construct(array $attributes)
{
$this->id = $attributes['id'];
$this->company_id = $attributes['company_id'];
$this->name = $attributes['name'];
$this->id = $attributes['id'] ?? '';
$this->company_id = $attributes['company_id'] ?? '';
$this->name = $attributes['name'] ?? '';
}
}
56 changes: 56 additions & 0 deletions src/Resources/CustomAttributes.php
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;
}
}
28 changes: 27 additions & 1 deletion src/Resources/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,27 @@ class Person extends Resource
*/
public $email;

/**
* The name of the person.
*
* @var string
*/
public $name;

/**
* The companies related to this person.
*
* @var array
*/
public $companies;

/**
* The custom attributes for this person.
*
* @var \TestMonitor\Custify\Resources\CustomAttributes
*/
public $customAttributes;

/**
* Create a new resource instance.
*
Expand All @@ -33,7 +54,12 @@ class Person extends Resource
public function __construct(array $attributes)
{
$this->id = $attributes['id'] ?? '';
$this->user_id = $attributes['user_id'];
$this->user_id = $attributes['user_id'] ?? '';
$this->email = $attributes['email'];
$this->name = $attributes['name'] ?? '';

$this->customAttributes = $attributes['custom_attributes'] ?? new CustomAttributes();

$this->companies = $attributes['companies'] ?? [];
}
}
54 changes: 54 additions & 0 deletions src/Transforms/TransformsCompanies.php
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,
];
}
}

0 comments on commit 3715328

Please sign in to comment.