Guard::authorize()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 3
eloc 4
nc 3
nop 2
1
<?php
2
3
namespace EloquentJs\Query\Guard;
4
5
use EloquentJs\Query\Guard\Policy\Policy;
6
use EloquentJs\Query\MethodCall;
7
use EloquentJs\Query\Query;
8
9
class Guard
10
{
11
    /**
12
     * Authorize a query according to the given policy.
13
     *
14
     * @param Query $query
15
     * @param Policy $policy
16
     * @throws NotPermittedException
17
     */
18
    public function authorize(Query $query, Policy $policy)
19
    {
20
        foreach ($query->calls as $call) {
21
            if ( ! $this->authorizeCall($call, $policy)) {
22
                throw new NotPermittedException($call->method);
23
            }
24
        }
25
    }
26
27
    /**
28
     * Authorize a method call according to the given policy.
29
     *
30
     * @param MethodCall $call
31
     * @param Policy $policy
32
     * @return bool
33
     */
34
    protected function authorizeCall(MethodCall $call, Policy $policy)
35
    {
36
        return $policy->test($call);
37
    }
38
}
39