Completed
Push — master ( 76abef...f75f17 )
by Jan
04:26
created

TreeCacheInvalidationListener::invalidate()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 8
nop 2
dl 0
loc 19
rs 9.9666
1
<?php
2
/**
3
 *
4
 * part-db version 0.1
5
 * Copyright (C) 2005 Christoph Lechner
6
 * http://www.cl-projects.de/
7
 *
8
 * part-db version 0.2+
9
 * Copyright (C) 2009 K. Jacobs and others (see authors.php)
10
 * http://code.google.com/p/part-db/
11
 *
12
 * Part-DB Version 0.4+
13
 * Copyright (C) 2016 - 2019 Jan Böhmer
14
 * https://github.com/jbtronics
15
 *
16
 * This program is free software; you can redistribute it and/or
17
 * modify it under the terms of the GNU General Public License
18
 * as published by the Free Software Foundation; either version 2
19
 * of the License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, write to the Free Software
28
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
29
 *
30
 */
31
32
namespace App\EntityListeners;
33
34
35
use App\Entity\Base\DBElement;
36
use App\Entity\Base\StructuralDBElement;
37
use App\Entity\UserSystem\Group;
38
use App\Entity\UserSystem\User;
39
use Doctrine\ORM\Event\LifecycleEventArgs;
40
use Doctrine\ORM\Event\PostFlushEventArgs;
41
use Doctrine\ORM\Event\PreFlushEventArgs;
42
use Doctrine\ORM\Mapping as ORM;
43
use Symfony\Contracts\Cache\TagAwareCacheInterface;
44
45
class TreeCacheInvalidationListener
46
{
47
    protected $cache;
48
49
    public function __construct(TagAwareCacheInterface $treeCache)
50
    {
51
        $this->cache = $treeCache;
52
    }
53
54
    /**
55
     * @ORM\PostUpdate()
56
     * @ORM\PostPersist()
57
     * @ORM\PostRemove()
58
     *
59
     * @param DBElement $element
60
     * @param LifecycleEventArgs $event
61
     */
62
    public function invalidate(DBElement $element, LifecycleEventArgs $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

62
    public function invalidate(DBElement $element, /** @scrutinizer ignore-unused */ LifecycleEventArgs $event)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
63
    {
64
        //If an element was changed, then invalidate all cached trees with this element class
65
        if ($element instanceof StructuralDBElement) {
66
            $secure_class_name = str_replace("\\", '_', get_class($element));
67
            $this->cache->invalidateTags([$secure_class_name]);
68
        }
69
70
        //If a user change, then invalidate all cached trees for him
71
        if ($element instanceof User) {
72
            $tag = "user_" . $element->getUsername();
73
            $this->cache->invalidateTags([$tag]);
74
        }
75
76
        /* If any group change, then invalidate all cached trees. Users Permissions can be inherited from groups,
77
            so a change in any group can cause big permisssion changes for users. So to be sure, invalidate all trees */
78
        if($element instanceof Group) {
79
            $tag = "groups";
80
            $this->cache->invalidateTags([$tag]);
81
        }
82
83
    }
84
}