googleanalytics
/
lib
/
analytics-admin
/
vendor
/
google
/
gax
/
src
/
Middleware
/
FixedHeaderMiddleware.php
CredentialsWrapperMiddleware.php
3 years ago
FixedHeaderMiddleware.php
3 years ago
OperationsMiddleware.php
3 years ago
OptionsFilterMiddleware.php
3 years ago
PagedMiddleware.php
3 years ago
ResponseMetadataMiddleware.php
3 years ago
RetryMiddleware.php
3 years ago
FixedHeaderMiddleware.php
74 lines
| 1 | <?php |
| 2 | /* |
| 3 | * Copyright 2018 Google LLC |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | */ |
| 32 | |
| 33 | namespace Google\ApiCore\Middleware; |
| 34 | |
| 35 | use Google\ApiCore\Call; |
| 36 | |
| 37 | /** |
| 38 | * Middleware to add fixed headers to an API call. |
| 39 | */ |
| 40 | class FixedHeaderMiddleware |
| 41 | { |
| 42 | /** @var callable */ |
| 43 | private $nextHandler; |
| 44 | |
| 45 | private $headers; |
| 46 | private $overrideUserHeaders; |
| 47 | |
| 48 | public function __construct( |
| 49 | callable $nextHandler, |
| 50 | array $headers, |
| 51 | bool $overrideUserHeaders = false |
| 52 | ) { |
| 53 | $this->nextHandler = $nextHandler; |
| 54 | $this->headers = $headers; |
| 55 | $this->overrideUserHeaders = $overrideUserHeaders; |
| 56 | } |
| 57 | |
| 58 | public function __invoke(Call $call, array $options) |
| 59 | { |
| 60 | $userHeaders = isset($options['headers']) ? $options['headers'] : []; |
| 61 | if ($this->overrideUserHeaders) { |
| 62 | $options['headers'] = $this->headers + $userHeaders; |
| 63 | } else { |
| 64 | $options['headers'] = $userHeaders + $this->headers; |
| 65 | } |
| 66 | |
| 67 | $next = $this->nextHandler; |
| 68 | return $next( |
| 69 | $call, |
| 70 | $options |
| 71 | ); |
| 72 | } |
| 73 | } |
| 74 |