TeamInvitation.php in Apigee Edge 8
File
modules/apigee_edge_teams/src/Entity/TeamInvitation.php
View source
<?php
namespace Drupal\apigee_edge_teams\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\user\EntityOwnerTrait;
use Drupal\user\UserInterface;
class TeamInvitation extends ContentEntityBase implements TeamInvitationInterface {
use StringTranslationTrait;
use EntityOwnerTrait;
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields += static::ownerBaseFieldDefinitions($entity_type);
$fields[$entity_type
->getKey('id')] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setReadOnly(TRUE);
$fields['uid']
->setDescription(t('The team invitation author.'));
$fields['label'] = BaseFieldDefinition::create('string')
->setLabel(t('Label'))
->setDescription(t('The label of the invitation.'))
->setDefaultValue('')
->setRequired(TRUE);
$fields['status'] = BaseFieldDefinition::create('list_string')
->setLabel(t('Status'))
->setDescription(t('The status of the invitation.'))
->setDefaultValue(TeamInvitationInterface::STATUS_PENDING)
->setSetting('allowed_values', [
TeamInvitationInterface::STATUS_PENDING => t('Pending'),
TeamInvitationInterface::STATUS_ACCEPTED => t('Accepted'),
TeamInvitationInterface::STATUS_DECLINED => t('Declined'),
TeamInvitationInterface::STATUS_EXPIRED => t('Expired'),
])
->setRequired(TRUE);
$fields['team'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Team'))
->setDescription(t('The team for this invitation.'))
->setSetting('target_type', 'team')
->setRequired(TRUE);
$fields['team_roles'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Roles'))
->setDescription(t('The team roles for this invitation.'))
->setSetting('target_type', 'team_role')
->setRequired(TRUE)
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED);
$fields['recipient'] = BaseFieldDefinition::create('email')
->setLabel(t('Recipient'))
->setDescription(t('The email address of the invitee.'))
->setDefaultValue('')
->setRequired(TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The created time for the invitation.'));
$fields['expiry'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Expiry'))
->setDescription(t('The expiry time for the invitation.'));
return $fields;
}
public function getLabel() : string {
return $this
->get('label')->value;
}
public function setLabel(string $label) : TeamInvitationInterface {
$this
->set('label', $label);
return $this;
}
public function getTeam() : ?TeamInterface {
return $this
->get('team')->entity;
}
public function setTeam(TeamInterface $team) : TeamInvitationInterface {
$this
->set('team', [
'target_id' => $team
->id(),
]);
return $this;
}
public function getStatus() : int {
return $this
->get('status')->value;
}
public function setStatus(int $status) : TeamInvitationInterface {
$this
->set('status', $status);
return $this;
}
public function getRecipient() : ?string {
return $this
->get('recipient')->value;
}
public function setRecipient(string $email) : TeamInvitationInterface {
$this
->set('recipient', $email);
return $this;
}
public function getTeamRoles() : ?array {
return $this
->get('team_roles')
->referencedEntities();
}
public function setTeamRoles(array $team_roles) : TeamInvitationInterface {
$this
->set('team_roles', $team_roles);
return $this;
}
public function getCreatedTime() : int {
return $this
->get('created')->value;
}
public function setExpiryTime(int $expiry_time) : TeamInvitationInterface {
$this
->set('expiry', $expiry_time);
return $this;
}
public function getExpiryTime() : int {
return $this
->get('expiry')->value;
}
public function isExpired() : bool {
return $this
->getStatus() === TeamInvitationInterface::STATUS_EXPIRED || $this
->getExpiryTime() < \Drupal::time()
->getCurrentTime();
}
public function isPending() : bool {
return $this
->getStatus() === TeamInvitationInterface::STATUS_PENDING;
}
public function isAccepted() : bool {
return $this
->getStatus() === TeamInvitationInterface::STATUS_ACCEPTED;
}
public function isDeclined() : bool {
return $this
->getStatus() === TeamInvitationInterface::STATUS_DECLINED;
}
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
if ($this
->get('label')
->isEmpty()) {
$this
->setLabel($this
->t('Invitation to join @team as @roles', [
'@team' => $this
->getTeam()
->label(),
'@roles' => implode(', ', array_map(function (TeamRoleInterface $team_role) {
return $team_role
->label();
}, $this
->getTeamRoles())),
]));
}
if ($this
->get('expiry')
->isEmpty()) {
$days = \Drupal::config('apigee_edge_teams.team_settings')
->get('team_invitation_expiry_days');
$this
->setExpiryTime($this
->getCreatedTime() + 24 * 60 * 60 * (int) $days);
}
}
public function getCacheTags() {
if ($this
->getTeam()) {
$this->cacheTags = array_merge($this->cacheTags, $this
->getTeam()
->getCacheTags());
}
return parent::getCacheTags();
}
}