Completed
Push — master ( 5dbaa0...c8c481 )
by Nikola
05:15 queued 01:20
created

RestController   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 144
Duplicated Lines 45.14 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 26.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
c 1
b 0
f 0
lcom 1
cbo 5
dl 65
loc 144
ccs 16
cts 60
cp 0.2667
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A hasAction() 13 13 2
A getAction() 13 13 2
A latestAction() 13 13 2
A todayAction() 13 13 2
A historicalAction() 13 13 2
A extractParameters() 0 19 3
A exceptionToResponse() 0 8 1
A rateToArray() 0 11 1
A getManager() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 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...
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(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...
41
    {
42
        list($sourceName, $currencyCode, $date, $rateType) = array_values($this->extractParameters($request));
43
44
        try {
45
            return new JsonResponse([
46
                'error' => false,
47
                'result' => $this->rateToArray($this->getManager()->get($sourceName, $currencyCode, $date, $rateType))
48
            ]);
49
        } catch (\Exception $e) {
50
            return $this->exceptionToResponse($e);
51
        }
52
    }
53
54 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...
55
    {
56
        list($sourceName, $currencyCode, , $rateType) = array_values($this->extractParameters($request));
57
58
        try {
59
            return new JsonResponse([
60
                'error' => false,
61
                'result' => $this->rateToArray($this->getManager()->latest($sourceName, $currencyCode, $rateType))
62
            ]);
63
        } catch (\Exception $e) {
64
            return $this->exceptionToResponse($e);
65
        }
66
    }
67
68 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...
69
    {
70
        list($sourceName, $currencyCode, , $rateType) = array_values($this->extractParameters($request));
71
72
        try {
73
            return new JsonResponse([
74
                'error' => false,
75
                'result' => $this->rateToArray($this->getManager()->today($sourceName, $currencyCode, $rateType))
76
            ]);
77
        } catch (\Exception $e) {
78
            return $this->exceptionToResponse($e);
79
        }
80
    }
81
82 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...
83
    {
84
        list($sourceName, $currencyCode, $date, $rateType) = array_values($this->extractParameters($request));
85
86
        try {
87
            return new JsonResponse([
88
                'error' => false,
89
                'result' => $this->rateToArray($this->getManager()->historical($sourceName, $currencyCode, $date, $rateType))
90
            ]);
91
        } catch (\Exception $e) {
92
            return $this->exceptionToResponse($e);
93
        }
94
    }
95
96
    /**
97
     * Extract params from request.
98
     *
99
     * @param Request $request
100
     *
101
     * @return array
102
     */
103 1
    private function extractParameters(Request $request)
104
    {
105
        $params = [
106 1
            'sourceName' => $request->get('source'),
107 1
            'currencyCode' => $request->get('currency_code'),
108 1
            'date' => $request->get('date'),
109 1
            'rateType' => $request->get('rate_type')
110
        ];
111
112 1
        if (!empty($params['date'])) {
113 1
            $params['date'] = \DateTime::createFromFormat('Y-m-d', $params['date']);
114
        }
115
116 1
        if (empty($params['rateType'])) {
117
            $params['rateType'] = RateType::MEDIAN;
118
        }
119
120 1
        return $params;
121
    }
122
123
    /**
124
     * Build exception response.
125
     *
126
     * @param \Exception $e
127
     *
128
     * @return JsonResponse
129
     */
130
    private function exceptionToResponse(\Exception $e)
131
    {
132
        return new JsonResponse([
133
            'error' => true,
134
            'message' => $e->getMessage(),
135
            'class' => (new \ReflectionClass($e))->getShortName()
136
        ], 500);
137
    }
138
139
    /**
140
     * Serialize rate to array
141
     *
142
     * @param RateInterface $rate
143
     *
144
     * @return array
145
     */
146
    private function rateToArray(RateInterface $rate)
147
    {
148
        return [
149
            'source_name' => $rate->getSourceName(),
150
            'rate_type' => $rate->getRateType(),
151
            'base_currency_code' => $rate->getBaseCurrencyCode(),
152
            'date' => $rate->getDate()->format('Y-m-d'),
153
            'get_value' => $rate->getValue(),
154
            'currency_code' => $rate->getCurrencyCode(),
155
        ];
156
    }
157
158
    /**
159
     * Get manager.
160
     *
161
     * @return ManagerInterface
162
     */
163 1
    private function getManager()
164
    {
165 1
        return $this->get('runopencode.exchange_rate');
166
    }
167
}
168