Completed
Push — master ( 8fc13e...c19aa3 )
by Axel
06:21
created

RegistryController::editAction()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 16
nc 8
nop 5
dl 0
loc 30
rs 9.1111
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula Foundation - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\CategoriesModule\Controller;
15
16
use Doctrine\ORM\EntityManagerInterface;
17
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
18
use Symfony\Component\HttpFoundation\RedirectResponse;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\Routing\Annotation\Route;
21
use Zikula\Bundle\CoreBundle\Controller\AbstractController;
22
use Zikula\Bundle\FormExtensionBundle\Form\Type\DeletionType;
23
use Zikula\CategoriesModule\Entity\CategoryRegistryEntity;
24
use Zikula\CategoriesModule\Entity\RepositoryInterface\CategoryRegistryRepositoryInterface;
25
use Zikula\CategoriesModule\Form\Type\CategoryRegistryType;
26
use Zikula\ExtensionsModule\Api\ApiInterface\CapabilityApiInterface;
27
use Zikula\ExtensionsModule\Api\CapabilityApi;
28
use Zikula\PermissionsModule\Annotation\PermissionCheck;
29
use Zikula\ThemeModule\Engine\Annotation\Theme;
30
31
/**
32
 * Controller for handling category registries.
33
 *
34
 * @Route("/registry")
35
 * @PermissionCheck("admin")
36
 */
37
class RegistryController extends AbstractController
38
{
39
    /**
40
     * @Route("/edit/{id}", requirements={"id" = "^[1-9]\d*$"}, defaults={"id" = null})
41
     * @Theme("admin")
42
     * @Template("@ZikulaCategoriesModule/Registry/edit.html.twig")
43
     *
44
     * Creates or edits a category registry.
45
     *
46
     * @return array|RedirectResponse
47
     */
48
    public function editAction(
49
        Request $request,
50
        EntityManagerInterface $entityManager,
51
        CapabilityApiInterface $capabilityApi,
52
        CategoryRegistryRepositoryInterface $registryRepository,
53
        CategoryRegistryEntity $registryEntity = null
54
    ) {
55
        if (null === $registryEntity) {
56
            $registryEntity = new CategoryRegistryEntity();
57
        }
58
59
        $form = $this->createForm(CategoryRegistryType::class, $registryEntity, [
60
            'categorizableModules' => $this->getCategorizableModules($capabilityApi)
61
        ]);
62
        $form->handleRequest($request);
63
        if ($form->isSubmitted() && $form->isValid()) {
64
            if ($form->get('save')->isClicked()) {
0 ignored issues
show
Bug introduced by
The method isClicked() does not exist on Symfony\Component\Form\FormInterface. It seems like you code against a sub-type of Symfony\Component\Form\FormInterface such as Symfony\Component\Form\SubmitButton. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
            if ($form->get('save')->/** @scrutinizer ignore-call */ isClicked()) {
Loading history...
65
                $entityManager->persist($registryEntity);
66
                $entityManager->flush();
67
                $this->addFlash('success', 'Done! Registry updated.');
68
            } elseif ($form->get('cancel')->isClicked()) {
69
                $this->addFlash('status', 'Operation cancelled.');
70
            }
71
72
            return $this->redirectToRoute('zikulacategoriesmodule_registry_edit');
73
        }
74
75
        return [
76
            'form' => $form->createView(),
77
            'registries' => $registryRepository->findAll()
78
        ];
79
    }
80
81
    private function getCategorizableModules(CapabilityApiInterface $capabilityApi): array
82
    {
83
        $modules = $capabilityApi->getExtensionsCapableOf(CapabilityApi::CATEGORIZABLE);
84
        $moduleOptions = [];
85
        foreach ($modules as $module) {
86
            $moduleName = $module->getName();
87
            $moduleOptions[$moduleName] = $moduleName;
88
        }
89
90
        return $moduleOptions;
91
    }
92
93
    /**
94
     * @Route("/delete/{id}", requirements={"id" = "^[1-9]\d*$"})
95
     * @Theme("admin")
96
     * @Template("@ZikulaCategoriesModule/Registry/delete.html.twig")
97
     *
98
     * Deletes a category registry.
99
     *
100
     * @return array|RedirectResponse
101
     */
102
    public function deleteAction(
103
        Request $request,
104
        EntityManagerInterface $entityManager,
105
        CategoryRegistryEntity $registry
106
    ) {
107
        $form = $this->createForm(DeletionType::class);
108
        $form->handleRequest($request);
109
        if ($form->isSubmitted() && $form->isValid()) {
110
            if ($form->get('delete')->isClicked()) {
111
                $entityManager->remove($registry);
112
                $entityManager->flush();
113
                $this->addFlash('success', 'Done! Registry entry deleted.');
114
            } elseif ($form->get('cancel')->isClicked()) {
115
                $this->addFlash('status', 'Operation cancelled.');
116
            }
117
118
            return $this->redirectToRoute('zikulacategoriesmodule_registry_edit');
119
        }
120
121
        return [
122
            'form' => $form->createView(),
123
            'registry' => $registry
124
        ];
125
    }
126
}
127