EngagementService::save()   B
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
c 4
b 2
f 0
dl 0
loc 24
rs 8.6845
cc 4
eloc 15
nc 3
nop 2
1
<?php
2
3
namespace SDIS62\Core\Ops\Service;
4
5
use SDIS62\Core\Ops\Entity\Engagement;
6
use SDIS62\Core\Ops\Exception\InvalidEngagementException;
7
use SDIS62\Core\Ops\Repository\EngagementRepositoryInterface;
8
use SDIS62\Core\Ops\Repository\InterventionRepositoryInterface;
9
use SDIS62\Core\Ops\Repository\MaterielRepositoryInterface;
10
use SDIS62\Core\Ops\Repository\PompierRepositoryInterface;
11
12
class EngagementService
13
{
14
    /**
15
     * Initialisation du service avec les repository utilisés.
16
     *
17
     * @param SDIS62\Core\Ops\Repository\EngagementRepositoryInterface   $engagement_repository
18
     * @param SDIS62\Core\Ops\Repository\MaterielRepositoryInterface     $intervention_repository
19
     * @param SDIS62\Core\Ops\Repository\InterventionRepositoryInterface $intervention_repository
20
     * @param SDIS62\Core\Ops\Repository\PompierRepositoryInterface      $pompier_repository
21
     */
22
    public function __construct(EngagementRepositoryInterface $engagement_repository,
23
                                MaterielRepositoryInterface $materiel_repository,
24
                                InterventionRepositoryInterface $intervention_repository,
25
                                PompierRepositoryInterface $pompier_repository
26
    ) {
27
        $this->engagement_repository   = $engagement_repository;
0 ignored issues
show
Bug introduced by
The property engagement_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...
28
        $this->materiel_repository     = $materiel_repository;
0 ignored issues
show
Bug introduced by
The property materiel_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...
29
        $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...
30
        $this->pompier_repository      = $pompier_repository;
0 ignored issues
show
Bug introduced by
The property pompier_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...
31
    }
32
33
    /**
34
     * Retourne un engagement correspondant à l'id spécifié.
35
     *
36
     * @param mixed $id_engagement
37
     *
38
     * @return SDIS62\Core\Ops\Entity\Engagement
39
     */
40
    public function find($id_engagement)
41
    {
42
        return $this->engagement_repository->find($id_engagement);
43
    }
44
45
    /**
46
     * Retourne les engagements se trouvant dans un rayon de 500m (par défaut) des coordonnées.
47
     *
48
     * @param float $lat
49
     * @param float $lon
50
     * @param int   $distance
51
     *
52
     * @return SDIS62\Core\Ops\Entity\Engagement[]
53
     */
54
    public function findAllByDistance($lat, $lon, $distance = 500)
55
    {
56
        return $this->engagement_repository->findAllByDistance($lat, $lon, $distance);
57
    }
58
59
    /**
60
     * Sauvegarde d'un engagement.
61
     *
62
     * @param array $data
63
     * @param array $id_engagement Optionnel
64
     *
65
     * @return SDIS62\Core\Ops\Entity\Engagement
66
     */
67
    public function save($data, $id_engagement = null)
68
    {
69
        if (empty($id_engagement)) {
70
            $intervention = $this->intervention_repository->find($data['intervention']);
71
72
            switch ($data['type']) {
73
                case 'pompier' :
74
                    $materiel   = $this->materiel_repository->find($data['materiel']);
75
                    $pompier    = $this->pompier_repository->find($data['pompier']);
76
                    $engagement = new Engagement\PompierEngagement($intervention, $materiel, $pompier, empty($data['specialites_engagees']) ? [] : $data['specialites_engagees']);
77
                    break;
78
                default:
79
                    throw new InvalidEngagementException();
80
            }
81
        } else {
82
            $engagement = $this->engagement_repository->find($data['id']);
83
        }
84
85
// evenements
86
87
        $this->engagement_repository->save($engagement);
88
89
        return $engagement;
90
    }
91
92
    /**
93
     * Suppression d'un engagement.
94
     *
95
     * @param mixed $id_engagement
96
     *
97
     * @return SDIS62\Core\Ops\Entity\Engagement
98
     */
99
    public function delete($id_engagement)
100
    {
101
        $engagement = $this->find($id_engagement);
102
103
        if (empty($engagement)) {
104
            return;
105
        }
106
107
        $this->engagement_repository->delete($engagement);
108
109
        return $engagement;
110
    }
111
}
112