Skip to content
Open
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
38 changes: 38 additions & 0 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace EclipseGc\DrupalOrg\Api;

use GuzzleHttp\Client;
use GuzzleHttp\Message\Request;

class Factory implements FactoryInterface {

Expand All @@ -27,6 +28,23 @@ public function getObjectTypeClass($type, array $data) {
throw new \Exception(sprintf('No object type %s', $type));
}

public function createList($type, $params, array $data = array()) {
$reflector = new \ReflectionClass('\EclipseGc\DrupalOrg\Api\Resources\ResourceList');
$data['type'] = $type;
$data['params'] = $params;
if (!isset($data['factory'])) {
$data['factory'] = $this;
}
$arguments = [];
foreach ($reflector->getMethod('__construct')->getParameters() as $param) {
$param_name = $param->getName();
if (array_key_exists($param_name, $data)) {
$arguments[] = $data[$param_name];
}
}
return $reflector->newInstanceArgs($arguments);
}

public function createObjectType($type, array $data = array()) {
$objectClass = $this->getObjectTypeClass($type, $data);
$reflector = new \ReflectionClass($objectClass);
Expand Down Expand Up @@ -57,6 +75,26 @@ public function request($entity_type, $id) {
return $request;
}

public function pagedRequest($entity_type, array $params, $page = 0) {
if (!isset($params['page'])) {
$params['page'] = $page;
}
// You have to add the .json or it doesn't work?
$request = $this->client()->createRequest('GET', $entity_type . '.json');
// Add our request params.
$request->getQuery()->merge($params);

var_dump($request->getUrl());

$request = $this->client()->send($request);

if ($request->getStatusCode() != 200) {
throw new \Exception(sprintf('Status code was not OK. %d returned instead.', $request->getStatusCode()));
}
return $request;

}

/**
* Statically stores and returns a guzzle client.
*
Expand Down
16 changes: 16 additions & 0 deletions src/FactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ public function getObjectTypeClass($type, array $data);
*/
public function request($entity_type, $id);


/**
* Makes a paged request.
*
* @param $entity_type
* The type of entity.
* @param $params
* Search parameters.
* @param $page
* The integer of the page to load.
* @return \GuzzleHttp\Message\ResponseInterface
*/
public function pagedRequest($entity_type, array $params, $page = 0);

/**
* Results in a full object for the provided data.
*
Expand All @@ -43,4 +57,6 @@ public function request($entity_type, $id);
* @return \EclipseGc\DrupalOrg\Api\Resources\ResourceInterface
*/
public function createObjectType($type, array $data = array());

public function createList($type, $params, array $data = array());
}
4 changes: 4 additions & 0 deletions src/Resources/GetResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ protected function getResource($resource_type, $id) {
return $this->factory->createObjectType($resource_type, $response->json());
}

public function getPagedResource($resource_type, array $params) {
$response = $this->factory->pagedRequest($resource_type, $params);
return $this->factory->createList($resource_type, $params, $response->json());
}
}
14 changes: 14 additions & 0 deletions src/Resources/PagedInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace EclipseGc\DrupalOrg\Api\Resources;

use EclipseGc\DrupalOrg\Api\FactoryInterface;


interface PagedInterface {
public function __construct(FactoryInterface $factoryInterface, $type, $params, $self, $list, $first = '', $last = '', $next = '');

public function getAll();

public function getSomeResources($number = 100);
}
72 changes: 72 additions & 0 deletions src/Resources/ResourceList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace EclipseGc\DrupalOrg\Api\Resources;

use EclipseGc\DrupalOrg\Api\FactoryInterface;

class ResourceList implements PagedInterface {

use GetResource;

protected $approxResults;
protected $totalResults;
protected $loadedPages;
protected $loadedResults;
protected $list;
protected $type;
protected $params;

public function __construct(FactoryInterface $factory, $type, $params, $self, $list, $first = '', $last = '', $next = '') {
$this->params = $params;
$this->factory = $factory;
$this->type = $type;
$this->list = $list;
$this->loadedResults = count($this->list);
preg_match('#page=(\d+)#', $last, $matches);
$this->lastPage = $matches[1];
$this->approxResults = $this->lastPage + 1 * 100;
$this->loadedPages = 0;
}

public function getItems() {
$items = [];
foreach ($this->list as $listItem) {
$items[] = $this->factory->createObjectType($this->type, $listItem);
}
return $items;
}

public function getTotalResults() {
if ($this->totalResults === NULL) {
$results = $this->factory->pagedRequest($this->type, $this->params, $this->lastPage);
$this->totalResults = $this->lastPage * 100 + count($results->json()['list']);
}
return $this->totalResults;
}

public function loadAllObjects() {
for ($i = $this->loadedPages + 1; $i <= $this->lastPage; $i++) {
$results = $this->factory->pagedRequest($this->type, $this->params, $i);
$data = $results->json();
$this->list = array_merge($this->list, $data['list']);
$this->loadedResults += count($data['list']);
}
}

public function getAll() {
$this->loadAllObjects();
$items = [];
foreach ($this->list as $listItem) {
$items[] = $this->factory->createObjectType($this->type, $listItem);
}
return $items;
}

public function getSomeResources($number = 100) {
$items = [];
foreach (array_slice($this->list, 0, $number) as $listItem) {
$items[] = $this->factory->createObjectType($this->type, $listItem);
}
return $items;
}
}