AuthInvalidator::handle()   B
last analyzed

Complexity

Conditions 8
Paths 21

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 20
rs 8.4444
cc 8
nc 21
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Events\Listeners;
6
7
use AbterPhp\Admin\Domain\Entities\AdminResource;
8
use AbterPhp\Admin\Domain\Entities\User;
9
use AbterPhp\Admin\Domain\Entities\UserGroup;
10
use AbterPhp\Framework\Authorization\CacheManager;
11
use AbterPhp\Framework\Constant\Session;
12
use AbterPhp\Framework\Events\EntityChange;
13
use Opulence\Sessions\ISession;
14
15
class AuthInvalidator
16
{
17
    protected CacheManager $cacheManager;
18
19
    protected ISession $session;
20
21
    /**
22
     * AuthInvalidator constructor.
23
     *
24
     * @param CacheManager $cacheManager
25
     * @param ISession     $session
26
     */
27
    public function __construct(CacheManager $cacheManager, ISession $session)
28
    {
29
        $this->cacheManager = $cacheManager;
30
31
        $this->session = $session;
32
    }
33
34
    /**
35
     * @param EntityChange $event
36
     */
37
    public function handle(EntityChange $event)
38
    {
39
        // phpcs:disable Generic.CodeAnalysis.EmptyStatement.DetectedCatch
40
        switch ($event->getEntityName()) {
41
            case AdminResource::class:
42
            case User::class:
43
            case UserGroup::class:
44
                try {
45
                    $this->cacheManager->clearAll();
46
                } catch (\Exception $e) {
47
                    // Empty catch
48
                }
49
50
                break;
51
        }
52
53
        if ($event->getEntityName() == User::class && $event->getEntityId() == $this->session->get(Session::USER_ID)) {
54
            try {
55
                $this->session->flush();
56
            } catch (\Exception $e) {
57
                // Empty catch
58
            }
59
        }
60
        // phpcs:enable Generic.CodeAnalysis.EmptyStatement.DetectedCatch
61
    }
62
}
63