Completed
Push — master ( c02aa8...d4d075 )
by Benjamin
36:25 queued 06:32
created

AdminNodeController::findContentFromNode()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 17
rs 9.4285
cc 3
eloc 10
nc 3
nop 1
1
<?php
2
3
namespace Alpixel\Bundle\CMSBundle\Controller;
4
5
use Alpixel\Bundle\CMSBundle\Entity\Node;
6
use Sonata\AdminBundle\Controller\CRUDController as Controller;
7
use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
10
11
class AdminNodeController extends Controller
12
{
13
    public function editContentAction()
14
    {
15
        $object = $this->admin->getSubject();
16
17
        if (!$object) {
18
            throw new NotFoundHttpException(sprintf('unable to find the object'));
19
        }
20
21
        $content = $this->get('cms.helper')->getNodeElementEntityFromNode($object);
22
23 View Code Duplication
        if ($content !== null) {
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...
24
            $instanceAdmin = $this->admin->getConfigurationPool()->getAdminByClass(get_class($content));
25
26
            if ($instanceAdmin !== null) {
27
                return $this->redirect($instanceAdmin->generateUrl('edit', ['id' => $content->getId()]));
28
            }
29
        }
30
31
        throw new NotFoundHttpException(sprintf('unable to find a class admin for the %s class', $contentType['class']));
32
    }
33
34
    public function createTranslationAction(Request $request)
35
    {
36
        $object = $this->admin->getSubject();
37
        $locale = $request->query->get('locale');
38
39
        if ($locale === null || $object === null) {
40
            return $this->createNotFoundException();
41
        }
42
43
        $entityManager = $this->get('doctrine.orm.entity_manager');
44
        $translation = $entityManager->getRepository('CMSBundle:Node')
45
                                     ->findTranslation($object, $locale);
46
47
        if ($translation !== null) {
48
            return $this->redirect($this->admin->generateUrl('editContent', ['id' => $translation->getId()]));
49
        } else {
50
            $translatedContent = $this->get('cms.helper')->createTranslation($object, $locale);
51
            $entityManager->persist($translatedContent);
52
            $entityManager->persist($translatedContent->getNode());
53
            $entityManager->flush();
54
            return $this->redirect($this->admin->generateUrl('editContent', ['id' => $translatedContent->getNode()->getId()]));
55
        }
56
    }
57
58
    public function listAction(Request $request = null)
59
    {
60
        if (false === $this->admin->isGranted('LIST')) {
61
            throw new AccessDeniedException();
0 ignored issues
show
Bug introduced by
The call to AccessDeniedException::__construct() misses a required argument $path.

This check looks for function calls that miss required arguments.

Loading history...
62
        }
63
64
        $datagrid = $this->admin->getDatagrid();
65
        $formView = $datagrid->getForm()->createView();
66
67
        if (!$this->container->hasParameter('cms.content_types')) {
68
            throw $this->createNotFoundException('cms.content_types parameters has not been  not found, maybe you must be configured cms.yml file');
69
        }
70
71
        $cmsContentType = $this->container->getParameter('cms.content_types');
72
73
        foreach ($cmsContentType as $key => $value) {
74 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...
75
                if ($instanceAdmin = $this->admin->getConfigurationPool()->getAdminByClass($value['class'])) {
76
                    $cmsContentType[$key]['admin'] = $instanceAdmin;
77
                }
78
            }
79
        }
80
81
        // set the theme for the current Admin Form
82
        $this->get('twig')->getExtension('form')->renderer->setTheme($formView, $this->admin->getFilterTheme());
83
84
        return $this->render($this->admin->getTemplate('list'), [
85
            'action'         => 'list',
86
            'cmsContentType' => $cmsContentType,
87
            'form'           => $formView,
88
            'datagrid'       => $datagrid,
89
            'csrf_token'     => $this->getCsrfToken('sonata.batch'),
90
        ], null, $request);
91
    }
92
93 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...
94
    {
95
        $user = $this->getUser();
96
97
        if (!$user) {
98
            throw new NotFoundHttpException(sprintf('unable to find user'));
99
        }
100
101
        $userRole = $user->getRoles()[0];
102
103
        if (!array_key_exists('role', $role) || in_array($userRole, $role['role'])) {
104
            return true;
105
        }
106
107
        return false;
108
    }
109
110
}
111