InterventionService::save()   F
last analyzed

Complexity

Conditions 14
Paths 1025

Size

Total Lines 50
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 1
Metric Value
c 5
b 1
f 1
dl 0
loc 50
rs 2.6835
cc 14
eloc 25
nc 1025
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SDIS62\Core\Ops\Service;
4
5
use SDIS62\Core\Ops\Entity\Coordinates;
6
use SDIS62\Core\Ops\Entity\Intervention;
7
use SDIS62\Core\Ops\Repository\CommuneRepositoryInterface;
8
use SDIS62\Core\Ops\Repository\InterventionRepositoryInterface;
9
use SDIS62\Core\Ops\Repository\SinistreRepositoryInterface;
10
11
class InterventionService
12
{
13
    /**
14
     * Initialisation du service avec les repository utilisés.
15
     *
16
     * @param SDIS62\Core\Ops\Repository\InterventionRepositoryInterface $intervention_repository
17
     * @param SDIS62\Core\Ops\Repository\SinistreRepositoryInterface     $sinistre_repository
18
     * @param SDIS62\Core\Ops\Repository\CommuneRepositoryInterface      $commune_repository
19
     */
20
    public function __construct(InterventionRepositoryInterface $intervention_repository,
21
                                SinistreRepositoryInterface $sinistre_repository,
22
                                CommuneRepositoryInterface $commune_repository
23
    ) {
24
        $this->intervention_repository = $intervention_repository;
0 ignored issues
show
Bug introduced by
The property intervention_repository does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
25
        $this->sinistre_repository     = $sinistre_repository;
0 ignored issues
show
Bug introduced by
The property sinistre_repository does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
26
        $this->commune_repository      = $commune_repository;
0 ignored issues
show
Bug introduced by
The property commune_repository does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
27
    }
28
29
    /**
30
     * Retourne les interventions.
31
     *
32
     * @param int $count Par défaut: 20
33
     * @param int $page  Par défaut: 1
34
     *
35
     * @return SDIS62\Core\Ops\Entity\Intervention[]
36
     */
37
    public function getAll($count = 20, $page = 1)
38
    {
39
        return $this->intervention_repository->getAll($count, $page);
40
    }
41
42
    /**
43
     * Retourne une intervention correspondant à l'id spécifié.
44
     *
45
     * @param mixed $id_intervention
46
     *
47
     * @return SDIS62\Core\Ops\Entity\Intervention
48
     */
49
    public function find($id_intervention)
50
    {
51
        return $this->intervention_repository->find($id_intervention);
52
    }
53
54
    /**
55
     * Retourne les interventions se trouvant dans un rayon de 500m (par défaut) des coordonnées.
56
     *
57
     * @param float $lat
58
     * @param float $lon
59
     * @param int   $distance
60
     *
61
     * @return SDIS62\Core\Ops\Entity\Intervention[]
62
     */
63
    public function findAllByDistance($lat, $lon, $distance = 500)
64
    {
65
        return $this->intervention_repository->findAllByDistance($lat, $lon, $distance);
66
    }
67
68
    /**
69
     * Sauvegarde d'une intervention.
70
     *
71
     * @param array $data
72
     * @param array $id_intervention Optionnel
73
     *
74
     * @return SDIS62\Core\Ops\Entity\Intervention
75
     */
76
    public function save($data, $id_intervention = null)
77
    {
78
        $sinistre = $this->sinistre_repository->find($data['sinistre']);
79
80
        if (empty($sinistre)) {
81
            return;
82
        }
83
84
        $intervention = empty($id_intervention) ? new Intervention($sinistre) : $this->intervention_repository->find($id_intervention);
85
86
        if (!empty($data['precision'])) {
87
            $intervention->setPrecision($data['precision']);
88
        }
89
90
        if (!empty($data['observations'])) {
91
            $intervention->setObservations($data['observations']);
92
        }
93
94
        if (!empty($data['updated'])) {
95
            $intervention->setUpdated($data['updated']);
96
        }
97
98
        if (!empty($data['ended'])) {
99
            $intervention->setEnded($data['ended']);
100
        }
101
102
        if (!empty($data['sinistre'])) {
103
            $intervention->setSinistre($sinistre);
104
        }
105
106
        if (!empty($data['coordinates']) && is_array($data['coordinates']) && count($data['coordinates']) == 2) {
107
            $intervention->setCoordinates(new Coordinates($data['coordinates'][0], $data['coordinates'][1]));
108
        }
109
110
        if (!empty($data['address'])) {
111
            $intervention->setAddress($data['address']);
112
        }
113
114
        if (array_key_exists('important', $data)) {
115
            $intervention->setImportant($data['important'] === true);
116
        }
117
118
        if (!empty($data['commune'])) {
119
            $intervention->setCommune($this->commune_repository->find($data['commune']));
120
        }
121
122
        $this->intervention_repository->save($intervention);
123
124
        return $intervention;
125
    }
126
127
    /**
128
     * Suppression d'une intervention.
129
     *
130
     * @param mixed $id_intervention
131
     *
132
     * @return SDIS62\Core\Ops\Entity\Intervention
133
     */
134
    public function delete($id_intervention)
135
    {
136
        $intervention = $this->find($id_intervention);
137
138
        if (empty($intervention)) {
139
            return;
140
        }
141
142
        $this->intervention_repository->delete($intervention);
143
144
        return $intervention;
145
    }
146
}
147