Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   1  <?php
   2  
   3  namespace Packback\Lti1p3;
   4  
   5  use Packback\Lti1p3\Interfaces\IServiceRequest;
   6  
   7  class ServiceRequest implements IServiceRequest
   8  {
   9      // Request methods
  10      public const METHOD_GET = 'GET';
  11      public const METHOD_POST = 'POST';
  12      public const METHOD_PUT = 'PUT';
  13  
  14      // Request types
  15      public const TYPE_UNSUPPORTED = 'unsupported';
  16      public const TYPE_AUTH = 'auth';
  17      // MessageLaunch
  18      public const TYPE_GET_KEYSET = 'get_keyset';
  19      // AGS
  20      public const TYPE_GET_GRADES = 'get_grades';
  21      public const TYPE_SYNC_GRADE = 'sync_grades';
  22      public const TYPE_CREATE_LINEITEM = 'create_lineitem';
  23      public const TYPE_GET_LINEITEMS = 'get_lineitems';
  24      public const TYPE_GET_LINEITEM = 'get_lineitem';
  25      public const TYPE_UPDATE_LINEITEM = 'update_lineitem';
  26      // CGS
  27      public const TYPE_GET_GROUPS = 'get_groups';
  28      public const TYPE_GET_SETS = 'get_sets';
  29      // NRPS
  30      public const TYPE_GET_MEMBERSHIPS = 'get_memberships';
  31  
  32      private $method;
  33      private $url;
  34      private $type;
  35      private $body;
  36      private $payload;
  37      private $accessToken;
  38      private $contentType = 'application/json';
  39      private $accept = 'application/json';
  40  
  41      public function __construct(string $method, string $url, $type = self::UNSUPPORTED)
  42      {
  43          $this->method = $method;
  44          $this->url = $url;
  45          $this->type = $type;
  46      }
  47  
  48      public function getMethod(): string
  49      {
  50          return strtoupper($this->method);
  51      }
  52  
  53      public function getUrl(): string
  54      {
  55          return $this->url;
  56      }
  57  
  58      public function getPayload(): array
  59      {
  60          if (isset($this->payload)) {
  61              return $this->payload;
  62          }
  63  
  64          $payload = [
  65              'headers' => $this->getHeaders(),
  66          ];
  67  
  68          $body = $this->getBody();
  69          if ($body) {
  70              $payload['body'] = $body;
  71          }
  72  
  73          return $payload;
  74      }
  75  
  76      public function setUrl(string $url): IServiceRequest
  77      {
  78          $this->url = $url;
  79  
  80          return $this;
  81      }
  82  
  83      public function setAccessToken(string $accessToken): IServiceRequest
  84      {
  85          $this->accessToken = 'Bearer '.$accessToken;
  86  
  87          return $this;
  88      }
  89  
  90      public function setBody(string $body): IServiceRequest
  91      {
  92          $this->body = $body;
  93  
  94          return $this;
  95      }
  96  
  97      public function setPayload(array $payload): IServiceRequest
  98      {
  99          $this->payload = $payload;
 100  
 101          return $this;
 102      }
 103  
 104      public function setAccept(string $accept): IServiceRequest
 105      {
 106          $this->accept = $accept;
 107  
 108          return $this;
 109      }
 110  
 111      public function setContentType(string $contentType): IServiceRequest
 112      {
 113          $this->contentType = $contentType;
 114  
 115          return $this;
 116      }
 117  
 118      public function getErrorPrefix(): string
 119      {
 120          $defaultMessage = 'Logging request data:';
 121          $errorMessages = [
 122              static::TYPE_UNSUPPORTED => $defaultMessage,
 123              static::TYPE_AUTH => 'Authenticating:',
 124              static::TYPE_GET_KEYSET => 'Getting key set:',
 125              static::TYPE_GET_GRADES => 'Getting grades:',
 126              static::TYPE_SYNC_GRADE => 'Syncing grade for this lti_user_id:',
 127              static::TYPE_CREATE_LINEITEM => 'Creating lineitem:',
 128              static::TYPE_GET_LINEITEMS => 'Getting lineitems:',
 129              static::TYPE_GET_LINEITEM => 'Getting a lineitem:',
 130              static::TYPE_UPDATE_LINEITEM => 'Updating lineitem:',
 131              static::TYPE_GET_GROUPS => 'Getting groups:',
 132              static::TYPE_GET_SETS => 'Getting sets:',
 133              static::TYPE_GET_MEMBERSHIPS => 'Getting memberships:',
 134          ];
 135  
 136          return $errorMessages[$this->type] ?? $defaultMessage;
 137      }
 138  
 139      private function getHeaders(): array
 140      {
 141          $headers = [
 142              'Accept' => $this->accept,
 143          ];
 144  
 145          if (isset($this->accessToken)) {
 146              $headers['Authorization'] = $this->accessToken;
 147          }
 148  
 149          // Include Content-Type for POST and PUT requests
 150          if (in_array($this->getMethod(), [ServiceRequest::METHOD_POST, ServiceRequest::METHOD_PUT])) {
 151              $headers['Content-Type'] = $this->contentType;
 152          }
 153  
 154          return $headers;
 155      }
 156  
 157      private function getBody(): ?string
 158      {
 159          return $this->body;
 160      }
 161  }