Changes::getPermissionOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Audit\Http\Controllers;
4
5
use Response;
6
use Audit\Models\Change;
7
use Illuminate\Http\Request;
8
9
/**
10
 * A log of model changes, used for auditing Admin activity. Can also be used
11
 * as a source for recovering changed / deleted content.
12
 */
13
class Changes extends Base
14
{
15
    /**
16
     * @var string
17
     */
18
    public $title = 'Changes';
19
20
    /**
21
     * @var string
22
     */
23
    public $description = 'A log of actions that can be used to audit <b>Admin</b> activity or recover content.';
24
25
    /**
26
     * @var array
27
     */
28
    public $columns = [
29
        'Activity' => 'getAdminTitleHtmlAttribute',
30
    ];
31
32
    /**
33
     * Make search options dependent on whether the site is using roles
34
     *
35
     * @return array
36
     */
37
    public function search()
38
    {
39
        $options = [
40
            'model' => [
41
                'label' => __('pedreiro::changes.controller.search.type'),
42
                'type' => 'text',
43
            ],
44
            'key' => [
45
                'label' => __('pedreiro::changes.controller.search.key'),
46
                'type' => 'text',
47
            ],
48
            'action' => [
49
                'label' => __('pedreiro::changes.controller.search.action'),
50
                'type' => 'select',
51
                'options' => 'Audit\Models\Change::getActions()',
52
            ],
53
            'title' => [
54
                'label' => __('pedreiro::changes.controller.search.title'),
55
                'type' => 'text',
56
            ],
57
            'changeable_id' => [
58
                'label' => __('pedreiro::changes.controller.search.admin'),
59
                'type' => 'select',
60
                'options' => 'Audit\Models\Change::getAdmins()',
61
            ],
62
            'created_at' => [
63
                'label' => __('pedreiro::changes.controller.search.date'),
64
                'type' => 'date',
65
            ],
66
        ];
67
68
        return $options;
69
    }
70
71
    /**
72
     * Only reading is possible
73
     *
74
     * @return array An associative array.
75
     */
76
    public function getPermissionOptions()
77
    {
78
        return [
79
            'read' => 'View changes of all content',
80
        ];
81
    }
82
83
    /**
84
     * Customize the edit view to return the changed attributes as JSON. Using
85
     * this method / action so that a new routing rule doesn't need to be created
86
     *
87
     * @param  int $id Model key
88
     * @return Illuminate\Http\Response
89
     */
90
    public function edit(Request $request, $id)
91
    {
92
        $change = Change::findOrFail($id);
93
        $admin = $change->admin;
94
        return Response::json(
95
            [
96
            'action' => __("facilitador::changes.actions.$change->action"),
97
            'title' => $change->title,
98
            'admin' => $admin ? $admin->getAdminTitleHtmlAttribute() : 'someone',
99
            'admin_edit' => $admin ? $admin->getAdminEditAttribute() : null,
100
            'date' => $change->getHumanDateAttribute(),
101
            'attributes' => $change->attributesForModal(),
102
            ]
103
        );
104
    }
105
106
    /**
107
     * Populate protected properties on init
108
     */
109
    public function __construct()
110
    {
111
        $this->title = __('pedreiro::changes.controller.title');
0 ignored issues
show
Documentation Bug introduced by
It seems like __('pedreiro::changes.controller.title') can also be of type object<Illuminate\Contra...Translation\Translator> or array. However, the property $title is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
112
        $this->description = __('pedreiro::changes.controller.description');
0 ignored issues
show
Documentation Bug introduced by
It seems like __('pedreiro::changes.controller.description') can also be of type object<Illuminate\Contra...Translation\Translator> or array. However, the property $description is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
113
        $this->columns = [
114
            __('pedreiro::changes.controller.column.activity') => 'getAdminTitleHtmlAttribute',
115
        ];
116
117
        parent::__construct();
118
    }
119
}
120