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

getCoordinatesForSearchField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
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
        if ($fetchedCoordinates === '') {
55
            return $this->render(
56
                'backend/coordinates/index.html.twig', ['coordinatesForm' => $form->createView()]
57
            );
58
        } else {
59
            return $this->render(
60
                'backend/coordinates/index.html.twig', ['coordinatesForm' => $form->createView(), 'coordinates_by_searchfield' => $fetchedCoordinates]
61
            );
62
        }
63
    }
64
65
    /**
66
     * @param string $lat
67
     * @param string $lon
68
     *
69
     * @return Response
70
     * @Route("/coordinate/{lat}+{lon}", name="coordinate_by_lat-lon")
71
     */
72
    // TODO: aktuell nur von Dec in andere Formate. Konvertierung von allen Formaten in alle anderen Formate sollte aber auch irgendwann gehen..
73
    public function convertCoordinates(string $lat, string $lon)
74
    : Response {
75
        $convertedCoordinates = [];
76
77
        $lat_float = floatval($lat);
78
        $lon_float = floatval($lon);
79
80
        if (is_float($lat_float) && is_float($lon_float)) {
81
            $c = &$this->coordinatesRepository;
82
            $c->setLatLon($lat_float, $lon_float);
83
84
            $convertedCoordinates['decimal'] = $c->getDecimal()['lat'] . ' ' . $c->getDecimal()['lon'];
85
            $convertedCoordinates['degreeMinute'] = $c->getDegreeMinutes()['lat'] . ' ' . $c->getDegreeMinutes()['lon'];
86
            $convertedCoordinates['degreeMinuteSecond'] = $c->getDegreeMinutesSeconds()['lat'] . ' ' . $c->getDegreeMinutesSeconds()['lon'];
87
            $convertedCoordinates['GK'] = $c->getGK();
88
            $convertedCoordinates['QTH'] = $c->getQTH();
89
            $convertedCoordinates['RD'] = $c->getRD();
90
            $convertedCoordinates['CH1903'] = $c->getSwissGrid()['coord'];
91
            $convertedCoordinates['UTM'] = $c->getUTM()['zone'] . $c->getUTM()['letter'] . ' ' . $c->getUTM()['east'] . ' ' . $c->getUTM()['north'];
92
            $convertedCoordinates['W3W_de'] = $c->getW3W('de');
93
            $convertedCoordinates['W3W_en'] = $c->getW3W();
94
        }
95
96
        return $this->render('backend/coordinates/index.html.twig', ['converted_coordinates' => $convertedCoordinates]);
97
    }
98
99
    /**
100
     * @param string $searchtext
101
     *
102
     * @return array
103
     */
104
    public function getCoordinatesForSearchField(string $searchtext)
105
    : array {
106
        $searchtext = preg_replace("/[^0-9.,+\- ]/", "", $searchtext);
107
108
        $arr = preg_split('/\s+/', $searchtext);
109
110
        $this->coordinatesRepository->setLatLon((float) $arr[0], (float) $arr[1]);
111
112
        return $this->coordinatesRepository->getAllCoordinatesFormatsAsArray();
113
    }
114
}
115