Completed
Pull Request — master (#340)
by Luc
05:05
created

GeoCoordinatesCommandHandler   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 65
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 1
cbo 6
dl 65
loc 65
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 11 1
A handleUpdateGeoCoordinatesFromAddress() 21 21 2

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
namespace CultuurNet\UDB3\Event;
4
5
use Broadway\Repository\RepositoryInterface;
6
use CultuurNet\Geocoding\GeocodingServiceInterface;
7
use CultuurNet\UDB3\Address\AddressFormatterInterface;
8
use CultuurNet\UDB3\CommandHandling\Udb3CommandHandler;
9
use CultuurNet\UDB3\Event\Commands\UpdateGeoCoordinatesFromAddress;
10
11 View Code Duplication
class GeoCoordinatesCommandHandler extends Udb3CommandHandler
0 ignored issues
show
Duplication introduced by
This class 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...
12
{
13
    /**
14
     * @var RepositoryInterface
15
     */
16
    private $eventRepository;
17
18
    /**
19
     * @var AddressFormatterInterface
20
     */
21
    private $defaultAddressFormatter;
22
23
    /**
24
     * @var AddressFormatterInterface
25
     */
26
    private $fallbackAddressFormatter;
27
28
    /**
29
     * @var GeocodingServiceInterface
30
     */
31
    private $geocodingService;
32
33
    /**
34
     * @param RepositoryInterface $eventRepository
35
     * @param AddressFormatterInterface $defaultAddressFormatter
36
     * @param AddressFormatterInterface $fallbackAddressFormatter
37
     * @param GeocodingServiceInterface $geocodingService
38
     */
39
    public function __construct(
40
        RepositoryInterface $eventRepository,
41
        AddressFormatterInterface $defaultAddressFormatter,
42
        AddressFormatterInterface $fallbackAddressFormatter,
43
        GeocodingServiceInterface $geocodingService
44
    ) {
45
        $this->eventRepository = $eventRepository;
46
        $this->defaultAddressFormatter = $defaultAddressFormatter;
47
        $this->fallbackAddressFormatter = $fallbackAddressFormatter;
48
        $this->geocodingService = $geocodingService;
49
    }
50
51
    /**
52
     * @param UpdateGeoCoordinatesFromAddress $updateGeoCoordinates
53
     */
54
    public function handleUpdateGeoCoordinatesFromAddress(UpdateGeoCoordinatesFromAddress $updateGeoCoordinates)
55
    {
56
        $coordinates = $this->geocodingService->getCoordinates(
57
            $this->defaultAddressFormatter->format(
58
                $updateGeoCoordinates->getAddress()
59
            )
60
        );
61
62
        if ($coordinates === null) {
63
            $coordinates = $this->geocodingService->getCoordinates(
64
                $this->fallbackAddressFormatter->format(
65
                    $updateGeoCoordinates->getAddress()
66
                )
67
            );
68
        }
69
70
        /** @var Event $event */
71
        $event = $this->eventRepository->load($updateGeoCoordinates->getItemId());
72
        $event->updateGeoCoordinates($coordinates);
73
        $this->eventRepository->save($event);
74
    }
75
}
76