Completed
Push — master ( 368645...05fd75 )
by Jan
03:49
created

AttachmentVoter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A voteOnUser() 0 4 2
A supports() 0 7 2
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\Security\Voter;
33
34
35
use App\Entity\Attachment;
36
use App\Entity\User;
37
38
class AttachmentVoter extends ExtendedVoter
39
{
40
41
    /**
42
     * Similar to voteOnAttribute, but checking for the anonymous user is already done.
43
     * The current user (or the anonymous user) is passed by $user.
44
     *
45
     * @param $attribute
46
     * @param $subject
47
     * @param User $user
48
     *
49
     * @return bool
50
     */
51
    protected function voteOnUser($attribute, $subject, User $user): bool
52
    {
53
        if ($subject instanceof Attachment) {
54
            return $this->resolver->inherit($user, 'parts_attachments', $attribute) ?? false;
55
        }
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 53 is false. This is incompatible with the type-hinted return boolean. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
56
    }
57
58
    /**
59
     * Determines if the attribute and subject are supported by this voter.
60
     *
61
     * @param string $attribute An attribute
62
     * @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type
63
     *
64
     * @return bool True if the attribute and subject are supported, false otherwise
65
     */
66
    protected function supports($attribute, $subject)
67
    {
68
        if ($subject instanceof Attachment) {
69
            return in_array($attribute, $this->resolver->listOperationsForPermission('parts_attachments'), false);
70
        }
71
72
        return false;
73
    }
74
}