Completed
Push — master ( efa664...ac0835 )
by Joachim
15:34
created

SiteSettingController::showAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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\Form;
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
    public function indexAction(Request $request)
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 Form           $form
95
     * @param SiteSetting $siteSetting
96
     * @param Request        $request
97
     *
98
     * @return null|RedirectResponse
99
     */
100
    private function handleUpdate(Form $form, SiteSetting $siteSetting, Request $request)
101
    {
102
        $form->handleRequest($request);
103
104
        if ($form->isSubmitted() && $form->isValid()) {
105
106
            $em = $this->getDoctrine()->getManager();
107
            $em->persist($siteSetting);
108
            $em->flush();
109
110
            $translator = $this->get('translator');
111
112
            $this->addFlash('success', $translator->trans('site_setting.edit.updated', [], 'LoevgaardDandomainAltapayBundle'));
113
114
            return $this->redirectToRoute('loevgaard_dandomain_altapay_site_setting_edit', [
115
                'id' => $siteSetting->getId(),
116
            ]);
117
        }
118
119
        return null;
120
    }
121
122
    /**
123
     * @param SiteSetting $siteSetting
124
     * @param Form           $form
125
     *
126
     * @return Response
127
     */
128
    private function updateResponse(SiteSetting $siteSetting, Form $form): Response
129
    {
130
        return $this->render('@LoevgaardDandomainAltapay/site_setting/edit.html.twig', [
131
            'siteSetting' => $siteSetting,
132
            'form' => $form->createView(),
133
        ]);
134
    }
135
136
    /**
137
     * @param SiteSetting $siteSetting
138
     *
139
     * @return Form
140
     */
141
    private function getForm(SiteSetting $siteSetting): Form
142
    {
143
        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...
144
    }
145
}
146