EvenementService::create()   B
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 23
rs 8.5906
cc 5
eloc 14
nc 6
nop 2
1
<?php
2
3
namespace SDIS62\Core\Ops\Service;
4
5
use SDIS62\Core\Ops\Entity\Evenement;
6
use SDIS62\Core\Ops\Repository\EngagementRepositoryInterface;
7
use SDIS62\Core\Ops\Repository\EvenementRepositoryInterface;
8
use SDIS62\Core\Ops\Repository\InterventionRepositoryInterface;
9
10
class EvenementService
11
{
12
    /**
13
     * Initialisation du service avec les repository utilisés.
14
     *
15
     * @param SDIS62\Core\Ops\Repository\EvenementRepositoryInterface    $evenement_repository
16
     * @param SDIS62\Core\Ops\Repository\InterventionRepositoryInterface $intervention_repository
17
     */
18
    public function __construct(EvenementRepositoryInterface $evenement_repository,
19
                                InterventionRepositoryInterface $intervention_repository,
20
                                EngagementRepositoryInterface $engagement_repository
21
    ) {
22
        $this->evenement_repository    = $evenement_repository;
0 ignored issues
show
Bug introduced by
The property evenement_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->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...
24
        $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...
25
    }
26
27
    /**
28
     * Retourne un évenement.
29
     *
30
     * @param mixed $id_evenement
31
     *
32
     * @return SDIS62\Core\Ops\Entity\Evenement
33
     */
34
    public function find($id_evenement)
35
    {
36
        return $this->evenement_repository->find($id_evenement);
37
    }
38
39
    /**
40
     * Création d'un evenement sur une intervention.
41
     *
42
     * @param string $type
43
     * @param array  $data
44
     *
45
     * @return SDIS62\Core\Ops\Entity\Evenement
46
     */
47
    public function create($type, $data)
48
    {
49
        $evenement = new Evenement($data['description'], array_key_exists('date', $data) ? $data['date'] : null);
50
51
        switch ($type) {
52
            case 'intervention':
53
                $object = $this->intervention_repository->find($data['intervention']);
54
                break;
55
            case 'engagement':
56
                $object = $this->engagement_repository->find($data['engagement']);
57
                break;
58
        }
59
60
        if (empty($object)) {
61
            return;
62
        }
63
64
        $object->addEvenement($evenement);
65
66
        $this->evenement_repository->save($evenement);
67
68
        return $evenement;
69
    }
70
71
    /**
72
     * Suppression d'un evenement.
73
     *
74
     * @param mixed $id_evenement
75
     *
76
     * @return SDIS62\Core\Ops\Entity\Evenement
77
     */
78
    public function delete($id_evenement)
79
    {
80
        $evenement = $this->find($id_evenement);
81
82
        if (empty($evenement)) {
83
            return;
84
        }
85
86
        $this->evenement_repository->delete($evenement);
87
88
        return $evenement;
89
    }
90
}
91