Completed
Push — master ( c8c481...3543bf )
by Nikola
13:01
created

RestController::extractParametersFromRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 9
nc 2
nop 1
crap 2
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\Model\Rate;
14
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
15
use Symfony\Component\HttpFoundation\JsonResponse;
16
use Symfony\Component\HttpFoundation\Request;
17
18
/**
19
 * Class RestController
20
 *
21
 * @package RunOpenCode\Bundle\ExchangeRate\Controller
22
 */
23
class RestController extends Controller
24
{
25
    /**
26
     * Check if repository has rate
27
     *
28
     * @see \RunOpenCode\ExchangeRate\Contract\ManagerInterface::has()
29
     *
30
     * @param Request $request
31
     *
32
     * @return JsonResponse
33
     */
34 3 View Code Duplication
    public function hasAction(Request $request)
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...
35
    {
36
        /**
37
         * @var $sourceName
38
         * @var $currencyCode
39
         * @var $date
40
         * @var $rateType
41
         */
42 3
        extract($this->extractParametersFromRequest($request), EXTR_OVERWRITE);
0 ignored issues
show
Bug introduced by
$this->extractParametersFromRequest($request) cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
43
44
        try {
45 3
            return $this->createSuccessResponse($this->getManager()->has($sourceName, $currencyCode, $date, $rateType));
46 1
        } catch (\Exception $e) {
47 1
            return $this->createExceptionResponse($e);
48
        }
49
    }
50
51
    /**
52
     * Get rate.
53
     *
54
     * @see \RunOpenCode\ExchangeRate\Contract\ManagerInterface::get()
55
     *
56
     * @param Request $request
57
     *
58
     * @return JsonResponse
59
     */
60 2 View Code Duplication
    public function getAction(Request $request)
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...
61
    {
62
        /**
63
         * @var $sourceName
64
         * @var $currencyCode
65
         * @var $date
66
         * @var $rateType
67
         */
68 2
        extract($this->extractParametersFromRequest($request), EXTR_OVERWRITE);
0 ignored issues
show
Bug introduced by
$this->extractParametersFromRequest($request) cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
69
70
        try {
71 2
            return $this->createSuccessResponse($this->getManager()->get($sourceName, $currencyCode, $date, $rateType));
72 1
        } catch (\Exception $e) {
73 1
            return $this->createExceptionResponse($e);
74
        }
75
    }
76
77
    /**
78
     * Get latest applicable rate.
79
     *
80
     * @see \RunOpenCode\ExchangeRate\Contract\ManagerInterface::latest()
81
     *
82
     * @param Request $request
83
     *
84
     * @return JsonResponse
85
     */
86 2 View Code Duplication
    public function latestAction(Request $request)
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...
87
    {
88
        /**
89
         * @var $sourceName
90
         * @var $currencyCode
91
         * @var $date
92
         * @var $rateType
93
         */
94 2
        extract($this->extractParametersFromRequest($request), EXTR_OVERWRITE);
0 ignored issues
show
Bug introduced by
$this->extractParametersFromRequest($request) cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
95
96
        try {
97 2
            return $this->createSuccessResponse($this->getManager()->latest($sourceName, $currencyCode, $rateType));
98 1
        } catch (\Exception $e) {
99 1
            return $this->createExceptionResponse($e);
100
        }
101
    }
102
103
    /**
104
     * Get rate applicable for today.
105
     *
106
     * @see \RunOpenCode\ExchangeRate\Contract\ManagerInterface::today()
107
     *
108
     * @param Request $request
109
     *
110
     * @return JsonResponse
111
     */
112 2 View Code Duplication
    public function todayAction(Request $request)
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...
113
    {
114
        /**
115
         * @var $sourceName
116
         * @var $currencyCode
117
         * @var $date
118
         * @var $rateType
119
         */
120 2
        extract($this->extractParametersFromRequest($request), EXTR_OVERWRITE);
0 ignored issues
show
Bug introduced by
$this->extractParametersFromRequest($request) cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
121
122
        try {
123 2
            return $this->createSuccessResponse($this->getManager()->today($sourceName, $currencyCode, $rateType));
124 1
        } catch (\Exception $e) {
125 1
            return $this->createExceptionResponse($e);
126
        }
127
    }
128
129
    /**
130
     * Get applicable rate for given date.
131
     *
132
     * @see \RunOpenCode\ExchangeRate\Contract\ManagerInterface::historical()
133
     *
134
     * @param Request $request
135
     *
136
     * @return JsonResponse
137
     */
138 2 View Code Duplication
    public function historicalAction(Request $request)
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...
139
    {
140
        /**
141
         * @var $sourceName
142
         * @var $currencyCode
143
         * @var $date
144
         * @var $rateType
145
         */
146 2
        extract($this->extractParametersFromRequest($request), EXTR_OVERWRITE);
0 ignored issues
show
Bug introduced by
$this->extractParametersFromRequest($request) cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
147
148
        try {
149 2
            return $this->createSuccessResponse($this->getManager()->historical($sourceName, $currencyCode, $date, $rateType));
150 1
        } catch (\Exception $e) {
151 1
            return $this->createExceptionResponse($e);
152
        }
153
    }
154
155
    /**
156
     * Extract params from request.
157
     *
158
     * @param Request $request
159
     *
160
     * @return array
161
     */
162 11
    private function extractParametersFromRequest(Request $request)
163
    {
164
        $params = [
165 11
            'sourceName' => $request->get('source'),
166 11
            'currencyCode' => $request->get('currency_code'),
167 11
            'date' => $request->get('date'),
168 11
            'rateType' => $request->get('rate_type')
169
        ];
170
171 11
        if (!empty($params['date'])) {
172 9
            $params['date'] = \DateTime::createFromFormat('Y-m-d', $params['date']);
173
        }
174
175 11
        return $params;
176
    }
177
178
    /**
179
     * Build exception response.
180
     *
181
     * @param \Exception $exception
182
     *
183
     * @return JsonResponse
184
     */
185 5
    private function createExceptionResponse(\Exception $exception)
186
    {
187 5
        return new JsonResponse([
188 5
            'error' => true,
189 5
            'message' => $exception->getMessage(),
190 5
            'class' => (new \ReflectionClass($exception))->getShortName()
191 5
        ], 500);
192
    }
193
194
    /**
195
     * Build success response
196
     *
197
     * @param mixed $result
198
     * @return JsonResponse
199
     */
200
    private function createSuccessResponse($result)
201
    {
202 6
        $rateToArray = function (Rate $rate) {
203
204
            return [
205 4
                'source_name' => $rate->getSourceName(),
206 4
                'rate_type' => $rate->getRateType(),
207 4
                'base_currency_code' => $rate->getBaseCurrencyCode(),
208 4
                'date' => $rate->getDate()->format('Y-m-d'),
209 4
                'value' => $rate->getValue(),
210 4
                'currency_code' => $rate->getCurrencyCode(),
211
            ];
212 6
        };
213
214 6
        return new JsonResponse([
215 6
            'error' => false,
216 6
            'result' => ($result instanceof Rate) ? $rateToArray($result) : $result
217
        ]);
218
    }
219
220
    /**
221
     * Get manager.
222
     *
223
     * @return ManagerInterface
224
     */
225 11
    private function getManager()
226
    {
227 11
        return $this->get('runopencode.exchange_rate');
228
    }
229
}
230