Completed
Push — master ( 9e1211...19df0f )
by Benjamin
02:32
created

AdminBlockController::findContent()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 31
rs 8.439
cc 6
eloc 19
nc 8
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A AdminBlockController::getInstancesAdmin() 6 11 4
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 = 'alpixel_cms.blocks';
13
    private $_cmsContentParameter = null;
14
    private $_blockDefaultClass = 'Alpixel\Bundle\CMSBundle\Entity\Block';
0 ignored issues
show
Unused Code introduced by
The property $_blockDefaultClass is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
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
        $instanceAdmin = $this->admin->getConfigurationPool()->getAdminByClass(get_class($object));
24
        if ($instanceAdmin !== null) {
25
            return $this->redirect($instanceAdmin->generateUrl('edit', ['id' => $object->getId()]));
26
        }
27
28
        throw new NotFoundHttpException(sprintf('unable to find a class admin for the %s class', get_class($content)));
29
    }
30
31
    public function listAction(Request $request = null)
32
    {
33
        if (false === $this->admin->isGranted('LIST')) {
34
            throw new AccessDeniedException();
35
        }
36
37
        $this->getInstancesAdmin();
38
        // set the theme for the current Admin Form
39
        $datagrid = $this->admin->getDatagrid();
40
        $formView = $datagrid->getForm()->createView();
41
        $this->get('twig')->getExtension('form')->renderer->setTheme($formView, $this->admin->getFilterTheme());
42
43
        return $this->render($this->admin->getTemplate('list'), [
44
            'action'         => 'list',
45
            'cmsContentType' => $this->getCMSParameter(),
46
            'form'           => $formView,
47
            'datagrid'       => $datagrid,
48
            'csrf_token'     => $this->getCsrfToken('sonata.batch'),
49
        ], null, $request);
50
    }
51
52
    /**
53
     *  Set instances of admin in $_cmsContentParameter.
54
     */
55
    protected function getInstancesAdmin()
56
    {
57
        foreach ($this->getCMSParameter() as $key => $value) {
58 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...
59
                $instanceAdmin = $this->admin->getConfigurationPool()->getAdminByClass($value['class']);
60
                if ($instanceAdmin !== null) {
61
                    $this->_cmsContentParameter[$key]['admin'] = $instanceAdmin;
62
                }
63
            }
64
        }
65
    }
66
67
    /**
68
     * @param $role array Role in cms.yml
69
     *
70
     * Check role define in different parameter of block or content_types cms.yml
71
     */
72 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...
73
    {
74
        $user = $this->getUser();
75
76
        if (!$user) {
77
            throw new NotFoundHttpException(sprintf('unable to find user'));
78
        }
79
80
        if (!array_key_exists('role', $role) || in_array($user->getRoles()[0], $role['role'])) {
81
            return true;
82
        }
83
84
        return false;
85
    }
86
87
    /**
88
     * Get content in cms.yml and set in $_cmsParameter.
89
     *
90
     * @return content of cms.yml
91
     */
92
    protected function getCMSParameter()
93
    {
94
        if ($this->_cmsContentParameter !== null) {
95
            return $this->_cmsContentParameter;
96
        }
97
98
        $this->_cmsContentParameter = $this->container->getParameter($this->_cmsParameter);
99
100
        return $this->_cmsContentParameter;
101
    }
102
}
103