Completed
Push — master ( e8b7ba...1de272 )
by Phecho
05:41
created

Http/Controllers/Project/ProjectRoleController.php (9 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of Jitamin.
5
 *
6
 * Copyright (C) Jitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Jitamin\Http\Controllers\Project;
13
14
use Jitamin\Foundation\Exceptions\AccessForbiddenException;
15
use Jitamin\Http\Controllers\Controller;
16
17
/**
18
 * Class ProjectRoleController.
19
 */
20
class ProjectRoleController extends Controller
21
{
22
    /**
23
     * Show roles and permissions.
24
     */
25 View Code Duplication
    public function show()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
26
    {
27
        $project = $this->getProject();
28
29
        $this->response->html($this->helper->layout->project('project/role/show', [
30
            'project' => $project,
31
            'roles'   => $this->projectRoleModel->getAllWithRestrictions($project['id']),
32
            'title'   => t('Custom Project Roles'),
33
        ]));
34
    }
35
36
    /**
37
     * Show form to create new role.
38
     *
39
     * @param array $values
40
     * @param array $errors
41
     *
42
     * @throws AccessForbiddenException
43
     */
44 View Code Duplication
    public function create(array $values = [], array $errors = [])
45
    {
46
        $project = $this->getProject();
47
48
        $this->response->html($this->template->render('project/role/create', [
49
            'project' => $project,
50
            'values'  => $values + ['project_id' => $project['id']],
51
            'errors'  => $errors,
52
        ]));
53
    }
54
55
    /**
56
     * Save new role.
57
     */
58
    public function store()
59
    {
60
        $project = $this->getProject();
61
        $values = $this->request->getValues();
62
63
        list($valid, $errors) = $this->projectRoleValidator->validateCreation($values);
64
65
        if ($valid) {
66
            $role_id = $this->projectRoleModel->create($project['id'], $values['role']);
67
68
            if ($role_id !== false) {
69
                $this->flash->success(t('Your custom project role has been created successfully.'));
70
            } else {
71
                $this->flash->failure(t('Unable to create custom project role.'));
72
            }
73
74
            $this->response->redirect($this->helper->url->to('Project/ProjectRoleController', 'show', ['project_id' => $project['id']]));
75
        } else {
76
            $this->create($values, $errors);
77
        }
78
    }
79
80
    /**
81
     * Show form to change existing role.
82
     *
83
     * @param array $values
84
     * @param array $errors
85
     *
86
     * @throws AccessForbiddenException
87
     */
88 View Code Duplication
    public function edit(array $values = [], array $errors = [])
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
    {
90
        $project = $this->getProject();
91
92
        if (empty($values)) {
93
            $values = $role;
0 ignored issues
show
The variable $role does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
94
        }
95
96
        $this->response->html($this->template->render('project/role/edit', [
97
            'role'    => $this->projectRoleModel->getById($project['id'], $this->request->getIntegerParam('role_id')),
0 ignored issues
show
The property projectRoleModel does not exist on object<Jitamin\Http\Cont...\ProjectRoleController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
The property request does not exist on object<Jitamin\Http\Cont...\ProjectRoleController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
98
            'project' => $project,
99
            'values'  => $values,
100
            'errors'  => $errors,
101
        ]));
102
    }
103
104
    /**
105
     * Update role.
106
     */
107
    public function update()
108
    {
109
        $project = $this->getProject();
110
        $role_id = $this->request->getIntegerParam('role_id');
0 ignored issues
show
The property request does not exist on object<Jitamin\Http\Cont...\ProjectRoleController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
111
        $role = $this->projectRoleModel->getById($project['id'], $role_id);
0 ignored issues
show
The property projectRoleModel does not exist on object<Jitamin\Http\Cont...\ProjectRoleController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
112
113
        $values = $this->request->getValues();
114
115
        list($valid, $errors) = $this->projectRoleValidator->validateModification($values);
116
117
        if ($valid) {
118
            if ($this->projectRoleModel->update($role['role_id'], $project['id'], $values['role'])) {
119
                $this->flash->success(t('Your custom project role has been updated successfully.'));
120
            } else {
121
                $this->flash->failure(t('Unable to update custom project role.'));
122
            }
123
124
            $this->response->redirect($this->helper->url->to('Project/ProjectRoleController', 'show', ['project_id' => $project['id']]));
125
        } else {
126
            $this->edit($values, $errors);
127
        }
128
    }
129
130
    /**
131
     * Remove a custom role.
132
     */
133 View Code Duplication
    public function remove()
134
    {
135
        $project = $this->getProject();
136
        $role_id = $this->request->getIntegerParam('role_id');
0 ignored issues
show
The property request does not exist on object<Jitamin\Http\Cont...\ProjectRoleController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
137
138
        if ($this->request->isPost()) {
139
            $this->request->checkCSRFToken();
140
            if ($this->projectRoleModel->remove($project['id'], $role_id)) {
141
                $this->flash->success(t('Custom project role removed successfully.'));
142
            } else {
143
                $this->flash->failure(t('Unable to remove this project role.'));
144
            }
145
146
            return $this->response->redirect($this->helper->url->to('Project/ProjectRoleController', 'show', ['project_id' => $project['id']]));
147
        }
148
149
        $role = $this->projectRoleModel->getById($project['id'], $role_id);
0 ignored issues
show
The property projectRoleModel does not exist on object<Jitamin\Http\Cont...\ProjectRoleController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
150
151
        return $this->response->html($this->helper->layout->project('project/role/remove', [
152
            'project' => $project,
153
            'role'    => $role,
154
        ]));
155
    }
156
}
157