LTI Integration Library  3.1.0
PHP class library for building LTI integrations
ResourceLinkShareKey.php
Go to the documentation of this file.
1 <?php
2 
3 namespace ceLTIc\LTI;
4 
6 
16 {
17 
21  const MAX_SHARE_KEY_LIFE = 168; // in hours (1 week)
22 
26  const DEFAULT_SHARE_KEY_LIFE = 24; // in hours
27 
32 
37 
43  public $resourceLinkId = null;
44 
50  public $length = null;
51 
57  public $life = null; // in hours
58 
64  public $autoApprove = false;
65 
71  public $expires = null;
72 
78  private $id = null;
79 
85  private $dataConnector = null;
86 
93  public function __construct($resourceLink, $id = null)
94  {
95  $this->initialize();
96  $this->dataConnector = $resourceLink->getDataConnector();
97  $this->resourceLinkId = $resourceLink->getRecordId();
98  $this->id = $id;
99  if (!empty($id)) {
100  $this->load();
101  }
102  }
103 
107  public function initialize()
108  {
109  $this->length = null;
110  $this->life = null;
111  $this->autoApprove = false;
112  $this->expires = null;
113  }
114 
120  public function initialise()
121  {
122  $this->initialize();
123  }
124 
130  public function save()
131  {
132  if (empty($this->life)) {
133  $this->life = self::DEFAULT_SHARE_KEY_LIFE;
134  } else {
135  $this->life = max(min($this->life, self::MAX_SHARE_KEY_LIFE), 0);
136  }
137  $this->expires = time() + ($this->life * 60 * 60);
138  if (empty($this->id)) {
139  if (empty($this->length) || !is_numeric($this->length)) {
140  $this->length = self::MAX_SHARE_KEY_LENGTH;
141  } else {
142  $this->length = max(min($this->length, self::MAX_SHARE_KEY_LENGTH), self::MIN_SHARE_KEY_LENGTH);
143  }
144  $this->id = DataConnector::getRandomString($this->length);
145  }
146 
147  return $this->dataConnector->saveResourceLinkShareKey($this);
148  }
149 
155  public function delete()
156  {
157  return $this->dataConnector->deleteResourceLinkShareKey($this);
158  }
159 
165  public function getId()
166  {
167  return $this->id;
168  }
169 
170 ###
171 ### PRIVATE METHOD
172 ###
173 
177  private function load()
178  {
179  $this->initialize();
180  $this->dataConnector->loadResourceLinkShareKey($this);
181  if (!is_null($this->id)) {
182  $this->length = strlen(strval($this->id));
183  }
184  if (!is_null($this->expires)) {
185  $this->life = ($this->expires - time()) / 60 / 60;
186  }
187  }
188 
189 }
$autoApprove
Whether the sharing arrangement should be automatically approved when first used.
$resourceLinkId
ID for resource link being shared.
Class to provide a connection to a persistent store for LTI objects.
initialize()
Initialise the resource link share key.
$expires
Timestamp for when the share key expires.
const MIN_SHARE_KEY_LENGTH
Minimum length for a share key value.
initialise()
Initialise the resource link share key.
save()
Save the resource link share key to the database.
const DEFAULT_SHARE_KEY_LIFE
Default life for a share key value.
const MAX_SHARE_KEY_LIFE
Maximum permitted life for a share key value.
Class to represent a tool consumer resource link share key.
static getRandomString($length=8)
Generate a random string.
const MAX_SHARE_KEY_LENGTH
Maximum length for a share key value.
__construct($resourceLink, $id=null)
Class constructor.