File size: 4,911 Bytes
12759cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php

class Disruption
{
    protected $impacts = [];
    protected $impacts_optimized = null;
    protected $id;
    protected $dateDayStart;
    protected $ligne;

    public function __construct($id, $dateDayStart, $ligne) {
        $this->id = $id;
        $this->dateDayStart = $dateDayStart;
        $this->ligne = $ligne;
    }

    public function isInProgress() {
        $current = new DateTime();

        return $current > $this->getDateStart() && $current < $this->getDateEnd();
    }

    public function isPast() {

        return new DateTime() > $this->getDateEnd();
    }

    public function isInFuture() {

        return new DateTime() < $this->getDateStart();
    }

    public function getCurrentColorClass() {
        foreach($this->impacts as $i) {
            return $i->getColorClass();
        }
    }

    public function getDateEnd() {
        $dateEnd = null;
        foreach($this->impacts as $i) {
            if($i->getDateEnd() > $dateEnd) {
                $dateEnd = $i->getDateEnd();
            }
        }

        return $dateEnd;
    }

    public function getDateStart() {

        return end($this->impacts)->getDateStart();
    }

    public function getDuration() {
        $dateEnd = $this->getDateEnd();

        if($this->getDateEnd() > new DateTime()) {

            $dateEnd = new DateTime();
        }

        return $dateEnd->diff($this->getDateStart());
    }

    public function getDurationText() {

        return Impact::generateDurationText($this->getDuration());
    }

    public function getCause() {
        foreach($this->impacts as $i) {
            return $i->getCause();
        }
    }

    public function getLigne() {

        return $this->ligne;
    }

    public function getId()
    {
        return $this->id;
    }

    public function getImpacts() {

        return $this->impacts;
    }

    public function addImpact($impact) {
        if(isset($this->impacts[$impact->getId()])) {
            $impact->setDateCreation($this->impacts[$impact->getId()]->getDateCreation());
        }

        $dateKey = $impact->getLastUpdate()->format('Y-m-d H:i:s');
        if($impact->getLastUpdate() < $this->dateDayStart) {
            $dateKey =  $impact->getDateStart()->format('Y-m-d H:i:s');
        }
        $this->impacts[$dateKey.$impact->getId()] = $impact;

        $nextImpact = null;
        krsort($this->impacts);
        foreach($this->impacts as $impact) {
            if($nextImpact && $nextImpact->isSameImpact($impact) && $impact->getDateEnd() > $nextImpact->getDateEnd()) {
                $impact->setDateEnd($nextImpact->getDateStart()->format('Ymd\THis'));
            }

            if($nextImpact && $nextImpact->isSameImpact($impact) && $nextImpact->getDateStart() > $impact->getDateEnd()) {
                $nextImpact = $impact;
                continue;
            }

            if($nextImpact && $nextImpact->isSameImpact($impact) && $impact->getDateEnd() > $nextImpact->getDateEnd()) {
                $impact->setDateEnd($nextImpact->getDateEnd()->format('Ymd\THis'));
            }

            if($nextImpact && $nextImpact->isSameImpact($impact) && $impact->getDateEnd() > $nextImpact->getDateStart()) {
                $impact->setDateEnd($nextImpact->getDateStart()->format('Ymd\THis'));
            }

            if($impact->getDateStart() > $impact->getDateEnd()) {
                $impact->setDateStart($impact->getDateEnd()->format('Ymd\THis'));
            }

            $nextImpact = $impact;
        }
    }

    public function optimize() {
        $this->impacts_optimized = $this->impacts;
        foreach($this->impacts_optimized as $key => $impact) {
            if(!isset($this->impacts_optimized[$key])) {
                continue;
            }
            foreach($this->impacts_optimized as $keyOther => $otherImpact) {
                if($key == $keyOther) {
                    continue;
                }
                if($otherImpact->isInPeriod($impact->getDateStart()) && $impact->getSeverity() == $otherImpact->getSeverity() && $impact->getTitle() == $otherImpact->getTitle()) {
                    $otherImpact->setDateEnd($impact->getDateEnd()->format('Ymd\THis'));
                    $otherImpact->data->message = $impact->data->message;
                    unset($this->impacts_optimized[$key]);
                }
            }
        }
    }

    public function getImpactsOptimized() {
        if(is_null($this->impacts_optimized)) {
            $this->optimize();
        }

        return $this->impacts_optimized;
    }

    public function getImpactsInPeriod($date) {
        $impacts = [];

        foreach($this->impacts as $impact) {
            if(!$impact->isInPeriod($date)) {
                continue;
            }
            $impacts[$impact->getDateCreation()->format('YMDHis').$impact->getId()] = $impact;
        }

        return $impacts;
    }
}