Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

   1  <?php
   2  
   3  namespace Packback\Lti1p3;
   4  
   5  use Packback\Lti1p3\Interfaces\IServiceRequest;
   6  
   7  class ServiceRequest implements IServiceRequest
   8  {
   9      private $method;
  10      private $url;
  11      private $body;
  12      private $accessToken;
  13      private $contentType = 'application/json';
  14      private $accept = 'application/json';
  15  
  16      public function __construct(string $method, string $url)
  17      {
  18          $this->method = $method;
  19          $this->url = $url;
  20      }
  21  
  22      public function getMethod(): string
  23      {
  24          return strtoupper($this->method);
  25      }
  26  
  27      public function getUrl(): string
  28      {
  29          return $this->url;
  30      }
  31  
  32      public function getPayload(): array
  33      {
  34          $payload = [
  35              'headers' => $this->getHeaders(),
  36          ];
  37  
  38          $body = $this->getBody();
  39          if ($body) {
  40              $payload['body'] = $body;
  41          }
  42  
  43          return $payload;
  44      }
  45  
  46      public function setUrl(string $url): IServiceRequest
  47      {
  48          $this->url = $url;
  49  
  50          return $this;
  51      }
  52  
  53      public function setAccessToken(string $accessToken): IServiceRequest
  54      {
  55          $this->accessToken = 'Bearer '.$accessToken;
  56  
  57          return $this;
  58      }
  59  
  60      public function setBody(string $body): IServiceRequest
  61      {
  62          $this->body = $body;
  63  
  64          return $this;
  65      }
  66  
  67      public function setAccept(string $accept): IServiceRequest
  68      {
  69          $this->accept = $accept;
  70  
  71          return $this;
  72      }
  73  
  74      public function setContentType(string $contentType): IServiceRequest
  75      {
  76          $this->contentType = $contentType;
  77  
  78          return $this;
  79      }
  80  
  81      private function getHeaders(): array
  82      {
  83          $headers = [
  84              'Accept' => $this->accept,
  85          ];
  86  
  87          if (isset($this->accessToken)) {
  88              $headers['Authorization'] = $this->accessToken;
  89          }
  90  
  91          if ($this->getMethod() === LtiServiceConnector::METHOD_POST) {
  92              $headers['Content-Type'] = $this->contentType;
  93          }
  94  
  95          return $headers;
  96      }
  97  
  98      private function getBody(): ?string
  99      {
 100          return $this->body;
 101      }
 102  }