diff --git a/src/Factory.php b/src/Factory.php index ed2a16e..9718a8e 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -7,6 +7,7 @@ namespace EclipseGc\DrupalOrg\Api; use GuzzleHttp\Client; +use GuzzleHttp\Message\Request; class Factory implements FactoryInterface { @@ -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); @@ -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. * diff --git a/src/FactoryInterface.php b/src/FactoryInterface.php index d0bcb39..cce0f41 100644 --- a/src/FactoryInterface.php +++ b/src/FactoryInterface.php @@ -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. * @@ -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()); } \ No newline at end of file diff --git a/src/Resources/GetResource.php b/src/Resources/GetResource.php index ba407ba..10b34e8 100644 --- a/src/Resources/GetResource.php +++ b/src/Resources/GetResource.php @@ -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()); + } } diff --git a/src/Resources/PagedInterface.php b/src/Resources/PagedInterface.php new file mode 100644 index 0000000..95ba20b --- /dev/null +++ b/src/Resources/PagedInterface.php @@ -0,0 +1,14 @@ +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; + } +}