RoleRequestAwareTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 46
ccs 9
cts 9
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setRoleAttribute() 0 5 1
A getRole() 0 10 2
1
<?php
2
/**
3
 * Cheka - Authorization
4
 *
5
 * PHP version 5
6
 *
7
 * Copyright (C) 2016 Jake Johns
8
 *
9
 * This software may be modified and distributed under the terms
10
 * of the MIT license.  See the LICENSE file for details.
11
 *
12
 * @category  Role
13
 * @package   Jnjxp\Cheka
14
 * @author    Jake Johns <[email protected]>
15
 * @copyright 2016 Jake Johns
16
 * @license   http://jnj.mit-license.org/2016 MIT License
17
 * @link      https://github.com/jnjxp/jnjxp.cheka
18
 */
19
20
namespace Jnjxp\Cheka;
21
22
use Zend\Permissions\Acl\Role\RoleInterface;
23
use Psr\Http\Message\ServerRequestInterface as Request;
24
25
/**
26
 * Get Role from Request
27
 *
28
 * @category Role
29
 * @package  Jnjxp\Cheka
30
 * @author   Jake Johns <[email protected]>
31
 * @license  http://jnj.mit-license.org/2016 MIT License
32
 * @link     https://github.com/jnjxp/jnjxp.cheka
33
 */
34
trait RoleRequestAwareTrait
35
{
36
    /**
37
     * Attribute on which Role is stored in Request
38
     *
39
     * @var string
40
     *
41
     * @access protected
42
     */
43
    protected $roleAttribute = 'jnjxp/cheka:role';
44
45
    /**
46
     * Set attribute on which to store role
47
     *
48
     * @param string $attr name of attribute
49
     *
50
     * @return $this
51
     *
52
     * @access public
53
     */
54 10
    public function setRoleAttribute($attr)
55
    {
56 10
        $this->roleAttribute = $attr;
57 10
        return $this;
58
    }
59
60
    /**
61
     * Get role
62
     *
63
     * @param Request $request PSR7 Request
64
     *
65
     * @return RoleInterface
66
     *
67
     * @access protected
68
     */
69 6
    protected function getRole(Request $request)
70
    {
71 6
        $role = $request->getAttribute($this->roleAttribute);
72 6
        if (! $role instanceof RoleInterface) {
73 1
            throw new \InvalidArgumentException(
74
                'Role attribute not available in request'
75 1
            );
76
        }
77 5
        return $role;
78
    }
79
}
80