Completed
Pull Request — master (#4522)
by Craig
13:45
created

OnlineRuntime   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 16
dl 0
loc 40
rs 10
c 1
b 0
f 1
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A onlineSince() 0 17 3
A __construct() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\UsersModule\Twig\Runtime;
15
16
use DateTime;
17
use Doctrine\Common\Collections\Criteria;
18
use Twig\Extension\RuntimeExtensionInterface;
19
use Zikula\ExtensionsModule\Api\ApiInterface\VariableApiInterface;
20
use Zikula\SecurityCenterModule\Constant;
21
use Zikula\UsersModule\Entity\RepositoryInterface\UserSessionRepositoryInterface;
22
use Zikula\UsersModule\Entity\UserEntity;
23
24
class OnlineRuntime implements RuntimeExtensionInterface
25
{
26
    /**
27
     * @var UserSessionRepositoryInterface
28
     */
29
    private $sessionRepository;
30
31
    /**
32
     * @var string
33
     */
34
    private $sessionStorageInFile;
35
36
    public function __construct(
37
        UserSessionRepositoryInterface $sessionRepository,
38
        VariableApiInterface $variableApi
39
    ) {
40
        $this->sessionRepository = $sessionRepository;
41
        $this->sessionStorageInFile = $variableApi->getSystemVar('sessionstoretofile', 1);
42
    }
43
44
    /**
45
     * @return bool|void
46
     */
47
    public function onlineSince(UserEntity $userEntity = null, int $minutes = 10)
48
    {
49
        if (null === $userEntity) {
50
            return;
51
        }
52
        if (Constant::SESSION_STORAGE_FILE === $this->sessionStorageInFile) {
0 ignored issues
show
introduced by
The condition Zikula\SecurityCenterMod...s->sessionStorageInFile is always false.
Loading history...
53
            return;
54
        }
55
56
        $since = new DateTime();
57
        $since->modify("-${minutes} minutes");
58
        $c = Criteria::create()
59
            ->where(Criteria::expr()->eq('uid', $userEntity->getUid()))
60
            ->andWhere(Criteria::expr()->gt('lastused', $since));
61
        $online = $this->sessionRepository->matching($c)->count();
62
63
        return (bool) $online;
64
    }
65
}
66