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\ExchangeRate\Contract\ManagerInterface; |
13
|
|
|
use RunOpenCode\ExchangeRate\Contract\RateInterface; |
14
|
|
|
use RunOpenCode\ExchangeRate\Enum\RateType; |
15
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
16
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class RestController |
21
|
|
|
* |
22
|
|
|
* @package RunOpenCode\Bundle\ExchangeRate\Controller |
23
|
|
|
*/ |
24
|
|
|
class RestController extends Controller |
25
|
|
|
{ |
26
|
1 |
|
public function hasAction(Request $request) |
27
|
|
|
{ |
28
|
1 |
|
list($sourceName, $currencyCode, $date, $rateType) = array_values($this->extractParameters($request)); |
29
|
|
|
|
30
|
|
|
try { |
31
|
1 |
|
return new JsonResponse([ |
32
|
1 |
|
'error' => false, |
33
|
1 |
|
'result' => $this->getManager()->has($sourceName, $currencyCode, $date, $rateType) |
34
|
|
|
]); |
35
|
|
|
} catch (\Exception $e) { |
36
|
|
|
return $this->exceptionToResponse($e); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
View Code Duplication |
public function getAction($sourceName, $currencyCode, \DateTime $date = null, $rateType = RateType::MEDIAN) |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
try { |
43
|
|
|
return new JsonResponse([ |
44
|
|
|
'error' => false, |
45
|
|
|
'result' => $this->rateToArray($this->getManager()->get($sourceName, $currencyCode, $date, $rateType)) |
|
|
|
|
46
|
|
|
]); |
47
|
|
|
} catch (\Exception $e) { |
48
|
|
|
return $this->exceptionToResponse($e); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
View Code Duplication |
public function latestAction($sourceName, $currencyCode, $rateType = RateType::MEDIAN) |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
try { |
55
|
|
|
return new JsonResponse([ |
56
|
|
|
'error' => false, |
57
|
|
|
'result' => $this->rateToArray($this->getManager()->latest($sourceName, $currencyCode, $rateType)) |
58
|
|
|
]); |
59
|
|
|
} catch (\Exception $e) { |
60
|
|
|
return $this->exceptionToResponse($e); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
View Code Duplication |
public function todayAction($sourceName, $currencyCode, $rateType = RateType::MEDIAN) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
try { |
67
|
|
|
return new JsonResponse([ |
68
|
|
|
'error' => false, |
69
|
|
|
'result' => $this->rateToArray($this->getManager()->today($sourceName, $currencyCode, $rateType)) |
70
|
|
|
]); |
71
|
|
|
} catch (\Exception $e) { |
72
|
|
|
return $this->exceptionToResponse($e); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
View Code Duplication |
public function historicalAction($sourceName, $currencyCode, \DateTime $date, $rateType = RateType::MEDIAN) |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
try { |
79
|
|
|
return new JsonResponse([ |
80
|
|
|
'error' => false, |
81
|
|
|
'result' => $this->rateToArray($this->getManager()->historical($sourceName, $currencyCode, $date, $rateType)) |
82
|
|
|
]); |
83
|
|
|
} catch (\Exception $e) { |
84
|
|
|
return $this->exceptionToResponse($e); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Extract params from request. |
90
|
|
|
* |
91
|
|
|
* @param Request $request |
92
|
|
|
* |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
1 |
|
private function extractParameters(Request $request) |
96
|
|
|
{ |
97
|
|
|
$params = [ |
98
|
1 |
|
'sourceName' => $request->get('source'), |
99
|
1 |
|
'currencyCode' => $request->get('currency_code'), |
100
|
1 |
|
'date' => $request->get('date'), |
101
|
1 |
|
'rateType' => $request->get('rate_type') |
102
|
|
|
]; |
103
|
|
|
|
104
|
1 |
|
if (!empty($params['date'])) { |
105
|
1 |
|
$params['date'] = \DateTime::createFromFormat('Y-m-d', $params['date']); |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
return $params; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Build exception response. |
113
|
|
|
* |
114
|
|
|
* @param \Exception $e |
115
|
|
|
* |
116
|
|
|
* @return JsonResponse |
117
|
|
|
*/ |
118
|
|
|
private function exceptionToResponse(\Exception $e) |
119
|
|
|
{ |
120
|
|
|
return new JsonResponse([ |
121
|
|
|
'error' => true, |
122
|
|
|
'message' => $e->getMessage(), |
123
|
|
|
'class' => (new \ReflectionClass($e))->getShortName() |
124
|
|
|
], 500); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Serialize rate to array |
129
|
|
|
* |
130
|
|
|
* @param RateInterface $rate |
131
|
|
|
* |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
|
|
private function rateToArray(RateInterface $rate) |
135
|
|
|
{ |
136
|
|
|
return [ |
137
|
|
|
'source_name' => $rate->getSourceName(), |
138
|
|
|
'rate_type' => $rate->getRateType(), |
139
|
|
|
'base_currency_code' => $rate->getBaseCurrencyCode(), |
140
|
|
|
'date' => $rate->getDate()->format('Y-m-d'), |
141
|
|
|
'get_value' => $rate->getValue(), |
142
|
|
|
'currency_code' => $rate->getCurrencyCode(), |
143
|
|
|
]; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get manager. |
148
|
|
|
* |
149
|
|
|
* @return ManagerInterface |
150
|
|
|
*/ |
151
|
1 |
|
private function getManager() |
152
|
|
|
{ |
153
|
1 |
|
return $this->get('runopencode.exchange_rate'); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
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.