GraphCollectionRequest.php
6 months ago
GraphRequest.php
6 months ago
GraphResponse.php
6 months ago
GraphCollectionRequest.php
242 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Copyright (c) Microsoft Corporation. All Rights Reserved. |
| 4 | * Licensed under the MIT License. See License in the project root |
| 5 | * for license information. |
| 6 | * |
| 7 | * GraphCollectionRequest File |
| 8 | * PHP version 7 |
| 9 | * |
| 10 | * @category Library |
| 11 | * @package Microsoft.Graph |
| 12 | * @copyright 2016 Microsoft Corporation |
| 13 | * @license https://opensource.org/licenses/MIT MIT License |
| 14 | * @version GIT: 0.1.0 |
| 15 | * @link https://graph.microsoft.io/ |
| 16 | */ |
| 17 | |
| 18 | namespace Microsoft\Graph\Http; |
| 19 | |
| 20 | use Microsoft\Graph\Exception\GraphException; |
| 21 | use Microsoft\Graph\Core\GraphConstants; |
| 22 | |
| 23 | /** |
| 24 | * Class GraphCollectionRequest |
| 25 | * |
| 26 | * @category Library |
| 27 | * @package Microsoft.Graph |
| 28 | * @license https://opensource.org/licenses/MIT MIT License |
| 29 | * @link https://graph.microsoft.io/ |
| 30 | */ |
| 31 | class GraphCollectionRequest extends GraphRequest |
| 32 | { |
| 33 | /** |
| 34 | * The size of page to divide the collection into |
| 35 | * |
| 36 | * @var int |
| 37 | */ |
| 38 | protected $pageSize; |
| 39 | /** |
| 40 | * The next link to use in calling a new page of results |
| 41 | * |
| 42 | * @var string |
| 43 | */ |
| 44 | protected $nextLink; |
| 45 | /** |
| 46 | * The delta link to use in calling /delta a subsequent time |
| 47 | * |
| 48 | * @var string |
| 49 | */ |
| 50 | protected $deltaLink; |
| 51 | /** |
| 52 | * True if the user has reached the end of the collection |
| 53 | * |
| 54 | * @var bool |
| 55 | */ |
| 56 | protected $end; |
| 57 | /** |
| 58 | * The endpoint that the user called (with query parameters) |
| 59 | * |
| 60 | * @var string |
| 61 | */ |
| 62 | protected $originalEndpoint; |
| 63 | /** |
| 64 | * The return type that the user specified |
| 65 | * |
| 66 | * @var object |
| 67 | */ |
| 68 | protected $originalReturnType; |
| 69 | |
| 70 | /** |
| 71 | * Constructs a new GraphCollectionRequest object |
| 72 | * |
| 73 | * @param string $requestType The HTTP verb for the |
| 74 | * request ("GET", "POST", "PUT", etc.) |
| 75 | * @param string $endpoint The URI of the endpoint to hit |
| 76 | * @param string $accessToken A valid access token |
| 77 | * @param string $baseUrl The base URL of the request |
| 78 | * @param string $apiVersion The version of the API to call |
| 79 | * @param string $proxyPort The url where to proxy through |
| 80 | * @param bool $proxyVerifySSL Whether the proxy requests should perform SSL verification |
| 81 | * @throws GraphException when no access token is provided |
| 82 | */ |
| 83 | public function __construct($requestType, $endpoint, $accessToken, $baseUrl, $apiVersion, $proxyPort = null, $proxyVerifySSL = false) |
| 84 | { |
| 85 | parent::__construct( |
| 86 | $requestType, |
| 87 | $endpoint, |
| 88 | $accessToken, |
| 89 | $baseUrl, |
| 90 | $apiVersion, |
| 91 | $proxyPort, |
| 92 | $proxyVerifySSL |
| 93 | ); |
| 94 | $this->end = false; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Gets the number of entries in the collection |
| 99 | * |
| 100 | * @return int the number of entries |
| 101 | * @throws GraphException |
| 102 | * @throws \AmeliaVendor\GuzzleHttp\Exception\GuzzleException |
| 103 | */ |
| 104 | public function count() |
| 105 | { |
| 106 | $query = '$count=true'; |
| 107 | $request = new GraphRequest( |
| 108 | $this->requestType, |
| 109 | $this->endpoint . $this->getConcatenator() . $query, |
| 110 | $this->accessToken, |
| 111 | $this->baseUrl, |
| 112 | $this->apiVersion, |
| 113 | $this->proxyPort |
| 114 | ); |
| 115 | $result = $request->execute()->getBody(); |
| 116 | |
| 117 | if (array_key_exists("@odata.count", $result)) { |
| 118 | return $result['@odata.count']; |
| 119 | } |
| 120 | |
| 121 | /* The $count query parameter for the Graph API |
| 122 | is available on several models but not all */ |
| 123 | trigger_error('Count unavailable for this collection'); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Sets the number of results to return with each call |
| 128 | * to "getPage()" |
| 129 | * |
| 130 | * @param int $pageSize The page size |
| 131 | * |
| 132 | * @throws GraphException if the requested page size exceeds |
| 133 | * the Graph's defined page size limit |
| 134 | * @return GraphCollectionRequest object |
| 135 | */ |
| 136 | public function setPageSize($pageSize) |
| 137 | { |
| 138 | if ($pageSize > GraphConstants::MAX_PAGE_SIZE) { |
| 139 | throw new GraphException(GraphConstants::MAX_PAGE_SIZE_ERROR); |
| 140 | } |
| 141 | $this->pageSize = $pageSize; |
| 142 | return $this; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Gets the next page of results |
| 147 | * |
| 148 | * @return array of objects of class $returnType |
| 149 | * @throws \AmeliaVendor\GuzzleHttp\Exception\GuzzleException |
| 150 | */ |
| 151 | public function getPage() |
| 152 | { |
| 153 | $this->setPageCallInfo(); |
| 154 | $response = $this->execute(); |
| 155 | |
| 156 | return $this->processPageCallReturn($response); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Sets the required query information to get a new page |
| 161 | * |
| 162 | * @return GraphCollectionRequest |
| 163 | */ |
| 164 | public function setPageCallInfo() |
| 165 | { |
| 166 | // Store these to add temporary query data to request |
| 167 | $this->originalReturnType = $this->returnType; |
| 168 | |
| 169 | /* This allows processPageCallReturn to receive |
| 170 | all of the response data, not just the objects */ |
| 171 | $this->returnType = null; |
| 172 | |
| 173 | if ($this->end) { |
| 174 | trigger_error('Reached end of collection'); |
| 175 | } |
| 176 | |
| 177 | if ($this->nextLink) { |
| 178 | $this->endpoint = "/" . implode("/", array_slice(explode("/", $this->nextLink), 4)); |
| 179 | } else { |
| 180 | // This is the first request to the endpoint |
| 181 | if ($this->pageSize) { |
| 182 | $this->endpoint .= $this->getConcatenator() . '$top=' . $this->pageSize; |
| 183 | } |
| 184 | } |
| 185 | return $this; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Clean up after making a page call request |
| 190 | * |
| 191 | * @param GraphResponse $response The GraphResponse returned |
| 192 | * after making a page call |
| 193 | * |
| 194 | * @return mixed result of the call, formatted according |
| 195 | * to the returnType set by the user |
| 196 | */ |
| 197 | public function processPageCallReturn($response) |
| 198 | { |
| 199 | $this->nextLink = $response->getNextLink(); |
| 200 | $this->deltaLink = $response->getDeltaLink(); |
| 201 | |
| 202 | /* If no skip token is returned, we have reached the end |
| 203 | of the collection */ |
| 204 | if (!$this->nextLink) { |
| 205 | $this->end = true; |
| 206 | } |
| 207 | |
| 208 | $result = $response->getBody(); |
| 209 | |
| 210 | // Cast as user-defined model |
| 211 | if ($this->originalReturnType) { |
| 212 | $result = $response->getResponseAsObject($this->originalReturnType); |
| 213 | } |
| 214 | |
| 215 | // Restore user-defined parameters |
| 216 | $this->returnType = $this->originalReturnType; |
| 217 | |
| 218 | return $result; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Gets whether the user has reached the end of the collection |
| 223 | * |
| 224 | * @return bool The end |
| 225 | */ |
| 226 | public function isEnd() |
| 227 | { |
| 228 | return $this->end; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Gets a delta link to use with subsequent |
| 233 | * calls to /delta |
| 234 | * |
| 235 | * @return string|null The delta link |
| 236 | */ |
| 237 | public function getDeltaLink() |
| 238 | { |
| 239 | return $this->deltaLink; |
| 240 | } |
| 241 | } |
| 242 |