ProfileService::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php
2
3
namespace SDIS62\Core\User\Service;
4
5
use SDIS62\Core\User\Entity\Profile;
6
use SDIS62\Core\User\Exception\InvalidProfileException;
7
use SDIS62\Core\User\Repository\UserRepositoryInterface;
8
use SDIS62\Core\User\Repository\ProfileRepositoryInterface;
9
10
class ProfileService
11
{
12
    /**
13
     * Initialisation du service avec les repository utilisés
14
     *
15
     * @param SDIS62\Core\User\Repository\ProfileRepositoryInterface $profile_repository
16
     * @param SDIS62\Core\User\Repository\UserRepositoryInterface    $user_repository
17
     */
18 18
    public function __construct(ProfileRepositoryInterface $profile_repository,
19
                                UserRepositoryInterface $user_repository
20
    ) {
21 18
        $this->profile_repository = $profile_repository;
0 ignored issues
show
Bug introduced by
The property profile_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 18
        $this->user_repository = $user_repository;
0 ignored issues
show
Bug introduced by
The property user_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 18
    }
24
25
    /**
26
     * Retourne un grade correspondant à l'id spécifié
27
     *
28
     * @param  int                             $id_profile
29
     * @return SDIS62\Core\User\Entity\Profile
30
     */
31 6
    public function find($id_profile)
32
    {
33 6
        return $this->profile_repository->find($id_profile);
34
    }
35
36
    /**
37
     * Retourne les profils d'un utilisateur
38
     *
39
     * @param  int                               $id_user
40
     * @return SDIS62\Core\User\Entity\Profile[]
41
     */
42 3
    public function findAllByUser($id_user)
43
    {
44 3
        return $this->profile_repository->findAllByUser($id_user);
45
    }
46
47
    /**
48
     * Sauvegarde d'un profil
49
     *
50
     * <code>
51
     * $array = array(
52
     *     'type' => 'sapeur_pompier|personnel_administratif|personnel_technique|maire',
53
     *     'grade' => 'Nom du grade (pour sapeur_pompier|personnel_administratif|personnel_technique)',
54
     *     'poste' => 'Nom du poste (pour sapeur_pompier|personnel_administratif|personnel_technique)',
55
     *     'pro' => 'true (pour sapeur_pompier)',
56
     *     'code_insee' => '62001 (pour maire)'
57
     * );
58
     * </code>
59
     *
60
     * @param  array                           $data
61
     * @return SDIS62\Core\User\Entity\Profile
62
     */
63 6
    public function save(array $data)
64
    {
65 6
        if (empty($data['id'])) {
66 6
            switch ($data['type']) {
67 4
                case 'sapeur_pompier' :
68 3
                    $profile = new Profile\Sdis\SapeurPompierSdisProfile($this->user_repository->find($data['user']), $data['grade'], $data['poste'], empty($data['pro']) ? false : (bool) $data['pro']);
69 3
                    break;
70 4
                case 'personnel_administratif' :
71 3
                    $profile = new Profile\Sdis\PersonnelAdministratifSdisProfile($this->user_repository->find($data['user']), $data['grade'], $data['poste']);
72 3
                    break;
73 4
                case 'personnel_technique' :
74 3
                    $profile = new Profile\Sdis\PersonnelTechniqueSdisProfile($this->user_repository->find($data['user']), $data['grade'], $data['poste']);
75 3
                    break;
76 5
                case 'maire' :
77 3
                    $profile = new Profile\MaireProfile($this->user_repository->find($data['user']), $data['code_insee']);
78 3
                    break;
79 2
                default:
80 4
                    throw new InvalidProfileException();
81 4
            }
82 2
        } else {
83 3
            $profile = $this->profile_repository->find($data['id']);
84
85 3
            if (!empty($data['code_insee'])) {
86 3
                $profile->setCodeInsee($data['code_insee']);
87 2
            }
88
89 3
            if (!empty($data['poste'])) {
90 3
                $profile->setPoste($data['poste']);
91 2
            }
92
93 3
            if (!empty($data['pro'])) {
94 3
                $profile->setPro($data['pro']);
95 2
            }
96
97 3
            if (!empty($data['grade'])) {
98 3
                $profile->promote($data['grade']);
99 2
            }
100
        }
101
102 3
        $this->profile_repository->save($profile);
103
104 3
        return $profile;
105
    }
106
107
    /**
108
     * Suppression d'un profil
109
     *
110
     * @param int $id_profile
111
     */
112 3
    public function delete($id_profile)
113
    {
114 3
        $profile = $this->find($id_profile);
115 3
        $this->profile_repository->delete($profile);
116 3
    }
117
}
118