DefaultController::editAction()   C
last analyzed

Complexity

Conditions 10
Paths 10

Size

Total Lines 119
Code Lines 76

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 76
nc 10
nop 0
dl 0
loc 119
rs 6.6569
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * @copyright   2014 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\CalendarBundle\Controller;
13
14
use Mautic\CoreBundle\Controller\FormController;
15
use Symfony\Component\HttpFoundation\JsonResponse;
16
17
/**
18
 * Class DefaultController.
19
 */
20
class DefaultController extends FormController
0 ignored issues
show
Deprecated Code introduced by
The class Mautic\CoreBundle\Controller\FormController has been deprecated: 2.3 - to be removed in 3.0; use AbstractFormController instead ( Ignorable by Annotation )

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

20
class DefaultController extends /** @scrutinizer ignore-deprecated */ FormController
Loading history...
21
{
22
    /**
23
     * Generates the default view.
24
     *
25
     * @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\Response
26
     */
27
    public function indexAction()
28
    {
29
        return $this->delegateView([
30
            'contentTemplate' => 'MauticCalendarBundle:Default:index.html.php',
31
            'passthroughVars' => [
32
                'activeLink'    => '#mautic_calendar_index',
33
                'mauticContent' => 'calendar',
34
                'route'         => $this->generateUrl('mautic_calendar_index'),
35
            ],
36
        ]);
37
    }
38
39
    /**
40
     * Generates the modal view.
41
     *
42
     * @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\Response
43
     */
44
    public function editAction()
45
    {
46
        $source    = $this->request->query->get('source');
47
        $startDate = new \DateTime($this->request->query->get('startDate'));
48
        $entityId  = $this->request->query->get('objectId');
49
50
        /* @type \Mautic\CalendarBundle\Model\CalendarModel $model */
51
        $calendarModel = $this->getModel('calendar');
52
        $event         = $calendarModel->editCalendarEvent($source, $entityId);
0 ignored issues
show
Bug introduced by
The method editCalendarEvent() does not exist on Mautic\CoreBundle\Model\AbstractCommonModel. It seems like you code against a sub-type of Mautic\CoreBundle\Model\AbstractCommonModel such as Mautic\CalendarBundle\Model\CalendarModel. ( Ignorable by Annotation )

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

52
        /** @scrutinizer ignore-call */ 
53
        $event         = $calendarModel->editCalendarEvent($source, $entityId);
Loading history...
53
54
        $model         = $event->getModel();
55
        $entity        = $event->getEntity();
56
        $session       = $this->get('session');
57
        $sourceSession = $this->get('session')->get('mautic.calendar.'.$source, 1);
58
59
        //set the return URL
60
        $returnUrl = $this->generateUrl('mautic_calendar_index', [$source => $sourceSession]);
61
62
        $postActionVars = [
63
            'returnUrl'       => $returnUrl,
64
            'viewParameters'  => [$source => $sourceSession],
65
            'contentTemplate' => $event->getContentTemplate(),
66
            'passthroughVars' => [
67
                'activeLink'    => 'mautic_calendar_index',
68
                'mauticContent' => $source,
69
            ],
70
        ];
71
72
        //not found
73
        if (null === $entity) {
74
            return $this->postActionRedirect(
75
                array_merge($postActionVars, [
76
                    'flashes' => [
77
                        [
78
                            'type'    => 'error',
79
                            'msg'     => 'mautic.'.$source.'.error.notfound',
80
                            'msgVars' => ['%id%' => $entityId],
81
                        ],
82
                    ],
83
                ])
84
            );
85
        } elseif (!$event->hasAccess()) {
86
            return $this->accessDenied();
87
        } elseif ($model->isLocked($entity)) {
88
            //deny access if the entity is locked
89
            return $this->isLocked($postActionVars, $entity, $source.'.'.$source);
90
        }
91
92
        //Create the form
93
        $action = $this->generateUrl('mautic_calendar_action', [
94
            'objectAction' => 'edit',
95
            'objectId'     => $entity->getId(),
96
            'source'       => $source,
97
            'startDate'    => $startDate->format('Y-m-d H:i:s'),
98
        ]);
99
        $form = $model->createForm($entity, $this->get('form.factory'), $action, ['formName' => $event->getFormName()]);
100
101
        ///Check for a submitted form and process it
102
        if ('POST' == $this->request->getMethod()) {
103
            $valid = false;
104
            if (!$cancelled = $this->isFormCancelled($form)) {
105
                if ($valid = $this->isFormValid($form)) {
106
                    $contentName     = 'mautic.'.$source.'builder.'.$entity->getSessionId().'.content';
107
                    $existingContent = $entity->getContent();
108
                    $newContent      = $session->get($contentName, []);
109
                    $content         = array_merge($existingContent, $newContent);
110
                    $entity->setContent($content);
111
112
                    //form is valid so process the data
113
                    $model->saveEntity($entity, $form->get('buttons')->get('save')->isClicked());
114
115
                    //clear the session
116
                    $session->remove($contentName);
117
118
                    $this->addFlash('mautic.core.notice.updated', [
0 ignored issues
show
Deprecated Code introduced by
The function Mautic\CoreBundle\Contro...nController::addFlash() has been deprecated: Will be removed in Mautic 3.0. Use CommonController::flashBag->addFlash() instead. ( Ignorable by Annotation )

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

118
                    /** @scrutinizer ignore-deprecated */ $this->addFlash('mautic.core.notice.updated', [

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
119
                        '%name%'      => $entity->getTitle(),
120
                        '%menu_link%' => 'mautic_'.$source.'_index',
121
                        '%url%'       => $this->generateUrl('mautic_'.$source.'_action', [
122
                            'objectAction' => 'edit',
123
                            'objectId'     => $entity->getId(),
124
                        ]),
125
                    ]);
126
                }
127
            } else {
128
                //clear any modified content
129
                $session->remove('mautic.'.$source.'builder.'.$entityId.'.content');
130
                //unlock the entity
131
                $model->unlockEntity($entity);
132
            }
133
134
            if ($cancelled || ($valid && $form->get('buttons')->get('save')->isClicked())) {
135
                return new JsonResponse([
136
                    'mauticContent' => 'calendarModal',
137
                    'closeModal'    => 1,
138
                ]);
139
            }
140
        } else {
141
            //lock the entity
142
            $model->lockEntity($entity);
143
        }
144
145
        $builderComponents = $model->getBuilderComponents($entity);
146
147
        return $this->delegateView([
148
            'viewParameters' => [
149
                'form'   => $this->setFormTheme($form, $event->getContentTemplate()),
150
                'tokens' => $builderComponents[$source.'Tokens'],
151
                'entity' => $entity,
152
                'model'  => $model,
153
            ],
154
            'contentTemplate' => $event->getContentTemplate(),
155
            'passthroughVars' => [
156
                'activeLink'    => '#mautic_calendar_index',
157
                'mauticContent' => 'calendarModal',
158
                'route'         => $this->generateUrl('mautic_calendar_action', [
159
                    'objectAction' => 'edit',
160
                    'objectId'     => $entity->getId(),
161
                    'source'       => $source,
162
                    'startDate'    => $startDate->format('Y-m-d H:i:s'),
163
                ]),
164
            ],
165
        ]);
166
    }
167
}
168