AndVoter::supports()   B
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 22
rs 8.6737
cc 5
eloc 10
nc 8
nop 1
1
<?php
2
3
namespace Vivait\Voter\Voter;
4
5
class AndVoter extends VoterAbstract
6
{
7 View Code Duplication
    public function result($entity)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
8
    {
9
        foreach ($this->conditions as $condition) {
10
            $result = $condition->result($entity);
11
            $this->logger->debug(
12
              sprintf('Condition "%s" returned result: %s', (string)$condition, $result ? 'true' : 'false')
13
            );
14
15
            if ($result == false) {
16
                return false;
17
            }
18
        }
19
20
        return true;
21
    }
22
23
    public function supports($entities)
24
    {
25
        if (!is_array($entities)) {
26
            $entities = [$entities];
27
        }
28
29
        $requirements = $this->getRequirements();
30
31
        // Loop through each requirement
32
        foreach ($requirements as $requirement) {
33
34
            foreach ($entities as $entity) {
35
                if ($entity instanceOf $requirement) {
36
                    continue 2;
37
                }
38
            }
39
40
            return false;
41
        }
42
43
        return true;
44
    }
45
46
    /**
47
     * @return array
48
     */
49
    private function getRequirements()
50
    {
51
        $requirements = [];
52
        foreach ($this->conditions as $condition) {
53
            $requirements = array_merge($requirements, $condition->requires());
54
        }
55
56
        return $requirements;
57
    }
58
}
59