SiteSettingController::handleUpdate()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 2
nop 3
1
<?php
2
3
namespace Loevgaard\DandomainAltapayBundle\Controller;
4
5
use Loevgaard\DandomainAltapayBundle\Entity\SiteSetting;
6
use Loevgaard\DandomainAltapayBundle\Form\SiteSettingType;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
9
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
10
use Symfony\Component\Form\FormInterface;
11
use Symfony\Component\HttpFoundation\RedirectResponse;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
15
/**
16
 * @Route("/site-setting")
17
 */
18
class SiteSettingController extends Controller
19
{
20
    /**
21
     * @Method("GET")
22
     * @Route("", name="loevgaard_dandomain_altapay_site_setting_index")
23
     *
24
     * @param Request $request
25
     *
26
     * @return Response
27
     */
28 View Code Duplication
    public function indexAction(Request $request)
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...
29
    {
30
        $repos = $this->get('loevgaard_dandomain_altapay.site_setting_repository');
31
        $siteSettings = $repos->findAllWithPaging($request->query->getInt('page', 1));
32
33
        return $this->render('@LoevgaardDandomainAltapay/site_setting/index.html.twig', [
34
            'siteSettings' => $siteSettings,
35
        ]);
36
    }
37
38
    /**
39
     * @Method("GET")
40
     * @Route("/{id}/show", name="loevgaard_dandomain_altapay_site_setting_show")
41
     *
42
     * @param SiteSetting $siteSetting
43
     *
44
     * @return Response
45
     */
46
    public function showAction(SiteSetting $siteSetting)
47
    {
48
        return $this->render('@LoevgaardDandomainAltapay/site_setting/show.html.twig', [
49
            'siteSetting' => $siteSetting,
50
        ]);
51
    }
52
53
    /**
54
     * @Method({"GET", "POST"})
55
     * @Route("/new", name="loevgaard_dandomain_altapay_site_setting_new")
56
     *
57
     * @param Request $request
58
     *
59
     * @return Response
60
     */
61 View Code Duplication
    public function newAction(Request $request)
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...
62
    {
63
        $siteSetting = new SiteSetting();
64
        $form = $this->getForm($siteSetting);
65
        $res = $this->handleUpdate($form, $siteSetting, $request);
66
        if ($res) {
67
            return $res;
68
        }
69
70
        return $this->updateResponse($siteSetting, $form);
71
    }
72
73
    /**
74
     * @Method({"GET", "POST"})
75
     * @Route("/{id}/edit", name="loevgaard_dandomain_altapay_site_setting_edit")
76
     *
77
     * @param SiteSetting $siteSetting
78
     * @param Request     $request
79
     *
80
     * @return Response
81
     */
82 View Code Duplication
    public function editAction(SiteSetting $siteSetting, Request $request)
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...
83
    {
84
        $form = $this->getForm($siteSetting);
85
        $res = $this->handleUpdate($form, $siteSetting, $request);
86
        if ($res) {
87
            return $res;
88
        }
89
90
        return $this->updateResponse($siteSetting, $form);
91
    }
92
93
    /**
94
     * @param FormInterface $form
95
     * @param SiteSetting   $siteSetting
96
     * @param Request       $request
97
     *
98
     * @return null|RedirectResponse
99
     */
100
    private function handleUpdate(FormInterface $form, SiteSetting $siteSetting, Request $request)
101
    {
102
        $form->handleRequest($request);
103
104
        if ($form->isSubmitted() && $form->isValid()) {
105
            $em = $this->getDoctrine()->getManager();
106
            $em->persist($siteSetting);
107
            $em->flush();
108
109
            $translator = $this->get('translator');
110
111
            $this->addFlash('success', $translator->trans('site_setting.edit.updated', [], 'LoevgaardDandomainAltapayBundle'));
112
113
            return $this->redirectToRoute('loevgaard_dandomain_altapay_site_setting_edit', [
114
                'id' => $siteSetting->getId(),
115
            ]);
116
        }
117
118
        return null;
119
    }
120
121
    /**
122
     * @param SiteSetting   $siteSetting
123
     * @param FormInterface $form
124
     *
125
     * @return Response
126
     */
127
    private function updateResponse(SiteSetting $siteSetting, FormInterface $form): Response
128
    {
129
        return $this->render('@LoevgaardDandomainAltapay/site_setting/edit.html.twig', [
130
            'siteSetting' => $siteSetting,
131
            'form' => $form->createView(),
132
        ]);
133
    }
134
135
    /**
136
     * @param SiteSetting $siteSetting
137
     *
138
     * @return FormInterface
139
     */
140
    private function getForm(SiteSetting $siteSetting): FormInterface
141
    {
142
        return $form = $this->createForm(SiteSettingType::class, $siteSetting);
0 ignored issues
show
Unused Code introduced by
$form 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...
143
    }
144
}
145