|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace RunOpenCode\Bundle\ExchangeRate\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use RunOpenCode\Bundle\ExchangeRate\Form\Type\NewType; |
|
6
|
|
|
use RunOpenCode\ExchangeRate\Contract\RateInterface; |
|
7
|
|
|
use RunOpenCode\ExchangeRate\Contract\RepositoryInterface; |
|
8
|
|
|
use RunOpenCode\Bundle\ExchangeRate\Model\Rate; |
|
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
10
|
|
|
use Symfony\Component\Form\FormError; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
12
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
13
|
|
|
use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException; |
|
14
|
|
|
use Symfony\Component\Security\Csrf\CsrfToken; |
|
15
|
|
|
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface; |
|
16
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
17
|
|
|
|
|
18
|
|
|
class ExchangeRateController extends Controller |
|
19
|
|
|
{ |
|
20
|
|
|
protected $repository; |
|
21
|
|
|
|
|
22
|
|
|
protected $baseCurrency; |
|
23
|
|
|
|
|
24
|
|
|
protected $settings; |
|
25
|
|
|
|
|
26
|
|
|
protected $csrfTokenManager; |
|
27
|
|
|
|
|
28
|
|
|
protected $translator; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(CsrfTokenManagerInterface $csrfTokenManager, TranslatorInterface $translator, RepositoryInterface $repository, $baseCurrency, $settings) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->repository = $repository; |
|
33
|
|
|
$this->baseCurrency = $baseCurrency; |
|
34
|
|
|
$this->settings = $settings; |
|
35
|
|
|
$this->csrfTokenManager = $csrfTokenManager; |
|
36
|
|
|
$this->translator = $translator; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function indexAction() |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->render($this->settings['list'], array( |
|
42
|
|
|
'base_template' => $this->settings['base_template'], |
|
43
|
|
|
'rates' => $this->repository->all(), |
|
44
|
|
|
'date_format' => $this->settings['date_format'], |
|
45
|
|
|
'date_time_format' => $this->settings['date_time_format'] |
|
46
|
|
|
)); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function newAction(Request $request) |
|
50
|
|
|
{ |
|
51
|
|
|
$form = $this->createForm(NewType::class, $this->getNewRate()); |
|
52
|
|
|
|
|
53
|
|
|
$form->handleRequest($request); |
|
54
|
|
|
|
|
55
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var RateInterface $rate |
|
59
|
|
|
*/ |
|
60
|
|
|
$rate = $form->getData(); |
|
61
|
|
|
|
|
62
|
|
|
if ($this->repository->has($rate->getCurrencyCode(), $rate->getDate(), $rate->getRateType())) { |
|
63
|
|
|
$form->addError(new FormError($this->translator->trans('exchange_rate.form.error.new_exists', array(), 'roc_exchange_rate'))); |
|
64
|
|
|
} else { |
|
65
|
|
|
$this->repository->save(array( |
|
66
|
|
|
$form->getData() |
|
67
|
|
|
)); |
|
68
|
|
|
|
|
69
|
|
|
$this->get('session')->getFlashBag()->add('success', 'exchange_rate.flash.new.success'); |
|
70
|
|
|
return $this->redirectToRoute('roc_exchange_rate_list'); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $this->render($this->settings['new'], array( |
|
75
|
|
|
'base_template' => $this->settings['base_template'], |
|
76
|
|
|
'form' => $form->createView() |
|
77
|
|
|
)); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function editAction(Request $request) |
|
81
|
|
|
{ |
|
82
|
|
|
$form = $this->createForm(NewType::class, $this->getRateFromRequest($request)); |
|
83
|
|
|
|
|
84
|
|
|
$form->handleRequest($request); |
|
85
|
|
|
|
|
86
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
87
|
|
|
|
|
88
|
|
|
$this->repository->save(array( |
|
89
|
|
|
$form->getData() |
|
90
|
|
|
)); |
|
91
|
|
|
|
|
92
|
|
|
$this->get('session')->getFlashBag()->add('success', 'exchange_rate.flash.edit.success'); |
|
93
|
|
|
return $this->redirectToRoute('roc_exchange_rate_list'); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $this->render($this->settings['edit'], array( |
|
97
|
|
|
'base_template' => $this->settings['base_template'], |
|
98
|
|
|
'form' => $form->createView() |
|
99
|
|
|
)); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function deleteAction(Request $request) |
|
103
|
|
|
{ |
|
104
|
|
|
if (!$this->csrfTokenManager->isTokenValid(new CsrfToken($request->getRequestUri(), $request->get('_csrf_token')))) { |
|
105
|
|
|
throw new InvalidCsrfTokenException; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$rate = $this->getRateFromRequest($request); |
|
109
|
|
|
|
|
110
|
|
|
$this->repository->delete(array($rate)); |
|
111
|
|
|
|
|
112
|
|
|
$this->get('session')->getFlashBag()->add('success', 'exchange_rate.flash.delete.success'); |
|
113
|
|
|
return $this->redirectToRoute('roc_exchange_rate_list'); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
protected function getNewRate() |
|
117
|
|
|
{ |
|
118
|
|
|
$rate = new Rate(null, null, $this->baseCurrency, null, null, $this->baseCurrency, null, null); |
|
119
|
|
|
$rate->setBaseCurrencyCode($this->baseCurrency); |
|
120
|
|
|
|
|
121
|
|
|
return $rate; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
protected function getRateFromRequest(Request $request) |
|
125
|
|
|
{ |
|
126
|
|
|
if (!$this->repository->has($request->get('currency_code'), \DateTime::createFromFormat('Y-m-d', $request->get('date')), $request->get('rate_type'))) { |
|
|
|
|
|
|
127
|
|
|
throw new NotFoundHttpException(); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return Rate::fromRateInterface($this->repository->get($request->get('currency_code'), \DateTime::createFromFormat('Y-m-d', $request->get('date')), $request->get('rate_type'))); |
|
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|