Completed
Pull Request — development (#820)
by
unknown
04:51
created

CoordinatesController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 93
Duplicated Lines 23.66 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 22
loc 93
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A index() 22 22 4
A convertCoordinates() 0 25 3
A getCoordinatesForSearchField() 0 11 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
    private $coordinatesRepository;
22
23
    /**
24
     * CoordinatesController constructor.
25
     *
26
     * @param CoordinatesRepository $coordinatesRepository
27
     */
28
    public function __construct(CoordinatesRepository $coordinatesRepository)
29
    {
30
        $this->coordinatesRepository = $coordinatesRepository;
31
    }
32
33
    /**
34
     * @param Request $request
35
     * @Route("/coordinates", name="coordinates_index")
36
     *
37
     * @return Response
38
     */
39 View Code Duplication
    public function 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...
40
    : Response {
41
        $fetchedCoordinates = '';
42
        $form = $this->createForm(CoordinatesFormType::class);
43
44
        $form->handleRequest($request);
45
        if ($form->isSubmitted() && $form->isValid()) {
46
            $inputData = $form->getData();
47
48
            $fetchedCoordinates = $this->getCoordinatesForSearchField($inputData['content_coordinates_searchfield']);
49
        }
50
51
        if ($fetchedCoordinates === '') {
52
            return $this->render(
53
                'backend/coordinates/index.html.twig', ['coordinatesForm' => $form->createView()]
54
            );
55
        } else {
56
            return $this->render(
57
                'backend/coordinates/index.html.twig', ['coordinatesForm' => $form->createView(), 'coordinates_by_searchfield' => $fetchedCoordinates]
58
            );
59
        }
60
    }
61
62
    /**
63
     * @param string $lat
64
     * @param string $lon
65
     *
66
     * @return Response
67
     * @Route("/coordinate/{lat}+{lon}", name="coordinate_by_lat-lon")
68
     */
69
    public function convertCoordinates(string $lat, string $lon)
70
    : Response {
71
        $convertedCoordinates = [];
72
73
        $lat_float = floatval($lat);
74
        $lon_float = floatval($lon);
75
76
        if (is_float($lat_float) && is_float($lon_float)) {
77
            $c = &$this->coordinatesRepository;
78
            $c->setLatLon($lat_float, $lon_float);
79
80
            $convertedCoordinates['decimal'] = $c->getDecimal()['lat'] . ' ' . $c->getDecimal()['lon'];
81
            $convertedCoordinates['degreeMinute'] = $c->getDegreeMinutes()['lat'] . ' ' . $c->getDegreeMinutes()['lon'];
82
            $convertedCoordinates['degreeMinuteSecond'] = $c->getDegreeMinutesSeconds()['lat'] . ' ' . $c->getDegreeMinutesSeconds()['lon'];
83
            $convertedCoordinates['GK'] = $c->getGK();
84
            $convertedCoordinates['QTH'] = $c->getQTH();
85
            $convertedCoordinates['RD'] = $c->getRD();
86
            $convertedCoordinates['CH1903'] = $c->getSwissGrid()['coord'];
87
            $convertedCoordinates['UTM'] = $c->getUTM()['zone'] . $c->getUTM()['letter'] . ' ' . $c->getUTM()['east'] . ' ' . $c->getUTM()['north'];
88
            $convertedCoordinates['W3W_de'] = $c->getW3W('de');
89
            $convertedCoordinates['W3W_en'] = $c->getW3W();
90
        }
91
92
        return $this->render('backend/coordinates/index.html.twig', ['converted_coordinates' => $convertedCoordinates]);
93
    }
94
95
    /**
96
     * @param string $searchtext
97
     *
98
     * @return array
99
     */
100
    public function getCoordinatesForSearchField(string $searchtext)
101
    : array {
102
        $searchtext = trim($searchtext);
103
        $searchtext = preg_replace("/[^0-9.,+\- ]/", "", $searchtext);
104
105
        $arr = preg_split('/\s+/', $searchtext);
106
107
        $this->coordinatesRepository->setLatLon((float) $arr[0], (float) $arr[1]);
108
109
        return $this->coordinatesRepository->getAllCoordinatesFormatsAsArray();
110
    }
111
}
112