CentreService   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 6
Bugs 2 Features 1
Metric Value
wmc 12
c 6
b 2
f 1
lcom 1
cbo 1
dl 0
loc 113
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getAll() 0 4 1
A find() 0 4 1
A findAllByName() 0 4 1
B save() 0 30 6
A delete() 0 12 2
1
<?php
2
3
namespace SDIS62\Core\Ops\Service;
4
5
use SDIS62\Core\Ops\Entity\Centre;
6
use SDIS62\Core\Ops\Repository\CentreRepositoryInterface;
7
use SDIS62\Core\Ops\Repository\CommuneRepositoryInterface;
8
9
class CentreService
10
{
11
    /**
12
     * Initialisation du service avec les repository utilisés.
13
     *
14
     * @param SDIS62\Core\Ops\Repository\CentreRepositoryInterface  $centre_repository
15
     * @param SDIS62\Core\Ops\Repository\CommuneRepositoryInterface $commune_repository
16
     */
17
    public function __construct(CentreRepositoryInterface $centre_repository,
18
                                CommuneRepositoryInterface $commune_repository
19
    ) {
20
        $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...
21
        $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...
22
    }
23
24
    /**
25
     * Retourne un ensemble de CIS.
26
     *
27
     * @param int $count Par défaut: 20
28
     * @param int $page  Par défaut: 1
29
     *
30
     * @return SDIS62\Core\User\Entity\Centre[]
31
     */
32
    public function getAll($count = 20, $page = 1)
33
    {
34
        return $this->centre_repository->getAll($count, $page);
35
    }
36
37
    /**
38
     * Retourne un CIS correspondant à l'id spécifié.
39
     *
40
     * @param mixed $id_centre
41
     *
42
     * @return SDIS62\Core\Ops\Entity\Centre
43
     */
44
    public function find($id_centre)
45
    {
46
        return $this->centre_repository->find($id_centre);
47
    }
48
49
    /**
50
     * Retourne des CIS correspondant au nom spécifié.
51
     *
52
     * @param string $name
53
     * @param int    $count Par défaut: 20
54
     * @param int    $page  Par défaut: 1
55
     *
56
     * @return SDIS62\Core\Ops\Entity\Centre[]
57
     */
58
    public function findAllByName($name, $count = 20, $page = 1)
59
    {
60
        return $this->centre_repository->findAllByName($name, $count, $page);
61
    }
62
63
    /**
64
     * Sauvegarde d'un centre.
65
     *
66
     * @param array $data
67
     * @param mixed $id_centre Optionnel
68
     *
69
     * @return SDIS62\Core\Ops\Entity\Centre
70
     */
71
    public function save($data, $id_centre = null)
72
    {
73
        if (empty($id_centre)) {
74
            $commune = $this->commune_repository->find($data['commune']);
75
76
            if (empty($commune)) {
77
                return;
78
            }
79
80
            $centre = new Centre($commune, $data['name']);
81
        } else {
82
            $centre = $this->centre_repository->find($id_centre);
83
        }
84
85
        if (empty($centre)) {
86
            return;
87
        }
88
89
        if (!empty($data['name'])) {
90
            $centre->setName($data['name']);
91
        }
92
93
        if (!empty($data['classement'])) {
94
            $centre->setClassement($data['classement']);
95
        }
96
97
        $this->centre_repository->save($centre);
98
99
        return $centre;
100
    }
101
102
    /**
103
     * Suppression d'un centre.
104
     *
105
     * @param mixed $id_centre
106
     *
107
     * @return SDIS62\Core\Ops\Entity\Centre
108
     */
109
    public function delete($id_centre)
110
    {
111
        $centre = $this->find($id_centre);
112
113
        if (empty($centre)) {
114
            return;
115
        }
116
117
        $this->centre_repository->delete($centre);
118
119
        return $centre;
120
    }
121
}
122