Completed
Pull Request — development (#820)
by
unknown
05:16
created

CoordinatesController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 90
Duplicated Lines 17.78 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 16
loc 90
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A coordinatesController_index() 16 16 3
A convertCoordinates() 0 25 3
A getCoordinatesForSearchField() 0 10 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
declare(strict_types=1);
4
5
namespace Oc\Controller\Backend;
6
7
use Oc\Form\CoordinatesFormType;
8
use Oc\Repository\CoordinatesRepository;
9
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\Routing\Annotation\Route;
13
14
/**
15
 * Class CoordinatesController
16
 *
17
 * @package Oc\Controller\Backend
18
 */
19
class CoordinatesController extends AbstractController
20
{
21
    /**
22
     * @var CoordinatesRepository
23
     */
24
    private $coordinatesRepository;
25
26
    /**
27
     * CoordinatesController constructor.
28
     *
29
     * @param CoordinatesRepository $coordinatesRepository
30
     */
31
    public function __construct(CoordinatesRepository $coordinatesRepository)
32
    {
33
        $this->coordinatesRepository = $coordinatesRepository;
34
    }
35
36
    /**
37
     * @param Request $request
38
     * @Route("/coordinates", name="coordinates_index")
39
     *
40
     * @return Response
41
     */
42 View Code Duplication
    public function coordinatesController_index(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...
43
    : Response {
44
        $fetchedCoordinates = '';
45
        $form = $this->createForm(CoordinatesFormType::class);
46
47
        $form->handleRequest($request);
48
        if ($form->isSubmitted() && $form->isValid()) {
49
            $inputData = $form->getData();
50
51
            $fetchedCoordinates = $this->getCoordinatesForSearchField($inputData['content_coordinates_searchfield']);
52
        }
53
54
        return $this->render(
55
            'backend/coordinates/index.html.twig', ['coordinatesForm' => $form->createView(), 'coordinates_by_searchfield' => $fetchedCoordinates]
56
        );
57
    }
58
59
    /**
60
     * @param string $lat
61
     * @param string $lon
62
     *
63
     * @return Response
64
     * @Route("/coordinate/{lat}+{lon}", name="coordinate_by_lat-lon")
65
     */
66
    // TODO: aktuell nur von Dec in andere Formate. Konvertierung von allen Formaten in alle anderen Formate sollte aber auch irgendwann gehen..
67
    public function convertCoordinates(string $lat, string $lon)
68
    : Response {
69
        $convertedCoordinates = [];
70
71
        $lat_float = floatval($lat);
72
        $lon_float = floatval($lon);
73
74
        if (is_float($lat_float) && is_float($lon_float)) {
75
            $c = &$this->coordinatesRepository;
76
            $c->setLatLon($lat_float, $lon_float);
77
78
            $convertedCoordinates['decimal'] = $c->getDecimal()['lat'] . ' ' . $c->getDecimal()['lon'];
79
            $convertedCoordinates['degreeMinute'] = $c->getDegreeMinutes()['lat'] . ' ' . $c->getDegreeMinutes()['lon'];
80
            $convertedCoordinates['degreeMinuteSecond'] = $c->getDegreeMinutesSeconds()['lat'] . ' ' . $c->getDegreeMinutesSeconds()['lon'];
81
            $convertedCoordinates['GK'] = $c->getGK();
82
            $convertedCoordinates['QTH'] = $c->getQTH();
83
            $convertedCoordinates['RD'] = $c->getRD();
84
            $convertedCoordinates['CH1903'] = $c->getSwissGrid()['coord'];
85
            $convertedCoordinates['UTM'] = $c->getUTM()['zone'] . $c->getUTM()['letter'] . ' ' . $c->getUTM()['east'] . ' ' . $c->getUTM()['north'];
86
            $convertedCoordinates['W3W_de'] = $c->getW3W('de');
87
            $convertedCoordinates['W3W_en'] = $c->getW3W();
88
        }
89
90
        return $this->render('backend/coordinates/index.html.twig', ['converted_coordinates' => $convertedCoordinates]);
91
    }
92
93
    /**
94
     * @param string $searchtext
95
     *
96
     * @return array
97
     */
98
    public function getCoordinatesForSearchField(string $searchtext)
99
    : array {
100
        $searchtext = preg_replace("/[^0-9.,+\- ]/", "", $searchtext);
101
102
        $arr = preg_split('/\s+/', $searchtext);
103
104
        $this->coordinatesRepository->setLatLon((float) $arr[0], (float) $arr[1]);
105
106
        return $this->coordinatesRepository->getAllCoordinatesFormatsAsArray();
107
    }
108
}
109