Completed
Push — master ( ba8ed9...770316 )
by Jeroen
06:11
created

Helper/Security/Acl/Permission/MaskBuilder.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\AdminBundle\Helper\Security\Acl\Permission;
4
5
use InvalidArgumentException;
6
use Symfony\Component\Security\Acl\Permission\AbstractMaskBuilder;
7
8
/**
9
 * This class allows you to build cumulative permissions easily, or convert
10
 * masks to a human-readable format.
11
 *
12
 * @see Symfony\Component\Security\Acl\Permission\MaskBuilder
13
 */
14
class MaskBuilder extends AbstractMaskBuilder
15
{
16
    const MASK_VIEW = 1;          // 1 << 0
17
    const MASK_EDIT = 4;          // 1 << 2
18
    const MASK_DELETE = 8;          // 1 << 3
19
    const MASK_PUBLISH = 16;         // 1 << 4
20
    const MASK_UNPUBLISH = 32;         // 1 << 5
21
    const MASK_IDDQD = 1073741823; // 1 << 0 | 1 << 1 | ... | 1 << 30
22
23
    const CODE_VIEW = 'V';
24
    const CODE_EDIT = 'E';
25
    const CODE_DELETE = 'D';
26
    const CODE_PUBLISH = 'P';
27
    const CODE_UNPUBLISH = 'U';
28
29
    const ALL_OFF = '................................';
30
    const OFF = '.';
31
    const ON = '*';
32
33
    /**
34
     * Returns a human-readable representation of the permission
35
     *
36
     * @return string
37
     */
38 1
    public function getPattern()
39
    {
40 1
        $pattern = self::ALL_OFF;
41 1
        $length = strlen($pattern);
42 1
        $bitmask = str_pad(decbin($this->mask), $length, '0', STR_PAD_LEFT);
43
44 1
        for ($i = $length - 1; $i >= 0; --$i) {
45 1
            if ('1' === $bitmask[$i]) {
46
                try {
47 1
                    $pattern[$i] = self::getCode(1 << ($length - $i - 1));
48 1
                } catch (\Exception $notPredefined) {
49 1
                    $pattern[$i] = self::ON;
50
                }
51
            }
52
        }
53
54 1
        return $pattern;
55
    }
56
57
    /**
58
     * Returns the code for the passed mask
59
     *
60
     * @param null|int $mask
61
     *
62
     * @throws InvalidArgumentException
63
     * @throws \RuntimeException
64
     *
65
     * @return string
66
     */
67 4
    public static function getCode($mask)
68
    {
69 4
        if (!is_int($mask)) {
70 1
            throw new InvalidArgumentException('$mask must be an integer.');
71
        }
72
73 3
        $reflection = new \ReflectionClass(get_called_class());
74 3
        foreach ($reflection->getConstants() as $name => $cMask) {
75 3
            if (0 !== strpos($name, 'MASK_')) {
76 1
                continue;
77
            }
78
79 3
            if ($mask === $cMask) {
80 3
                if (!defined($cName = 'static::CODE_'.substr($name, 5))) {
81 1
                    throw new \RuntimeException('There was no code defined for this mask.');
82
                }
83
84 2
                return constant($cName);
85
            }
86
        }
87
88 1
        throw new InvalidArgumentException(sprintf('The mask "%d" is not supported.', $mask));
89
    }
90
91
    /**
92
     * Checks if a specific permission or mask value is set in the current mask
93
     *
94
     * @param string|int $mask
95
     *
96
     * @throws InvalidArgumentException
97
     *
98
     * @return bool
99
     */
100 2
    public function has($mask)
101
    {
102 2 View Code Duplication
        if (is_string($mask) && defined($name = 'static::MASK_'.strtoupper($mask))) {
0 ignored issues
show
This code seems to be duplicated across 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...
103 1
            $mask = constant($name);
104 2
        } elseif (!is_int($mask)) {
105 1
            throw new InvalidArgumentException('$mask must be an integer.');
106
        }
107
108 1
        return ($this->mask & $mask) != 0;
109
    }
110
111
    /**
112
     * Returns the mask for the passed code.
113
     *
114
     * @param mixed $code
115
     *
116
     * @return int
117
     *
118
     * @throws \InvalidArgumentException
119
     */
120 10
    public function resolveMask($code)
121
    {
122 10 View Code Duplication
        if (is_string($code)) {
123 8
            if (!defined($name = sprintf('static::MASK_%s', strtoupper($code)))) {
124 1
                throw new \InvalidArgumentException(sprintf('The code "%s" is not supported', $code));
125
            }
126
127 7
            return constant($name);
128
        }
129
130 3
        if (!is_int($code)) {
131 2
            throw new \InvalidArgumentException('$code must be an integer.');
132
        }
133
134 1
        return $code;
135
    }
136
}
137