Completed
Push — master ( 97c16f...13d085 )
by Nikola
05:14
created

EditController::handleForm()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 9

Duplication

Lines 4
Ratio 20 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 4
loc 20
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 3
crap 3
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
0 ignored issues
show
Bug introduced by
There is no parameter named $date. 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...
37
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response|\Symfony\Component\HttpKernel\Exception\NotFoundHttpException
38
     */
39 6
    public function indexAction(Request $request)
40
    {
41 6
        $source = $request->get('source');
42 6
        $rateType = $request->get('rate_type');
43 6
        $currencyCode = $request->get('currency_code');
44 6
        $date = \DateTime::createFromFormat('Y-m-d', $request->get('date'));
45
46
        /**
47
         * @var RepositoryInterface $repository
48
         */
49 6
        $repository = $this->get('runopencode.exchange_rate.repository');
50
51 6
        if (!$repository->has($source, $currencyCode, $date, $rateType)) {
0 ignored issues
show
Security Bug introduced by
It seems like $date defined by \DateTime::createFromFor... $request->get('date')) on line 44 can also be of type false; however, RunOpenCode\ExchangeRate...ositoryInterface::has() does only seem to accept null|object<DateTime>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
52 1
            throw $this->createNotFoundException();
53
        }
54
55 5
        $rate = $repository->get($source, $currencyCode, $date, $rateType);
0 ignored issues
show
Security Bug introduced by
It seems like $date defined by \DateTime::createFromFor... $request->get('date')) on line 44 can also be of type false; however, RunOpenCode\ExchangeRate...ositoryInterface::get() does only seem to accept null|object<DateTime>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

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