<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Security\Model\ResourceModel\AdminSessionInfo;

/**
 * Admin Session Info collection
 */
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
    /**
     * @var string
     */
    protected $_idFieldName = 'id';

    /**
     * @var \Magento\Framework\Stdlib\DateTime\DateTime
     */
    protected $dateTime;

    /**
     * @param \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory
     * @param \Psr\Log\LoggerInterface $logger
     * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
     * @param \Magento\Framework\Event\ManagerInterface $eventManager
     * @param \Magento\Framework\Stdlib\DateTime\DateTime $dateTime
     * @param \Magento\Framework\DB\Adapter\AdapterInterface|null $connection
     * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb|null $resource
     */
    public function __construct(
        \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
        \Psr\Log\LoggerInterface $logger,
        \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
        \Magento\Framework\Event\ManagerInterface $eventManager,
        \Magento\Framework\Stdlib\DateTime\DateTime $dateTime,
        \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
        \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
    ) {
        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
        $this->dateTime = $dateTime;
    }

    /**
     * Define resource model
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init(
            'Magento\Security\Model\AdminSessionInfo',
            'Magento\Security\Model\ResourceModel\AdminSessionInfo'
        );
    }

    /**
     * Update active sessions status except a specific one
     *
     * @param int $status
     * @param int $userId
     * @param string $sessionIdToExclude
     * @param int $updateOlderThen
     * @return int The number of affected rows.
     */
    public function updateActiveSessionsStatus(
        $status,
        $userId,
        $sessionIdToExclude,
        $updateOlderThen = null
    ) {
        return $this->getResource()->updateStatusByUserId(
            $status,
            $userId,
            [\Magento\Security\Model\AdminSessionInfo::LOGGED_IN],
            [$sessionIdToExclude],
            $updateOlderThen
        );
    }

    /**
     * Filter by user
     *
     * @param int $userId
     * @param int $status
     * @param null|string $sessionIdToExclude
     * @return $this
     */
    public function filterByUser(
        $userId,
        $status = \Magento\Security\Model\AdminSessionInfo::LOGGED_IN,
        $sessionIdToExclude = null
    ) {
        $this->addFieldToFilter('user_id', $userId);
        $this->addFieldToFilter('status', $status);
        if (null !== $sessionIdToExclude) {
            $this->addFieldToFilter('session_id', ['neq' => $sessionIdToExclude]);
        }
        return $this;
    }

    /**
     * Filter expired sessions
     *
     * @param int $sessionLifeTime
     * @return $this
     */
    public function filterExpiredSessions($sessionLifeTime)
    {
        $connection = $this->getConnection();
        $gmtTimestamp = $this->dateTime->gmtTimestamp();
        $this->addFieldToFilter(
            'updated_at',
            ['gt' => $connection->formatDate($gmtTimestamp - $sessionLifeTime)]
        );
        return $this;
    }

    /**
     * Delete sessions older than some value
     *
     * @param int $timestamp
     * @return $this
     */
    public function deleteSessionsOlderThen($timestamp)
    {
        $this->getResource()->deleteSessionsOlderThen((int) $timestamp);

        return $this;
    }
}
