Completed
Push — master ( 431275...4f28c1 )
by Nikola
05:08
created

DeleteController::getTwigTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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\Security\AccessVoter;
13
use RunOpenCode\ExchangeRate\Contract\RateInterface;
14
use RunOpenCode\ExchangeRate\Contract\RepositoryInterface;
15
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
18
19
/**
20
 * Class DeleteController
21
 *
22
 * @package RunOpenCode\Bundle\ExchangeRate\Controller
23
 */
24
class DeleteController extends Controller
25
{
26
    /**
27
     * Main controller action
28
     *
29
     * @param Request $request
30
     * @return \Symfony\Component\HttpFoundation\Response
31
     */
32 3
    public function indexAction(Request $request)
33
    {
34 3
        return $this->render($this->getTwigTemplate(), [
35 3
            'rate' => $this->getRateFromRequest($request)
36
        ]);
37
    }
38
39
    /**
40
     * Execute delete action.
41
     *
42
     * @param Request $request
43
     *
44
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
45
     */
46 5
    public function deleteAction(Request $request)
47
    {
48 5
        if (!$this->isCsrfTokenValid($request->getRequestUri(), $request->get('_csrf_token'))) {
49 1
            throw new InvalidCsrfTokenException();
50
        }
51
52
        /**
53
         * @var \RunOpenCode\ExchangeRate\Contract\RateInterface $rate
54
         */
55 4
        $rate = $this->getRateFromRequest($request);
56
57 2
        if (!$this->delete($rate)) {
58 1
            return $this->indexAction($request);
59
        }
60
61 1
        return $this->redirectAfterSuccess();
62
    }
63
64
    /**
65
     * Get rate from request
66
     *
67
     * @param Request $request
68
     *
69
     * @return \RunOpenCode\ExchangeRate\Contract\RateInterface
70
     */
71 5
    protected function getRateFromRequest(Request $request)
72
    {
73 5
        $source = $request->get('source');
74 5
        $rateType = $request->get('rate_type');
75 5
        $currencyCode = $request->get('currency_code');
76 5
        $date = \DateTime::createFromFormat('Y-m-d', $request->get('date'));
77
78
        /**
79
         * @var RepositoryInterface $repository
80
         */
81 5
        $repository = $this->get('runopencode.exchange_rate.repository');
82
83 5
        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 76 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...
84 1
            throw $this->createNotFoundException();
85
        }
86
87 4
        $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 76 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...
88
89 4
        if (!$this->isGranted(AccessVoter::DELETE, $rate)) {
90 2
            throw $this->createAccessDeniedException();
91
        }
92
93 2
        return $rate;
94
    }
95
96
    /**
97
     * Save rate.
98
     *
99
     * @param RateInterface $rate
100
     *
101
     * @return TRUE if successful.
102
     */
103 2 View Code Duplication
    protected function delete(RateInterface $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...
104
    {
105
        try {
106 2
            $this->get('runopencode.exchange_rate.repository')->delete([$rate]);
107 1
            $this->addFlash('success', $this->get('translator')->trans('flash.delete.success', [], 'runopencode_exchange_rate'));
108 1
            return true;
109 1
        } catch (\Exception $e) {
110 1
            $this->addFlash('error', $this->get('translator')->trans('flash.delete.error.unknown', [], 'runopencode_exchange_rate'));
111 1
            return false;
112
        }
113
    }
114
115
    /**
116
     * Redirect after success.
117
     *
118
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
119
     */
120 1
    protected function redirectAfterSuccess()
121
    {
122 1
        return $this->redirectToRoute('runopencode_exchange_rate_list');
123
    }
124
125
    /**
126
     * Get Twig template path.
127
     *
128
     * @return string
129
     */
130 3
    protected function getTwigTemplate()
131
    {
132 3
        return '@ExchangeRate/delete.html.twig';
133
    }
134
}
135