Completed
Push — master ( 0c847f...8db672 )
by Nikola
04:52 queued 01:06
created

CreateController::handleForm()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 24
Code Lines 11

Duplication

Lines 4
Ratio 16.67 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 4
loc 24
ccs 0
cts 16
cp 0
rs 8.6845
c 0
b 0
f 0
cc 4
eloc 11
nc 5
nop 2
crap 20
1
<?php
2
/*
3
 * This file is part of the Exchange Rate Bundle, an RunOpenCode project.
4
 *
5
 * (c) 2017 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\Bundle\ExchangeRate\Controller;
11
12
use RunOpenCode\Bundle\ExchangeRate\Form\FormType;
13
use RunOpenCode\Bundle\ExchangeRate\Security\AccessVoter;
14
use RunOpenCode\ExchangeRate\Contract\RateInterface;
15
use RunOpenCode\ExchangeRate\Model\Rate;
16
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
17
use Symfony\Component\Form\Form;
18
use Symfony\Component\Form\FormError;
19
use Symfony\Component\HttpFoundation\Request;
20
21
/**
22
 * Class CreateController
23
 *
24
 * @package RunOpenCode\Bundle\ExchangeRate\Controller
25
 */
26
class CreateController extends Controller
27
{
28
    /**
29
     * Main controller action
30
     *
31
     * @param Request $request
32
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
33
     */
34 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...
35
    {
36
        if (!$this->isGranted(AccessVoter::CREATE, RateInterface::class)) {
37
            throw $this->createAccessDeniedException();
38
        }
39
40
        $form = $this->getForm();
41
42
        if (true === $this->handleForm($form, $request)) {
43
            return $this->redirectAfterSuccess();
44
        }
45
46
        return $this->render('@ExchangeRate/create.html.twig', [
47
            'form' => $form->createView(),
48
        ]);
49
    }
50
51
    /**
52
     * Handle form submission.
53
     *
54
     * @param Form $form
55
     * @param Request $request
56
     *
57
     * @return bool TRUE if successful
58
     */
59
    protected function handleForm(Form $form, Request $request)
60
    {
61
        $form->handleRequest($request);
62
63
        if (!$form->isSubmitted()) {
64
            return false;
65
        }
66
67
        /**
68
         * @var Rate $rate
69
         */
70
        $rate = $form->getData()->toRate();
71
72
        if ($this->get('runopencode.exchange_rate.repository')->has($rate->getSourceName(), $rate->getCurrencyCode(), $rate->getDate(), $rate->getRateType())) {
73
            $form->addError(new FormError($this->get('translator')->trans('flash.create.error.exits', [], 'runopencode_exchange_rate'), 'flash.create.error.exits'));
74
        }
75
76 View Code Duplication
        if (!$form->isValid()) {
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...
77
            $this->addFlash('error', $this->get('translator')->trans('flash.form.error', [], 'runopencode_exchange_rate'));
78
            return false;
79
        }
80
81
        return $this->save($rate);
82
    }
83
84
    /**
85
     * Get FQCN of FormType form.
86
     *
87
     * @return string
88
     */
89
    protected function getFormType()
90
    {
91
        return FormType::class;
92
    }
93
94
    /**
95
     * Get form.
96
     *
97
     * @return Form
98
     */
99
    protected function getForm()
100
    {
101
        return $this->createForm($this->getFormType());
102
    }
103
104
    /**
105
     * Save rate.
106
     *
107
     * @param Rate $rate
108
     * @return TRUE if successful.
109
     */
110 View Code Duplication
    protected function save(Rate $rate)
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...
111
    {
112
        try {
113
            $this->get('runopencode.exchange_rate.repository')->save([$rate]);
114
            $this->addFlash('success', $this->get('translator')->trans('flash.create.success', [], 'runopencode_exchange_rate'));
115
            return true;
116
        } catch (\Exception $e) {
117
            $this->addFlash('error', $this->get('translator')->trans('flash.create.error.unknown', [], 'runopencode_exchange_rate'));
118
            return false;
119
        }
120
    }
121
122
    /**
123
     * Redirect after success.
124
     *
125
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
126
     */
127
    protected function redirectAfterSuccess()
128
    {
129
        return $this->redirectToRoute('runopencode_exchange_rate_list');
130
    }
131
}
132