openquestions-1.0.x-dev/src/EventSubscriber/OQVoteEventSubscriber.php
src/EventSubscriber/OQVoteEventSubscriber.php
<?php
namespace Drupal\openquestions\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\openquestions\Event\OQVoteEvent;
use Drupal\openquestions\Service\OQUtilsInterface;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* When a vote is placed, lock the question and item associated with that vote.
*
* Unless circumvented, that will prevent changes from being made after
* something has been voted on.
*/
class OQVoteEventSubscriber implements EventSubscriberInterface {
use StringTranslationTrait;
/**
* Config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManager
*/
protected $entityTypeManager;
/**
* Our utils.
*
* @var \Drupal\openquestions\Service\OQUtilsInterface
*/
protected $oqUtils;
/**
* Time.
*
* @var \Drupal\Component\Datetime\TimeInterface
*/
protected $time;
/**
* For t().
*
* @var \Drupal\Core\StringTranslation\TranslationInterface
*/
protected $stringTranslation;
/**
* Our logger.
*
* @var \Drupal\Core\Logger\LoggerChannelInterface
*/
protected $logger;
/**
* OQVoteEventSubscriber constructor.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* Config.
* @param \Drupal\Core\Entity\EntityTypeManager $entityTypeManager
* Entity info.
* @param \Drupal\openquestions\Service\OQUtilsInterface $oqUtils
* Our utils.
* @param \Drupal\Component\Datetime\TimeInterface $time
* Time.
* @param \Drupal\Core\StringTranslation\TranslationInterface $stringTranslation
* For t().
* @param \Drupal\Core\Logger\LoggerChannelInterface $logger
* Our logger.
*/
public function __construct(ConfigFactoryInterface $configFactory,
EntityTypeManager $entityTypeManager,
OQUtilsInterface $oqUtils,
TimeInterface $time,
TranslationInterface $stringTranslation,
LoggerChannelInterface $logger) {
$this->configFactory = $configFactory;
$this->entityTypeManager = $entityTypeManager;
$this->oqUtils = $oqUtils;
$this->time = $time;
$this->stringTranslation = $stringTranslation;
$this->logger = $logger;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[OQVoteEvent::EVENT][] = ['onEvent', 0];
return $events;
}
/**
* Callback.
*/
public function onEvent(OQVoteEvent $event) {
$vote = $event->getVote();
if (!$vote) {
return;
}
$msg = $this->t('Vote # @id has been cast', ['@id' => $vote->id()]);
$item = $this->oqUtils->getFirstReference($vote, 'item');
if ($item && !$item->get('locked_status')->value) {
$item->set('locked_status', TRUE);
$item->set('locked_date', $this->time->getRequestTime());
$item->set('locked_reason', $msg);
$item->save();
}
$question = $this->oqUtils->getFirstReference($vote, 'question');
if ($question && !$question->get('locked_status')->value) {
$question->set('locked_status', TRUE);
$question->set('locked_date', $this->time->getRequestTime());
$question->set('locked_reason', $msg);
$question->save();
}
}
}
