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)) { |
|
|
|
|
84
|
1 |
|
throw $this->createNotFoundException(); |
85
|
|
|
} |
86
|
|
|
|
87
|
4 |
|
$rate = $repository->get($source, $currencyCode, $date, $rateType); |
|
|
|
|
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) |
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
|
|
|
|
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
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 returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.