Completed
Push — master ( 8615db...c6d859 )
by Jonathan
02:37
created

Service::getAll()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 9.2
cc 4
eloc 11
nc 4
nop 2
crap 4
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Caridea
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
 * use this file except in compliance with the License. You may obtain a copy of
8
 * the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 * License for the specific language governing permissions and limitations under
16
 * the License.
17
 *
18
 * @copyright 2015-2016 LibreWorks contributors
19
 * @license   http://opensource.org/licenses/Apache-2.0 Apache 2.0 License
20
 */
21
namespace Caridea\Acl;
22
23
/**
24
 * Service for working with permissions.
25
 *
26
 * @copyright 2015-2016 LibreWorks contributors
27
 * @license   http://opensource.org/licenses/Apache-2.0 Apache 2.0 License
28
 */
29
class Service
30
{
31
    /**
32
     * @var Strategy The loading strategy
33
     */
34
    private $strategy;
35
36
    /**
37
     * Creates a new Service.
38
     *
39
     * @param \Caridea\Acl\Strategy $strategy The loading strategy
40
     */
41 9
    public function __construct(Strategy $strategy)
42
    {
43 9
        $this->strategy = $strategy;
44 9
    }
45
46
    /**
47
     * Asserts that one of the provided subjects can verb the Target.
48
     *
49
     * @param Subject[] $subjects An array of `Subject`s
50
     * @param string $verb The verb (e.g. `read`, `create`, `delete`)
51
     * @param \Caridea\Acl\Target $target The target
52
     * @throws Exception\Forbidden if the subject cannot *verb* the Target
53
     */
54 3
    public function assert(array $subjects, string $verb, Target $target)
55
    {
56
        try {
57 3
            if ($this->get($target, $subjects)->can($subjects, $verb)) {
58 2
                return;
59
            }
60 1
        } catch (\Exception $ex) {
61 1
            throw new Exception\Forbidden("Access denied to $verb the target", 0, $ex);
62
        }
63 1
        throw new Exception\Forbidden("Access denied to $verb the target");
64
    }
65
66
    /**
67
     * Whether any of the provided subjects has permission to verb the Target.
68
     *
69
     * @param Subject[] $subjects An array of `Subject`s
70
     * @param string $verb The verb (e.g. `read`, `create`, `delete`)
71
     * @param \Caridea\Acl\Target $target The target
72
     * @return bool Whether one of the subjects can *verb* the provided Target
73
     */
74 2
    public function can(array $subjects, string $verb, Target $target): bool
75
    {
76
        try {
77 2
            return $this->get($target, $subjects)->can($subjects, $verb);
78 1
        } catch (\Exception $ex) {
79
            // just return false below
80
        }
81 1
        return false;
82
    }
83
84
    /**
85
     * Gets an access control list for a Target.
86
     *
87
     * @param Target $target The Target whose ACL will be loaded
88
     * @param Subject[] $subjects An array of `Subject`s
89
     * @return Acl The Acl found
90
     * @throws Exception\Unloadable If the target provided is invalid
91
     * @throws \InvalidArgumentException If the `subjects` argument contains invalid values
92
     */
93 6
    public function get(Target $target, array $subjects): Acl
94
    {
95 6
        return $this->strategy->load($target, $subjects, $this);
96
    }
97
98
    /**
99
     * Gets access control lists for several Targets.
100
     *
101
     * @since 2.1.0
102
     * @param \Caridea\Acl\Target[] $targets The `Target` whose ACL will be loaded
103
     * @param \Caridea\Acl\Subject[] $subjects An array of `Subject`s
104
     * @return array<string,\Caridea\Acl\Acl> The loaded ACLs
105
     * @throws \Caridea\Acl\Exception\Unloadable If the target provided is invalid
106
     * @throws \InvalidArgumentException If the `targets` or `subjects` argument contains invalid values
107
     */
108 3
    public function getAll(array $targets, array $subjects): array
109
    {
110 3
        $acls = [];
111 3
        if ($this->strategy instanceof MultiStrategy) {
112 2
            $acls = $this->strategy->loadAll($targets, $subjects, $this);
113
        } else {
114 1
            foreach ($targets as $target) {
115 1
                $acls[(string)$target] = $this->strategy->load($target, $subjects, $this);
116
            }
117
        }
118
        $missing = array_diff(array_map(function ($a) {return (string) $a;}, $targets), array_keys($acls));
0 ignored issues
show
Coding Style introduced by
Opening brace must be the last content on the line
Loading history...
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
119
        // Check every requested target was found
120 3
        if (!empty($missing)) {
121 1
            throw new Exception\Unloadable("Unable to load ACL for " . implode(", ", $missing));
122
        }
123 2
        return $acls;
124
    }
125
}
126