Passed
Push — master ( 1445d7...8e2362 )
by Jan
04:04
created

UserCacheKeyGenerator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 30
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A generateKey() 0 15 4
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\Services;
33
34
35
use App\Entity\UserSystem\User;
36
use Symfony\Component\Security\Core\Security;
37
38
/**
39
 * Purpose of this service is to generate a key unique for a user, to use in Cache keys and tags.
40
 * @package App\Services
41
 */
42
class UserCacheKeyGenerator
43
{
44
    protected $security;
45
46
    public function __construct(Security $security)
47
    {
48
        $this->security = $security;
49
    }
50
51
    /**
52
     * Generates a key for the given user.
53
     * @param User|null $user The user for which the key should be generated. When set to null, the currently logged in
54
     * user is used.
55
     * @return string
56
     */
57
    public function generateKey(User $user = null) : string
58
    {
59
        //If no user was specified, use the currently used one.
60
        if ($user === null) {
61
            $user = $this->security->getUser();
62
        }
63
64
        //If the user is null, then treat it as anonymous user.
65
        //When the anonymous user is passed as user then use this path too.
66
        if ($user === null || $user->getID() === User::ID_ANONYMOUS) {
0 ignored issues
show
Bug introduced by
The method getID() does not exist on Symfony\Component\Security\Core\User\UserInterface. It seems like you code against a sub-type of Symfony\Component\Security\Core\User\UserInterface such as App\Entity\UserSystem\User. ( Ignorable by Annotation )

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

66
        if ($user === null || $user->/** @scrutinizer ignore-call */ getID() === User::ID_ANONYMOUS) {
Loading history...
67
            return 'user$_' . User::ID_ANONYMOUS;
68
        }
69
70
        //In the most cases we can just use the username (its unique)
71
        return "user_" . $user->getUsername();
72
    }
73
}