|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is part of the bitbucket-api package. |
| 4 | + * |
| 5 | + * (c) Alexandru G. <alex@gentle.ro> |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + */ |
| 10 | +namespace Bitbucket\API\Http\Response; |
| 11 | + |
| 12 | +use Bitbucket\API\Http\ClientInterface; |
| 13 | +use Buzz\Message\MessageInterface; |
| 14 | +use Buzz\Message\Response; |
| 15 | + |
| 16 | +/** |
| 17 | + * @author Alexandru Guzinschi <alex@gentle.ro> |
| 18 | + */ |
| 19 | +class Pager implements PagerInterface |
| 20 | +{ |
| 21 | + /** @var ClientInterface */ |
| 22 | + private $httpClient; |
| 23 | + |
| 24 | + /** @var MessageInterface */ |
| 25 | + private $response; |
| 26 | + |
| 27 | + /** |
| 28 | + * @param ClientInterface $httpClient |
| 29 | + * @param MessageInterface $response |
| 30 | + * |
| 31 | + * @throws \UnexpectedValueException |
| 32 | + */ |
| 33 | + public function __construct(ClientInterface $httpClient, MessageInterface $response) |
| 34 | + { |
| 35 | + /** @var Response $response */ |
| 36 | + if (!$response->isOk()) { |
| 37 | + throw new \UnexpectedValueException("Can't paginate an unsuccessful response."); |
| 38 | + } |
| 39 | + |
| 40 | + $this->httpClient = $httpClient; |
| 41 | + $this->response = $response; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * {@inheritDoc} |
| 46 | + */ |
| 47 | + public function hasNext() |
| 48 | + { |
| 49 | + return array_key_exists('next', $this->getContent()); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * {@inheritDoc} |
| 54 | + */ |
| 55 | + public function hasPrevious() |
| 56 | + { |
| 57 | + return array_key_exists('previous', $this->getContent()); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * {@inheritDoc} |
| 62 | + */ |
| 63 | + public function fetchNext() |
| 64 | + { |
| 65 | + if ($this->hasNext()) { |
| 66 | + $content = $this->getContent(); |
| 67 | + return $this->response = $this->httpClient->get($content['next']); |
| 68 | + } |
| 69 | + |
| 70 | + return null; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * {@inheritDoc} |
| 75 | + */ |
| 76 | + public function fetchPrevious() |
| 77 | + { |
| 78 | + if ($this->hasPrevious()) { |
| 79 | + $content = $this->getContent(); |
| 80 | + return $this->response = $this->httpClient->get($content['previous']); |
| 81 | + } |
| 82 | + |
| 83 | + return null; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * {@inheritDoc} |
| 88 | + */ |
| 89 | + public function fetchAll() |
| 90 | + { |
| 91 | + $content = $this->getContent(); |
| 92 | + $values = []; |
| 93 | + |
| 94 | + // merge all `values` and replace it inside the most recent response. |
| 95 | + while (true) { |
| 96 | + if (!array_key_exists('values', $content)) { |
| 97 | + break; |
| 98 | + } |
| 99 | + |
| 100 | + $values = (0 === count($values)) ? $content['values'] : array_merge($values, $content['values']); |
| 101 | + |
| 102 | + if (null !== ($next = $this->fetchNext())) { |
| 103 | + $content = $this->getContent(); |
| 104 | + continue; |
| 105 | + } |
| 106 | + |
| 107 | + break; |
| 108 | + } |
| 109 | + |
| 110 | + $content['values'] = $values; |
| 111 | + $this->response->setContent(json_encode($content)); |
| 112 | + |
| 113 | + return $this->response; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * {@inheritDoc} |
| 118 | + */ |
| 119 | + public function getCurrent() |
| 120 | + { |
| 121 | + return $this->response; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @access private |
| 126 | + * @return array |
| 127 | + */ |
| 128 | + private function getContent() |
| 129 | + { |
| 130 | + $content = json_decode($this->response->getContent(), true); |
| 131 | + |
| 132 | + if (is_array($content) && JSON_ERROR_NONE === json_last_error()) { |
| 133 | + return $content; |
| 134 | + } |
| 135 | + |
| 136 | + return []; |
| 137 | + } |
| 138 | +} |
0 commit comments