Skip to content

Commit

Permalink
Merge pull request #48 from 5pm-HDH/feature/refactor-request-builder
Browse files Browse the repository at this point in the history
Refactor RequestBuilder
  • Loading branch information
DumbergerL authored Aug 20, 2021
2 parents 16c8b63 + abe268f commit 429af85
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 237 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Group: [add Group-API](https://github.com/5pm-HDH/churchtools-api/pull/47)

### Changed
- Refactoring: [refactor Code, introduce namespace to test-suites, update phpunit-configuration](https://github.com/5pm-HDH/churchtools-api/pull/46)
- Refactoring:
- [introduce namespace to test-suites, update phpunit-configuration](https://github.com/5pm-HDH/churchtools-api/pull/46)
- [create AbstractRequestBuilder to simplify RequestBuilder implementation](https://github.com/5pm-HDH/churchtools-api/pull/48)
### Fixed


Expand Down
68 changes: 68 additions & 0 deletions src/Requests/AbstractRequestBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php


namespace CTApi\Requests;


use CTApi\CTClient;
use CTApi\Exceptions\CTModelException;
use CTApi\Models\AbstractModel;
use CTApi\Requests\Traits\OrderByCondition;
use CTApi\Requests\Traits\Pagination;
use CTApi\Requests\Traits\WhereCondition;
use CTApi\Utils\CTResponseUtil;
use GuzzleHttp\Exception\GuzzleException;

abstract class AbstractRequestBuilder
{

use Pagination, WhereCondition, OrderByCondition;

public function all(): array
{
$data = $this->collectDataFromPages($this->getApiEndpoint(), []);
return $this->getModelClass()::createModelsFromArray($data);
}

public function findOrFail(int $id)
{
$model = $this->find($id);
if ($model != null) {
return $model;
} else {
throw new CTModelException("Could not retrieve model!");
}
}

public function find(int $id)
{
$modelData = null;
try {
$response = CTClient::getClient()->get($this->getApiEndpoint() . '/' . $id);
$modelData = CTResponseUtil::dataAsArray($response);
} catch (GuzzleException $e) {
// ignore
}

if (empty($modelData)) {
return null;
} else {
return $this->getModelClass()::createModelFromData($modelData);
}
}

public function get(): array
{
$options = [];
$this->addWhereConditionsToOption($options);

$data = $this->collectDataFromPages($this->getApiEndpoint(), $options);

$this->orderRawData($data);

return $this->getModelClass()::createModelsFromArray($data);
}

abstract protected function getApiEndpoint(): string;
abstract protected function getModelClass(): string;
}
53 changes: 5 additions & 48 deletions src/Requests/EventRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,18 @@
namespace CTApi\Requests;


use CTApi\CTClient;
use CTApi\Exceptions\CTModelException;
use CTApi\Models\Event;
use CTApi\Requests\Traits\OrderByCondition;
use CTApi\Requests\Traits\Pagination;
use CTApi\Requests\Traits\WhereCondition;
use CTApi\Utils\CTResponseUtil;
use GuzzleHttp\Exception\GuzzleException;

class EventRequestBuilder
class EventRequestBuilder extends AbstractRequestBuilder
{
use Pagination, WhereCondition, OrderByCondition;

public function all(): array
protected function getApiEndpoint(): string
{
$data = $this->collectDataFromPages('/api/events', []);
return Event::createModelsFromArray($data);
return '/api/events';
}

public function findOrFail(int $id): Event
protected function getModelClass(): string
{
$event = $this->find($id);
if ($event != null) {
return $event;
} else {
throw new CTModelException("Could not retrieve model!");
}
}

public function find(int $id): ?Event
{
$eventData = null;
try {
$response = CTClient::getClient()->get('/api/events/' . $id);
$eventData = CTResponseUtil::dataAsArray($response);
} catch (GuzzleException $e) {
// ignore
}

if (empty($eventData)) {
return null;
} else {
return Event::createModelFromData($eventData);
}
}

public function get(): array
{
$options = [];
$this->addWhereConditionsToOption($options);

$data = $this->collectDataFromPages('/api/events', $options);

$this->orderRawData($data);

return Event::createModelsFromArray($data);
return Event::class;
}
}
46 changes: 5 additions & 41 deletions src/Requests/GroupRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,16 @@
use CTApi\Utils\CTResponseUtil;
use GuzzleHttp\Exception\GuzzleException;

class GroupRequestBuilder
class GroupRequestBuilder extends AbstractRequestBuilder
{
use Pagination, WhereCondition, OrderByCondition;

public function all(): array
protected function getApiEndpoint(): string
{
$data = $this->collectDataFromPages('/api/groups', []);
return Group::createModelsFromArray($data);
return '/api/groups';
}

public function findOrFail(int $id): Group
protected function getModelClass(): string
{
$group = $this->find($id);
if($group != null){
return $group;
}else{
throw new CTModelException("Could not retrieve model!");
}
}

public function find(int $id): ?Group
{
$groupData = null;
try{
$response = CTClient::getClient()->get('/api/groups/'.$id);
$groupData = CTResponseUtil::dataAsArray($response);
} catch(GuzzleException $e){
// ignore
}

if(empty($groupData)){
return null;
} else {
return Group::createModelFromData($groupData);
}
}

public function get(): array
{
$options = [];
$this->addWhereConditionsToOption($options);

$data = $this->collectDataFromPages('/api/groups', $options);

$this->orderRawData($data);

return Event::createModelsFromArray($data);
return Group::class;
}
}
49 changes: 12 additions & 37 deletions src/Requests/PersonRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
use CTApi\Utils\CTResponseUtil;
use GuzzleHttp\Exception\GuzzleException;

class PersonRequestBuilder
class PersonRequestBuilder extends AbstractRequestBuilder
{
use Pagination, WhereCondition, OrderByCondition;

public function whoami(): Person
{
$client = CTClient::getClient();
Expand All @@ -32,39 +30,6 @@ public function whoami(): Person
}
}

public function all(): array
{
$data = $this->collectDataFromPages('/api/persons', []);
return Person::createModelsFromArray($data);
}

public function findOrFail(int $id): Person
{
$person = $this->find($id);
if ($person != null) {
return $person;
} else {
throw new CTRequestException("Person could not be found!");
}
}

public function find(int $id): ?Person
{
$personData = null;
try {
$response = CTClient::getClient()->get('/api/persons/' . $id);
$personData = CTResponseUtil::dataAsArray($response);
} catch (GuzzleException $e) {
//ignore
}

if (empty($personData)) {
return null;
} else {
return Person::createModelFromData($personData);
}
}

public function get(): array
{
$options = [
Expand All @@ -74,10 +39,20 @@ public function get(): array
//Where-Clauses
$this->addWhereConditionsToOption($options);

$data = $this->collectDataFromPages('/api/persons', $options);
$data = $this->collectDataFromPages($this->getApiEndpoint(), $options);

$this->orderRawData($data);

return Person::createModelsFromArray($data);
}

protected function getApiEndpoint(): string
{
return '/api/persons';
}

protected function getModelClass(): string
{
return Person::class;
}
}
33 changes: 5 additions & 28 deletions src/Requests/ServiceGroupRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,16 @@
use CTApi\Utils\CTResponseUtil;
use GuzzleHttp\Exception\GuzzleException;

class ServiceGroupRequestBuilder
class ServiceGroupRequestBuilder extends AbstractRequestBuilder
{
use Pagination;

public function all(): array
protected function getApiEndpoint(): string
{
$data = $this->collectDataFromPages('/api/servicegroups', []);
return ServiceGroup::createModelsFromArray($data);
return '/api/servicegroups';
}

public function findOrFail(int $id): ServiceGroup
protected function getModelClass(): string
{
$serviceGroup = $this->find($id);
if ($serviceGroup != null) {
return $serviceGroup;
} else {
throw new CTRequestException("ServiceGroup could not be found!");
}
}

public function find(int $id): ?ServiceGroup
{
$serviceGroupData = null;
try {
$serviceGroupData = CTResponseUtil::dataAsArray(CTClient::getClient()->get('/api/servicegroups/' . $id));
} catch (GuzzleException $e) {
CTLog::getLog()->info('ServiceGroupRequestBuilder: Could not get ServiceGroup with Id' . $id);
}

if (empty($serviceGroupData)) {
return null;
} else {
return ServiceGroup::createModelFromData($serviceGroupData);
}
return ServiceGroup::class;
}
}
33 changes: 5 additions & 28 deletions src/Requests/ServiceRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,16 @@
use CTApi\Utils\CTResponseUtil;
use GuzzleHttp\Exception\GuzzleException;

class ServiceRequestBuilder
class ServiceRequestBuilder extends AbstractRequestBuilder
{
use Pagination;

public function all(): array
protected function getApiEndpoint(): string
{
$data = $this->collectDataFromPages('/api/services', []);
return Service::createModelsFromArray($data);
return '/api/services';
}

public function findOrFail(int $id): Service
protected function getModelClass(): string
{
$service = $this->find($id);
if ($service != null) {
return $service;
} else {
throw new CTRequestException("Service could not be found!");
}
}

public function find(int $id): ?Service
{
$serviceData = null;
try {
$serviceData = CTResponseUtil::dataAsArray(CTClient::getClient()->get('/api/services/' . $id));
} catch (GuzzleException $e) {
CTLog::getLog()->info('ServiceRequestBuilder: Could not get Service with Id' . $id);
}

if (empty($serviceData)) {
return null;
} else {
return Service::createModelFromData($serviceData);
}
return Service::class;
}
}
Loading

0 comments on commit 429af85

Please sign in to comment.