MaterielService::find()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace SDIS62\Core\Ops\Service;
4
5
use SDIS62\Core\Ops\Entity\Coordinates;
6
use SDIS62\Core\Ops\Entity\Materiel;
7
use SDIS62\Core\Ops\Entity\Statut;
8
use SDIS62\Core\Ops\Repository\CentreRepositoryInterface;
9
use SDIS62\Core\Ops\Repository\MaterielRepositoryInterface;
10
11
class MaterielService
12
{
13
    /**
14
     * Initialisation du service avec les repository utilisés.
15
     *
16
     * @param SDIS62\Core\Ops\Repository\MaterielRepositoryInterface $materiel_repository
17
     * @param SDIS62\Core\Ops\Repository\CentreRepositoryInterface   $centre_repository
18
     */
19
    public function __construct(MaterielRepositoryInterface $materiel_repository,
20
                                CentreRepositoryInterface $centre_repository
21
    ) {
22
        $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...
23
        $this->centre_repository   = $centre_repository;
0 ignored issues
show
Bug introduced by
The property centre_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...
24
    }
25
26
    /**
27
     * Retourne un matériel correspondant à l'id spécifié.
28
     *
29
     * @param mixed $id_materiel
30
     *
31
     * @return SDIS62\Core\Ops\Entity\Materiel
32
     */
33
    public function find($id_materiel)
34
    {
35
        return $this->materiel_repository->find($id_materiel);
36
    }
37
38
    /**
39
     * Sauvegarde d'un matériel.
40
     *
41
     * @param array $data
42
     * @param array $id_materiel Optionnel
43
     *
44
     * @return SDIS62\Core\Ops\Entity\Centre
45
     */
46
    public function save($data, $id_materiel = null)
47
    {
48
        $centre = $this->centre_repository->find($data['centre']);
49
50
        if (empty($centre)) {
51
            return;
52
        }
53
54
        $materiel = empty($id_materiel) ? new Materiel($centre, $data['name']) : $this->materiel_repository->find($id_materiel);
55
56
        if (!empty($data['name'])) {
57
            $materiel->setName($data['name']);
58
        }
59
60
        if (!empty($data['centre'])) {
61
            $materiel->setCentre($centre);
62
        }
63
64
        if (!empty($data['statut'])) {
65
            $materiel->setStatut(Statut::getByName($data['statut']));
66
        }
67
68
        if (!empty($data['coordinates']) && is_array($data['coordinates']) && count($data['coordinates']) == 2) {
69
            $materiel->setCoordinates(new Coordinates($data['coordinates'][0], $data['coordinates'][1]));
70
        }
71
72
        $this->materiel_repository->save($materiel);
73
74
        return $materiel;
75
    }
76
77
    /**
78
     * Suppression d'un matériel.
79
     *
80
     * @param mixed $id_materiel
81
     *
82
     * @return SDIS62\Core\Ops\Entity\Materiel
83
     */
84
    public function delete($id_materiel)
85
    {
86
        $materiel = $this->find($id_materiel);
87
88
        if (empty($materiel)) {
89
            return;
90
        }
91
92
        $this->materiel_repository->delete($materiel);
93
94
        return $materiel;
95
    }
96
}
97