• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

FastyBird / simple-auth / 10026942148

21 Jul 2024 08:32AM UTC coverage: 67.871% (-1.0%) from 68.883%
10026942148

push

github

web-flow
Enable cache + added file fallback (#10)

13 of 29 new or added lines in 3 files covered. (44.83%)

1 existing line in 1 file now uncovered.

526 of 775 relevant lines covered (67.87%)

4.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

53.85
/src/Subscribers/Policy.php
1
<?php declare(strict_types = 1);
2

3
/**
4
 * Policy.php
5
 *
6
 * @license        More in LICENSE.md
7
 * @copyright      https://www.fastybird.com
8
 * @author         Adam Kadlec <adam.kadlec@fastybird.com>
9
 * @package        FastyBird:DevicesModule!
10
 * @subpackage     Subscribers
11
 * @since          1.0.0
12
 *
13
 * @date           22.03.20
14
 */
15

16
namespace FastyBird\SimpleAuth\Subscribers;
17

18
use Casbin;
19
use Doctrine\Common;
20
use Doctrine\ORM;
21
use Doctrine\Persistence;
22
use FastyBird\SimpleAuth\Entities;
23
use Nette;
24
use function count;
25

26
/**
27
 * Casbin policy entity subscriber
28
 *
29
 * @package        FastyBird:DevicesModule!
30
 * @subpackage     Subscribers
31
 *
32
 * @author         Adam Kadlec <adam.kadlec@fastybird.com>
33
 */
34
final class Policy implements Common\EventSubscriber
35
{
36

37
        use Nette\SmartObject;
38

39
        public function __construct(
40
                private readonly ORM\EntityManagerInterface $entityManager,
41
                private readonly Casbin\CachedEnforcer $enforcer,
42
        )
43
        {
44
        }
3✔
45

46
        public function getSubscribedEvents(): array
47
        {
48
                return [
3✔
49
                        0 => ORM\Events::postPersist,
3✔
50
                        1 => ORM\Events::postUpdate,
3✔
51
                        2 => ORM\Events::postRemove,
3✔
52
                ];
3✔
53
        }
54

55
        /**
56
         * @param Persistence\Event\LifecycleEventArgs<ORM\EntityManagerInterface> $eventArgs
57
         */
58
        public function postPersist(Persistence\Event\LifecycleEventArgs $eventArgs): void
59
        {
60
                // onFlush was executed before, everything already initialized
61
                $entity = $eventArgs->getObject();
3✔
62

63
                // Check for valid entity
64
                if (!$entity instanceof Entities\Policies\Policy) {
3✔
65
                        return;
×
66
                }
67

68
                $this->enforcer->invalidateCache();
3✔
69
                $this->enforcer->loadPolicy();
3✔
70
        }
71

72
        /**
73
         * @param Persistence\Event\LifecycleEventArgs<ORM\EntityManagerInterface> $eventArgs
74
         */
75
        public function postUpdate(Persistence\Event\LifecycleEventArgs $eventArgs): void
76
        {
77
                $uow = $this->entityManager->getUnitOfWork();
×
78

79
                // onFlush was executed before, everything already initialized
80
                $entity = $eventArgs->getObject();
×
81

82
                // Get changes => should be already computed here (is a listener)
83
                $changeSet = $uow->getEntityChangeSet($entity);
×
84

85
                // If we have no changes left => don't create revision log
86
                if (count($changeSet) === 0) {
×
87
                        return;
×
88
                }
89

90
                // Check for valid entity
91
                if (
92
                        !$entity instanceof Entities\Policies\Policy
×
93
                        || $uow->isScheduledForDelete($entity)
×
94
                ) {
95
                        return;
×
96
                }
97

NEW
98
                $this->enforcer->invalidateCache();
×
UNCOV
99
                $this->enforcer->loadPolicy();
×
100
        }
101

102
        /**
103
         * @param Persistence\Event\LifecycleEventArgs<ORM\EntityManagerInterface> $eventArgs
104
         */
105
        public function postRemove(Persistence\Event\LifecycleEventArgs $eventArgs): void
106
        {
107
                // onFlush was executed before, everything already initialized
108
                $entity = $eventArgs->getObject();
3✔
109

110
                // Check for valid entity
111
                if (!$entity instanceof Entities\Policies\Policy) {
3✔
112
                        return;
×
113
                }
114

115
                $this->enforcer->invalidateCache();
3✔
116
                $this->enforcer->loadPolicy();
3✔
117
        }
118

119
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc