Completed
Push — master ( d34cec...faecd8 )
by Augusto
02:22
created

AbstractCallbackMediator::mediate()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 20
ccs 14
cts 14
cp 1
rs 8.8571
cc 6
eloc 12
nc 6
nop 4
crap 6
1
<?php
2
/*
3
 * This file is part of the Respect\Rest package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Respect\Rest\Routines;
10
11
use Respect\Rest\Request;
12
use UnexpectedValueException;
13
14
/**
15
 * Mediates the callback selection process when choosing the appropriate
16
 * callback based on the request. Decisions are deligated to the implementation
17
 * classes.
18
 * @author Nick Lombard <[email protected]>
19
 */
20
abstract class AbstractCallbackMediator extends AbstractCallbackList implements ProxyableWhen
21
{
22
    /** Implementations are tasked to provide a list of appropriate request identifiers */
23
    abstract protected function identifyRequested(Request $request, $params);
24
    /** Based on each of the identified particulars a list of provisiens must be supplied */
25
    abstract protected function considerProvisions($requested);
26
    /** If an agreement wasreached to allow positive notification and preparation */
27
    abstract protected function notifyApproved($requested, $provided, Request $request, $params);
28
    /** If declined to apply the nescessary notifications and preparations */
29
    abstract protected function notifyDeclined($requested, $provided, Request $request, $params);
30
31
    /**
32
     * Mediate the authorization or ultimately service denial based on client's
33
     * request and implementation's provisioning.
34
     * The outcome will trigger the appropriate notification mehtods to allow
35
     * for appropriate header configuration if nescesary.
36
     */
37 40
    public function when(Request $request, $params)
38
    {
39 40
        if (true ==
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
40 40
        ($decision = $this->mediate($requested, $provided, $request, $params))) {
41 39
            $this->notifyApproved($requested, $provided, $request, $params);
42 39
        } else {
43 2
            $this->notifyDeclined($requested, $provided, $request, $params);
44
        }
45
46 40
        return $decision;
47
    }
48
49
    /**
50
     * Implementing classes will be asked to identify the request list and for
51
     * each item in the list of requests identified a list of provisions must be
52
     * supplied.
53
     * The implementation gets to authorize against each possibility or the
54
     * request will be declined if no satisfactory requirements are reached.
55
     **/
56 40
    private function mediate(&$requested, &$provided, Request $request, $params)
0 ignored issues
show
Unused Code introduced by
The parameter $requested is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $provided is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
57
    {
58 40
        if (is_array($requests = $this->identifyRequested($request, $params))) {
59 40
            foreach ($requests as $requested) {
60 40
                if (is_array($provisions = $this->considerProvisions($requested))) {
61 40
                    foreach ($provisions as $provided) {
62 40
                        if ($this->authorize($requested, $provided, $request)) {
0 ignored issues
show
Unused Code introduced by
The call to AbstractCallbackMediator::authorize() has too many arguments starting with $request.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
63 39
                            return true;
64
                        }
65 8
                    }
66 2
                } else {
67 1
                    throw new UnexpectedValueException('Provisions must be an array of 0 to many.');
68
                }
69 2
            }
70 2
        } else {
71 1
            throw new UnexpectedValueException('Requests must be an array of 0 to many.');
72
        }
73
74 2
        return false;
75
    }
76
77
    /** Affirm apprval or decline the r */
78 11
    protected function authorize($requested, $provided)
79
    {
80 11
        return $requested == $provided;
81
    }
82
}
83