metadata = $metadata; $this->entityManager = $entityManager; $this->user = $user; $this->listLoadProcessor = $listLoadProcessor; } /** * @return RecordCollection */ public function getLastViewed(?int $maxSize, ?int $offset): RecordCollection { $scopes = $this->metadata->get('scopes'); $targetTypeList = array_filter( array_keys($scopes), function ($item) use ($scopes) { return !empty($scopes[$item]['object']) || !empty($scopes[$item]['lastViewed']); } ); $maxSize = $maxSize ?? 0; $offset = $offset ?? 0; $collection = $this->entityManager ->getRDBRepositoryByClass(ActionHistoryRecord::class) ->where([ 'userId' => $this->user->getId(), 'action' => ActionHistoryRecord::ACTION_READ, 'targetType' => $targetTypeList, ]) ->order('MAX:createdAt', 'DESC') ->select([ 'targetId', 'targetType', 'MAX:number', ['MAX:createdAt', 'createdAt'], ]) ->group(['targetId', 'targetType']) ->limit($offset, $maxSize + 1) ->find(); foreach ($collection as $entity) { $this->listLoadProcessor->process($entity); $entity->set('id', Util::generateId()); } return RecordCollection::createNoCount($collection, $maxSize); } }