RoleController::getValueFromArray()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 6
1
<?php
2
3
/**
4
 * This file is part of Webcook security bundle.
5
 *
6
 * See LICENSE file in the root of the bundle. Webcook
7
 */
8
9
namespace Webcook\Cms\SecurityBundle\Controller;
10
11
use Webcook\Cms\CoreBundle\Base\BaseRestController;
12
use Webcook\Cms\SecurityBundle\Entity\Role;
13
use Webcook\Cms\SecurityBundle\Entity\Resource;
14
use Webcook\Cms\SecurityBundle\Entity\RoleResource;
15
use Webcook\Cms\SecurityBundle\Form\Type\RoleType;
16
use Webcook\Cms\SecurityBundle\Form\Type\ResourcesType;
17
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
18
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
19
use Webcook\Cms\SecurityBundle\Authorization\Voter\WebcookCmsVoter;
20
use FOS\RestBundle\Controller\Annotations\Get;
21
use FOS\RestBundle\Controller\Annotations\Post;
22
use FOS\RestBundle\Controller\Annotations\Put;
23
use FOS\RestBundle\Controller\Annotations\Delete;
24
use Doctrine\DBAL\LockMode;
25
26
/**
27
 * REST api controller - role management.
28
 */
29
class RoleController extends BaseRestController
30
{
31
    /**
32
     * Update resources of the role.
33
     *
34
     * @param int $id Id of the desired role.
35
     *
36
     * @ApiDoc(
37
     *  description="Update existing role.",
38
     *  input="Webcook\Cms\SecurityBundle\Form\Type\ResourcesType"
39
     * )
40 1
     * @Put(options={"i18n"=false})
41
     */
42 1
    public function putRolesResourcesAction($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id 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...
43
    {
44 1
        $parameters = $this->getPutParameters('roleResources');
45 1
46
        foreach ($parameters as $roleResourceArray) {
47 1
            $roleResource = $this->getEntityManager()->getRepository('Webcook\Cms\SecurityBundle\Entity\RoleResource')->find($roleResourceArray['id']);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $roleResource is correct as $this->getEntityManager(...oleResourceArray['id']) (which targets Doctrine\Common\Persiste...bjectRepository::find()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
48
49
            if ($roleResource) {
50
                $roleResource->setView($this->getValueFromArray($roleResourceArray, 'view'));
51
                $roleResource->setInsert($this->getValueFromArray($roleResourceArray, 'insert'));
52
                $roleResource->setEdit($this->getValueFromArray($roleResourceArray, 'edit'));
53
                $roleResource->setDelete($this->getValueFromArray($roleResourceArray, 'delete'));
54
            }
55
        }
56
57
        $this->getEntityManager()->flush();
58
59
        $view = $this->view(null, 204);
60
61
        return $this->handleView($view);
62
    }
63 2
64
    /**
65 2
     * Get specific value from an array.
66
     *
67 2
     * @param array  $array array of values
68 2
     * @param string $key   key of desired value
69
     *
70 2
     * @return bool|string Value or false.
71
     */
72
    private function getValueFromArray($array, $key)
73
    {
74
        if (array_key_exists($key, $array)) {
75
            return filter_var($array[$key], FILTER_VALIDATE_BOOLEAN);
76
        }
77
78
        return false;
79
    }
80
}
81