Completed
Push — master ( ba6a60...9d0a78 )
by Benjamin
50:48 queued 40:24
created

AdminBlockController::getInstancesAdmin()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 6
Ratio 54.55 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 6
loc 11
rs 9.2
cc 4
eloc 6
nc 4
nop 0
1
<?php
2
3
namespace Alpixel\Bundle\CMSBundle\Controller;
4
5
use Sonata\AdminBundle\Controller\CRUDController as Controller;
6
use Symfony\Component\HttpFoundation\Request;
7
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
8
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
9
10
class AdminBlockController extends Controller
11
{
12
    private $_cmsParameter = 'cms.blocks';
13
    private $_cmsContentParameter = null;
14
    private $_blockDefaultClass = 'Alpixel\Bundle\CMSBundle\Entity\Block';
15
16 View Code Duplication
    public function editContentAction()
0 ignored issues
show
Duplication introduced by
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...
17
    {
18
        $object = $this->admin->getSubject();
19
        if (!$object) {
20
            throw new NotFoundHttpException(sprintf('unable to find the object'));
21
        }
22
23
        $content = $this->findContent();
24
25
        if ($content !== null) {
26
            $instanceAdmin = $this->admin->getConfigurationPool()->getAdminByClass($className);
0 ignored issues
show
Bug introduced by
The variable $className 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...
27
28
            if ($instanceAdmin !== null) {
29
                return $this->redirect($instanceAdmin->generateUrl('edit', ['id' => $content->getId()]));
30
            }
31
        }
32
33
        throw new NotFoundHttpException(sprintf('unable to find a class admin for the %s class', get_class($content)));
34
    }
35
36
    private function findContent()
37
    {
38
        $classPassed = [];
39
        $className = '';
0 ignored issues
show
Unused Code introduced by
$className is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
40
        $content = null;
41
        $entityManager = $this->get('doctrine.orm.entity_manager');
42
        foreach ($this->getCMSParameter() as $key => $value) {
43
            if ($this->_blockDefaultClass === $value['class'] || in_array($value['class'], $classPassed)) {
44
                continue;
45
            }
46
47
            $repository = $entityManager->getRepository($value['class']);
48
            $content = $repository->findOneByBlock($object->getId());
0 ignored issues
show
Bug introduced by
The variable $object 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...
49
50
            if ($content !== null) {
51
                $className = $value['class'];
0 ignored issues
show
Unused Code introduced by
$className is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
52
                break;
53
            }
54
55
            $classPassed[] = $value['class'];
56
        }
57
58
        if ($content === null) {
59
            $repository = $entityManager->getRepository($this->_blockDefaultClass);
60
            $content = $repository->findOneById($object);
61
            $className = $this->_blockDefaultClass;
0 ignored issues
show
Unused Code introduced by
$className is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
62
        }
63
        return $content;
64
    }
65
66
    public function listAction(Request $request = null)
67
    {
68
        if (false === $this->admin->isGranted('LIST')) {
69
            throw new AccessDeniedException();
70
        }
71
72
        $this->getInstancesAdmin();
73
        // set the theme for the current Admin Form
74
        $datagrid = $this->admin->getDatagrid();
75
        $formView = $datagrid->getForm()->createView();
76
        $this->get('twig')->getExtension('form')->renderer->setTheme($formView, $this->admin->getFilterTheme());
77
78
        return $this->render($this->admin->getTemplate('list'), [
79
            'action'         => 'list',
80
            'cmsContentType' => $this->getCMSParameter(),
81
            'form'           => $formView,
82
            'datagrid'       => $datagrid,
83
            'csrf_token'     => $this->getCsrfToken('sonata.batch'),
84
        ], null, $request);
85
    }
86
87
    /**
88
     *  Set instances of admin in $_cmsContentParameter.
89
     */
90
    protected function getInstancesAdmin()
91
    {
92
        foreach ($this->getCMSParameter() as $key => $value) {
93 View Code Duplication
            if ($this->checkRolesCMS($value)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
94
                $instanceAdmin = $this->admin->getConfigurationPool()->getAdminByClass($value['class']);
95
                if ($instanceAdmin !== null) {
96
                    $this->_cmsContentParameter[$key]['admin'] = $instanceAdmin;
97
                }
98
            }
99
        }
100
    }
101
102
    /**
103
     * @param $role array Role in cms.yml
104
     *
105
     * Check role define in different parameter of block or content_types cms.yml
106
     */
107 View Code Duplication
    protected function checkRolesCMS(array $role)
0 ignored issues
show
Duplication introduced by
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...
108
    {
109
        $user = $this->getUser();
110
111
        if (!$user) {
112
            throw new NotFoundHttpException(sprintf('unable to find user'));
113
        }
114
115
        if (!array_key_exists('role', $role) || in_array($user->getRoles()[0], $role['role'])) {
116
            return true;
117
        }
118
119
        return false;
120
    }
121
122
    /**
123
     * Get content in cms.yml and set in $_cmsParameter.
124
     *
125
     * @return content of cms.yml
126
     */
127
    protected function getCMSParameter()
128
    {
129
        if ($this->_cmsContentParameter !== null) {
130
            return $this->_cmsContentParameter;
131
        }
132
133
        $this->_cmsContentParameter = $this->container->getParameter($this->_cmsParameter);
134
135
        return $this->_cmsContentParameter;
136
    }
137
}
138