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

EditController::save()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 11
loc 11
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 3
nop 1
crap 6
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\Dto\Rate as DtoRate;
13
use RunOpenCode\Bundle\ExchangeRate\Form\FormType;
14
use RunOpenCode\Bundle\ExchangeRate\Security\AccessVoter;
15
use RunOpenCode\ExchangeRate\Contract\RateInterface;
16
use RunOpenCode\ExchangeRate\Contract\RepositoryInterface;
17
use RunOpenCode\ExchangeRate\Model\Rate;
18
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
19
use Symfony\Component\Form\Form;
20
use Symfony\Component\HttpFoundation\Request;
21
22
/**
23
 * Class EditController
24
 *
25
 * @package RunOpenCode\Bundle\ExchangeRate\Controller
26
 */
27
class EditController extends Controller
28
{
29
    /**
30
     * Main controller action
31
     *
32
     * @param Request $request
33
     * @param $source
34
     * @param $rateType
35
     * @param $currencyCode
36
     * @param \DateTime $date
37
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response|\Symfony\Component\HttpKernel\Exception\NotFoundHttpException
38
     */
39
    public function indexAction(Request $request, $source, $rateType, $currencyCode, \DateTime $date)
40
    {
41
        /**
42
         * @var RepositoryInterface $repository
43
         */
44
        $repository = $this->get('runopencode.exchange_rate.repository');
45
46
        if ($repository->has($source, $currencyCode, $date, $rateType)) {
47
            return $this->createNotFoundException();
48
        }
49
50
        $rate = $repository->get($source, $currencyCode, $date, $rateType);
51
52
        if (!$this->isGranted(AccessVoter::EDIT, $rate)) {
53
            throw $this->createAccessDeniedException();
54
        }
55
56
        $form = $this->getForm($rate);
57
58
        if (true === $this->handleForm($form, $request, $rate)) {
59
            return $this->redirectAfterSuccess();
60
        }
61
62
        return $this->render('@ExchangeRate/edit.html.twig', [
63
            'form' => $form->createView(),
64
        ]);
65
    }
66
67
    /**
68
     * Handle form submission.
69
     *
70
     * @param Form $form
71
     * @param Request $request
72
     * @param RateInterface $rate
0 ignored issues
show
Bug introduced by
There is no parameter named $rate. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
73
     *
74
     * @return bool TRUE if successful
75
     */
76
    protected function handleForm(Form $form, Request $request, RateInterface $exchangeRate)
77
    {
78
        $form->handleRequest($request);
79
80
        if (!$form->isSubmitted()) {
81
            return false;
82
        }
83
84
        /**
85
         * @var Rate $rate
86
         */
87
        $rate = $form->getData()->toRate($exchangeRate);
88
89 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...
90
            $this->addFlash('error', $this->get('translator')->trans('flash.form.error', [], 'runopencode_exchange_rate'));
91
            return false;
92
        }
93
94
        return $this->save($rate);
95
    }
96
97
    /**
98
     * Get FQCN of FormType form.
99
     *
100
     * @return string
101
     */
102
    protected function getFormType()
103
    {
104
        return FormType::class;
105
    }
106
107
    /**
108
     * Get form.
109
     *
110
     * @return Form
111
     */
112
    protected function getForm(RateInterface $rate)
113
    {
114
        return $this->createForm($this->getFormType(), DtoRate::fromRateInterface($rate));
115
    }
116
117
    /**
118
     * Save rate.
119
     *
120
     * @param Rate $rate
121
     * @return TRUE if successful.
122
     */
123 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...
124
    {
125
        try {
126
            $this->get('runopencode.exchange_rate.repository')->save([$rate]);
127
            $this->addFlash('success', $this->get('translator')->trans('flash.edit.success', [], 'runopencode_exchange_rate'));
128
            return true;
129
        } catch (\Exception $e) {
130
            $this->addFlash('error', $this->get('translator')->trans('flash.edit.error.unknown', [], 'runopencode_exchange_rate'));
131
            return false;
132
        }
133
    }
134
135
    /**
136
     * Redirect after success.
137
     *
138
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
139
     */
140
    protected function redirectAfterSuccess()
141
    {
142
        return $this->redirectToRoute('runopencode_exchange_rate_list');
143
    }
144
}
145