1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RunOpenCode\Bundle\ExchangeRate\Controller; |
4
|
|
|
|
5
|
|
|
use RunOpenCode\Bundle\ExchangeRate\Form\Type\NewType; |
6
|
|
|
use RunOpenCode\ExchangeRate\Contract\RepositoryInterface; |
7
|
|
|
use RunOpenCode\Bundle\ExchangeRate\Model\Rate; |
8
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
10
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
11
|
|
|
|
12
|
|
|
class ExchangeRateController extends Controller |
13
|
|
|
{ |
14
|
|
|
protected $repository; |
15
|
|
|
|
16
|
|
|
protected $baseCurrency; |
17
|
|
|
|
18
|
|
|
protected $settings; |
19
|
|
|
|
20
|
|
|
public function __construct(RepositoryInterface $repository, $baseCurrency, $settings) |
21
|
|
|
{ |
22
|
|
|
$this->repository = $repository; |
23
|
|
|
$this->baseCurrency = $baseCurrency; |
24
|
|
|
$this->settings = $settings; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function indexAction() |
28
|
|
|
{ |
29
|
|
|
return $this->render($this->settings['list'], array( |
30
|
|
|
'base_template' => $this->settings['base_template'], |
31
|
|
|
'rates' => $this->repository->all(), |
32
|
|
|
'date_format' => $this->settings['date_format'], |
33
|
|
|
'date_time_format' => $this->settings['date_time_format'] |
34
|
|
|
)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
View Code Duplication |
public function newAction(Request $request) |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$form = $this->createForm(NewType::class, $this->getNewRate()); |
40
|
|
|
|
41
|
|
|
$form->handleRequest($request); |
42
|
|
|
|
43
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
44
|
|
|
|
45
|
|
|
$this->repository->save(array( |
46
|
|
|
$form->getData() |
47
|
|
|
)); |
48
|
|
|
|
49
|
|
|
$this->get('session')->getFlashBag()->add('success', 'run_open_code.exchange_rate.flash.new.success'); |
50
|
|
|
return $this->redirectToRoute('roc_exchange_rate_list'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this->render($this->settings['new'], array( |
54
|
|
|
'base_template' => $this->settings['base_template'], |
55
|
|
|
'form' => $form->createView() |
56
|
|
|
)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
View Code Duplication |
public function editAction(Request $request) |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$form = $this->createForm(NewType::class, $this->getRateFromRequest($request)); |
62
|
|
|
|
63
|
|
|
$form->handleRequest($request); |
64
|
|
|
|
65
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
66
|
|
|
|
67
|
|
|
$this->repository->save(array( |
68
|
|
|
$form->getData() |
69
|
|
|
)); |
70
|
|
|
|
71
|
|
|
$this->get('session')->getFlashBag()->add('success', 'run_open_code.exchange_rate.flash.edit.success'); |
72
|
|
|
return $this->redirectToRoute('roc_exchange_rate_list'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this->render($this->settings['edit'], array( |
76
|
|
|
'base_template' => $this->settings['base_template'], |
77
|
|
|
'form' => $form->createView() |
78
|
|
|
)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function getNewRate() |
82
|
|
|
{ |
83
|
|
|
$rate = new Rate(null, null, $this->baseCurrency, null, null, $this->baseCurrency, null, null); |
84
|
|
|
$rate->setBaseCurrencyCode($this->baseCurrency); |
85
|
|
|
|
86
|
|
|
return $rate; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function getRateFromRequest(Request $request) |
90
|
|
|
{ |
91
|
|
|
if (!$this->repository->has($request->get('currency_code'), \DateTime::createFromFormat('Y-m-d', $request->get('date')), $request->get('rate_type'))) { |
|
|
|
|
92
|
|
|
throw new NotFoundHttpException(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return Rate::fromRateInterface($this->repository->get($request->get('currency_code'), \DateTime::createFromFormat('Y-m-d', $request->get('date')), $request->get('rate_type'))); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
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.