PlageHoraireService::delete()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace SDIS62\Core\Ops\Service;
4
5
use Datetime;
6
use SDIS62\Core\Ops\Entity\PlageHoraire;
7
use SDIS62\Core\Ops\Exception\InvalidPlageHoraireTypeException;
8
use SDIS62\Core\Ops\Repository\PlageHoraireRepositoryInterface;
9
use SDIS62\Core\Ops\Repository\PlanningRepositoryInterface;
10
use SDIS62\Core\Ops\Repository\PompierRepositoryInterface;
11
12
class PlageHoraireService
13
{
14
    /**
15
     * Initialisation du service avec les repository utilisés.
16
     *
17
     * @param SDIS62\Core\Ops\Repository\PlageHoraireRepositoryInterface $plagehoraire_repository
18
     * @param SDIS62\Core\Ops\Repository\PompierRepositoryInterface      $pompier_repository
19
     * @param SDIS62\Core\Ops\Repository\PlanningRepositoryInterface     $planning_repository
20
     */
21
    public function __construct(PlageHoraireRepositoryInterface $plagehoraire_repository,
22
                                PompierRepositoryInterface $pompier_repository,
23
                                PlanningRepositoryInterface $planning_repository
24
    ) {
25
        $this->plagehoraire_repository = $plagehoraire_repository;
0 ignored issues
show
Bug introduced by
The property plagehoraire_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...
26
        $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...
27
        $this->planning_repository     = $planning_repository;
0 ignored issues
show
Bug introduced by
The property planning_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
    }
29
30
    /**
31
     * Retourne une plage horaire correspondant à l'id spécifié.
32
     *
33
     * @param mixed $id_plage_horaire
34
     *
35
     * @return SDIS62\Core\Ops\Entity\PlageHoraire
36
     */
37
    public function find($id_plage_horaire)
38
    {
39
        return $this->plagehoraire_repository->find($id_plage_horaire);
40
    }
41
42
    /**
43
     * Sauvegarde d'une plage horaire.
44
     *
45
     * @param array $data
46
     *
47
     * @return SDIS62\Core\Ops\Entity\PlageHoraire
48
     */
49
    public function save(array $data)
50
    {
51
        $planning = $this->planning_repository->find($data['planning']);
52
        $pompier  = $this->pompier_repository->find($data['pompier']);
53
        $start    = DateTime::createFromFormat('d-m-Y H:i', (string) $data['start']);
54
        $end      = DateTime::createFromFormat('d-m-Y H:i', (string) $data['end']);
55
56
        switch ($data['type']) {
57
            case 'garde' :
58
                $plage_horaire = new PlageHoraire\GardePlageHoraire($planning, $pompier, $start, $end);
0 ignored issues
show
Security Bug introduced by
It seems like $start defined by \Datetime::createFromFor...string) $data['start']) on line 53 can also be of type false; however, SDIS62\Core\Ops\Entity\PlageHoraire::__construct() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
Security Bug introduced by
It seems like $end defined by \Datetime::createFromFor... (string) $data['end']) on line 54 can also be of type false; however, SDIS62\Core\Ops\Entity\PlageHoraire::__construct() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
59
                break;
60
            case 'dispo' :
61
                $plage_horaire = new PlageHoraire\DispoPlageHoraire($planning, $pompier, $start, $end);
0 ignored issues
show
Security Bug introduced by
It seems like $start defined by \Datetime::createFromFor...string) $data['start']) on line 53 can also be of type false; however, SDIS62\Core\Ops\Entity\PlageHoraire::__construct() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
Security Bug introduced by
It seems like $end defined by \Datetime::createFromFor... (string) $data['end']) on line 54 can also be of type false; however, SDIS62\Core\Ops\Entity\PlageHoraire::__construct() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
62
                break;
63
            default:
64
                throw new InvalidPlageHoraireTypeException();
65
        }
66
67
        $this->plagehoraire_repository->save($plage_horaire);
68
69
        return $plage_horaire;
70
    }
71
72
    /**
73
     * Suppression d'une plage horaire.
74
     *
75
     * @param mixed $id_plage_horaire
76
     */
77
    public function delete($id_plage_horaire)
78
    {
79
        $plage_horaire = $this->find($id_plage_horaire);
80
81
        if (empty($plage_horaire)) {
82
            return;
83
        }
84
85
        $this->plagehoraire_repository->delete($plage_horaire);
86
87
        return $plage_horaire;
88
    }
89
}
90