123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- namespace Drupal\heartbeat;
- use Drupal\Core\Entity\EntityTypeManager;
- use Drupal\Core\Entity\EntityTypeRepository;
- use Drupal\Core\Entity\Query\QueryFactory;
- use Drupal\Core\Config\ConfigFactoryInterface;
- class HeartbeatStreamServices {
- protected $lastId;
- protected $latestTimestamp;
-
- protected $entityTypeManager;
-
- protected $entityTypeRepository;
-
- protected $entityQuery;
-
- protected $configFactory;
-
- public function __construct(EntityTypeManager $entityTypeManager, EntityTypeRepository $entityTypeRepository, QueryFactory $entityQuery, ConfigFactoryInterface $configFactory) {
- $this->entityTypeManager = $entityTypeManager;
- $this->entityTypeRepository = $entityTypeRepository;
- $this->entityQuery = $entityQuery;
- $this->configFactory = $configFactory;
- }
-
- public function getEntityById($id) {
- return $this->entityTypeManager->getStorage('heartbeat_stream')->load($id);
- }
-
- public function getTypesById($id) {
- return $this->entityTypeManager->getStorage('heartbeat_stream')->load($id)->get('types');
- }
-
- public function loadAllEntities() {
- return $this->entityQuery->get('heartbeat_stream')->execute();
- }
- public function loadStream($type) {
- return $this->entityQuery->get('heartbeat_stream')->condition('name', $type)->execute();
- }
-
- public function getAllStreams() {
- return $this->entityTypeManager->getStorage('heartbeat_stream')->loadMultiple($this->loadAllEntities());
- }
- public function createStreamForUids($uids) {
- return $this->entityTypeManager->getStorage('heartbeat')->loadMultiple($this->entityQuery->get('heartbeat')->condition('status', 1)->condition('uid', $uids, 'IN')->sort('created', 'DESC')->execute());
- }
- public function createStreamForUidsByType($uids, $type) {
- $stream = $this->entityTypeManager->getStorage('heartbeat_stream')->load(array_values($this->loadStream($type))[0]);
- $beats = $this->entityTypeManager->getStorage('heartbeat')->loadMultiple($this->entityQuery->get('heartbeat')->condition('status', 1)->condition('type', array_column($stream->getTypes(), 'target_id'), 'IN')->condition('uid', $uids, 'IN')->sort('created', 'DESC')->execute());
- $this->lastId = call_user_func('end', array_keys($beats));
- $this->configFactory->getEditable('heartbeat_update_feed')->set('timestamp', $this->lastId)->set('update', false)->save();
- $this->latestTimestamp = array_values($beats)[0]->getRevisionCreationTime();
- return $beats;
- }
- public function updateStreamForUidsByType($uids, $type) {
- $stream = $this->entityTypeManager->getStorage('heartbeat_stream')->load(array_values($this->loadStream($type))[0]);
- return $this->entityTypeManager->getStorage('heartbeat')->loadMultiple($this->entityQuery->get('heartbeat')->condition('status', 1)->condition('revision_created', $this->latestTimestamp, '>')->condition('type', array_column($stream->getTypes(), 'target_id'), 'IN')->condition('uid', $uids, 'IN')->sort('created', 'DESC')->execute());
- }
- }
|