Completed
Push — master ( 28d05b...16bc73 )
by Raphaël
02:44
created

arrayRecursiveDiff()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 2
1
<?php
2
3
namespace Wabel\CertainAPI\Services;
4
5
use Wabel\CertainAPI\Ressources\AppointmentsCertain;
6
7
class DetectAppointmentsChangingsService
8
{
9
10
    /**
11
     * @var AppointmentsCertain
12
     */
13
    private $appointmentsCertain;
14
15
    public function __construct(AppointmentsCertain $appointmentsCertain)
16
    {
17
        $this->appointmentsCertain = $appointmentsCertain;
18
    }
19
20
    /**
21
     * @param $eventCode
22
     * @param null|int $start
23
     * @param null|int $maxResult
24
     * @return mixed
25
     */
26
    public function getCurrentAppoiments($eventCode,$start=null,$maxResult=null){
27
        if(!$start){
0 ignored issues
show
Bug Best Practice introduced by
The expression $start of type null|integer is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
28
            $start = 0;
29
        }
30
        if(!$maxResult){
0 ignored issues
show
Bug Best Practice introduced by
The expression $maxResult of type null|integer is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
31
            $maxResult = $this->appointmentsCertain->get($eventCode,['start_index'=>0,'max_results'=>1])->getMaxResults();
32
        }
33
        return $this->certainAppointmentsList = $this->appointmentsCertain->get($eventCode,['start_index'=>$start,'max_results'=>$maxResult])->getResults()->appointments;
0 ignored issues
show
Bug introduced by
The property certainAppointmentsList 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...
34
    }
35
36
    /**
37
     * @param array $appointmentsOld
38
     * @param array $appointmentsNew
39
     * @return bool
40
     */
41
    public function hasChanged(array $appointmentsOld,array $appointmentsNew){
42
        $hasChanged = false;
43
        $appointmentsOld = self::recursiveArrayObjectToFullArray($appointmentsOld);
44
        $appointmentsNew = self::recursiveArrayObjectToFullArray($appointmentsNew);
45
        foreach ($appointmentsOld as $appointmentOld){
46
            if(!in_array($appointmentOld,$appointmentsNew)){
47
                $hasChanged = true;
48
                break;
49
            }
50
        }
51
        return $hasChanged;
52
    }
53
54
    /**
55
     * @param $object
56
     * @return array
57
     */
58
    public static  function objectToArray($object) {
59
        if(is_object($object)){
60
61
            return (array) $object;
62
        }
63
        return $object;
64
    }
65
66
    /**
67
     * @param $appointments
68
     * @return array
69
     */
70
    public static function recursiveArrayObjectToFullArray($appointments){
71
        return json_decode(json_encode($appointments), true);
72
    }
73
74
    /**
75
     * @param array $arrayOlds
76
     * @param array $arrayNews
77
     * @return array
78
     */
79
    private function arrayRecursiveDiff(array $arrayOlds, array $arrayNews) {
80
        $difference = [];
81
        foreach($arrayOlds as $key => $arrayOld){
82
            if(!in_array($arrayOld,$arrayNews)){
83
                $difference[$key] = $arrayOld;
84
            }
85
        }
86
        return $difference;
87
    }
88
89
    /**
90
     * @param array $appointmentsOld
91
     * @param array $appointmentsNew
92
     * @return array
93
     */
94
    public function getListChangings(array $appointmentsOld,array $appointmentsNew){
95
        $appointmentsOld = self::recursiveArrayObjectToFullArray($appointmentsOld);
96
        $appointmentsNew = self::recursiveArrayObjectToFullArray($appointmentsNew);
97
        $changesList = [];
98
        if($this->hasChanged($appointmentsOld,$appointmentsNew)){
99
            $changesList = self::recursiveArrayObjectToFullArray($this->arrayRecursiveDiff($appointmentsOld,$appointmentsNew));
100
        }
101
        return $changesList;
102
    }
103
104
    /**
105
     *
106
     * @param array $currentAppointments
107
     * @param array $changingsDetected
108
     * @return array ['deleted'=>[],'updated'=>[]]
109
     */
110
    public function detectDeleteOrUpdated(array $currentAppointments,array $changingsDetected){
111
        $delete = [];
112
        $update = [];
113
        //@Todo: Detect Fields has changed
114
        $appointmentsNew = self::recursiveArrayObjectToFullArray($currentAppointments);
115
        $changings = self::recursiveArrayObjectToFullArray($changingsDetected);
116
        foreach ($changings as $changing){
117
            $registration = $changing['registration']['regCode'];
118
            $registrationTarget = $changing['targetRegistration']['regCode'];
119
            foreach ($appointmentsNew as $currentAppointment){
120
                $registrationCurrent = $currentAppointment['registration']['regCode'];
121
                $registrationTargetCurrent = $currentAppointment['targetRegistration']['regCode'];
122
                if(in_array($registration,[$registrationCurrent,$registrationTargetCurrent])
123
                    && in_array($registrationTarget,[$registrationCurrent,$registrationTargetCurrent])
124
                    && !in_array($changing,$update) && !in_array($changing,$delete)) {
125
                    $update[] = $changing;
126
                    break;
127
                }
128
            }
129
            if(!in_array($changing,$update) && !in_array($changing,$delete)){
130
                $delete[] = $changing;
131
            }
132
        }
133
        return [
134
            'deleted' => $delete,
135
            'updated' => $update
136
        ];
137
    }
138
139
    /**
140
     * @param array $appointmentsOld
141
     * @param array $appointmentsNew
142
     * @return array ['deleted'=>[],'updated'=>[]]
143
     */
144
    public function detectAppointmentsChangings(array $appointmentsOld,array $appointmentsNew){
145
        $changings = $this->getListChangings($appointmentsOld,$appointmentsNew);
146
        $changesList = $this->detectDeleteOrUpdated($appointmentsNew,$changings);
147
        return $changesList;
148
    }
149
150
}