InviteAccept.php in Invite 8
File
src/Controller/InviteAccept.php
View source
<?php
namespace Drupal\invite\Controller;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\invite\InviteConstants;
use Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher;
use Drupal\Core\Controller\ControllerBase;
use Drupal\invite\InviteAcceptEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
class InviteAccept extends ControllerBase {
public $dispatcher;
protected $messenger;
public static function create(ContainerInterface $container) {
return new static($container
->get('event_dispatcher'), $container
->get('messenger'));
}
public function __construct(ContainerAwareEventDispatcher $dispatcher, MessengerInterface $messenger) {
$this->dispatcher = $dispatcher;
$this->messenger = $messenger;
}
public function accept($invite) {
$account = $this
->currentUser();
$redirect = '<front>';
$message = 'Hmm.';
$type = 'status';
if ($account
->id() == $invite
->getOwnerId()) {
$message = $this
->t("You can't use your own invite.");
$type = 'error';
}
elseif ($invite
->getStatus() == InviteConstants::INVITE_USED) {
$message = $this
->t('Sorry, this invitation has already been used.');
$type = 'error';
}
elseif ($invite
->getStatus() == InviteConstants::INVITE_WITHDRAWN) {
$message = $this
->t('Sorry, this invitation has already been withdrawn.');
$type = 'error';
}
elseif ($invite->expires->value < time()) {
$message = $this
->t('Sorry, this invitation is expired.');
$type = 'error';
$invite
->setStatus(InviteConstants::INVITE_EXPIRED);
$invite
->save();
}
else {
$_SESSION['invite_code'] = $invite
->getRegCode();
$redirect = 'user.register';
$message = $this
->t('Please create an account to accept the invitation.');
}
$invite_accept = new InviteAcceptEvent([
'redirect' => &$redirect,
'message' => &$message,
'type' => &$type,
'invite' => &$invite,
]);
$this->dispatcher
->dispatch('invite_accept', $invite_accept);
$this->messenger
->addStatus($message, $type);
return $this
->redirect($redirect);
}
}