|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace SoapBox\SignedRequests\Requests; |
| 4 | + |
| 5 | +use GuzzleHttp\Psr7\Request as GuzzleRequest; |
| 6 | +use Illuminate\Http\Request as IlluminateRequest; |
| 7 | + |
| 8 | +class Payload |
| 9 | +{ |
| 10 | + /** |
| 11 | + * A request object. Currently both \GuzzleHttp\Psr7\Request and |
| 12 | + * \Illuminate\Http\Request are supported. |
| 13 | + * |
| 14 | + * @var mixed |
| 15 | + */ |
| 16 | + private $request; |
| 17 | + |
| 18 | + /** |
| 19 | + * Set's the local request to extract a payload from. |
| 20 | + * |
| 21 | + * @param mixed $request |
| 22 | + * The request to extract a payload from. Currently both |
| 23 | + * \GuzzleHttp\Psr7\Request and \Illuminate\Http\Request are |
| 24 | + * supported. |
| 25 | + */ |
| 26 | + public function __construct($request) |
| 27 | + { |
| 28 | + $this->request = $request; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Returns the payload from a guzzle request. |
| 33 | + * |
| 34 | + * @param \GuzzleHttp\Psr7\Request $request |
| 35 | + * An instance of the guzzle request to extract a payload from. |
| 36 | + * |
| 37 | + * @return string |
| 38 | + * The payload. |
| 39 | + */ |
| 40 | + protected function generateFromGuzzleRequest(GuzzleRequest $request) : string |
| 41 | + { |
| 42 | + $id = isset($this->request->getHeader('X-SIGNED-ID')[0]) ? |
| 43 | + $this->request->getHeader('X-SIGNED-ID')[0] : ''; |
| 44 | + |
| 45 | + return json_encode([ |
| 46 | + 'id' => (string) $id, |
| 47 | + 'method' => $this->request->getMethod(), |
| 48 | + 'uri' => (string) $this->request->getUri(), |
| 49 | + 'content' => $this->request->getBody() |
| 50 | + ], JSON_UNESCAPED_SLASHES); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Retruns the payload from an illuminate request. |
| 55 | + * |
| 56 | + * @param \Illuminate\Http\Request $request |
| 57 | + * An instance of the illuminate request to extract the payload from. |
| 58 | + * |
| 59 | + * @return string |
| 60 | + * The payload. |
| 61 | + */ |
| 62 | + protected function generateFromIlluminateRequest(IlluminateRequest $request) : string |
| 63 | + { |
| 64 | + $id = $this->request->headers->get('X-SIGNED-ID', ''); |
| 65 | + |
| 66 | + return json_encode([ |
| 67 | + 'id' => (string) $id, |
| 68 | + 'method' => $this->request->getMethod(), |
| 69 | + 'uri' => (string) $this->request->fullUrl(), |
| 70 | + 'content' => $this->request->getContent() |
| 71 | + ], JSON_UNESCAPED_SLASHES); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Returns a payload with the id, method, uri, and content embedded. |
| 76 | + * |
| 77 | + * @return string |
| 78 | + * A json encoded payload. |
| 79 | + */ |
| 80 | + public function __toString() : string |
| 81 | + { |
| 82 | + if ($this->request instanceof GuzzleRequest) { |
| 83 | + return $this->generateFromGuzzleRequest($this->request); |
| 84 | + } |
| 85 | + |
| 86 | + if ($this->request instanceof IlluminateRequest) { |
| 87 | + return $this->generateFromIlluminateRequest($this->request); |
| 88 | + } |
| 89 | + |
| 90 | + return ''; |
| 91 | + } |
| 92 | +} |
0 commit comments