Calendar::callClass()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 8
cp 0
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace SET\Handlers\Calendar;
4
5
use Carbon\Carbon;
6
use SET\Handlers\DateFormat;
7
use SET\TrainingUser;
8
use SET\Travel;
9
use SET\User;
10
11
class Calendar
12
{
13
    use DateFormat;
14
15
    private $start;
16
    private $end;
17
    private $calendarArray;
18
    private $test;
19
20
    /**
21
     * Name of function that generates a collection followed by an array of dates
22
     * we want to mark in our calendar/agenda.
23
     *
24
     * @var array
25
     */
26
    private $lists = [
27
        Separated::class     => ['destroyed_date'],
28
        Travels::class       => ['leave_date', 'return_date'],
29
        TrainingUsers::class => ['due_date'],
30
        NewUser::class       => ['created_at'],
31
    ];
32
33
    public function __construct()
34
    {
35
        $this->start = Carbon::today()->subWeeks(1);
36
        $this->end = Carbon::today()->addMonths(2);
37
        $this->calendarArray = [];
38
39
        $this->generateCalendarItems();
40
    }
41
42
    public function getCalendar()
43
    {
44
        return $this->calendarArray;
45
    }
46
47
    private function generateCalendarItems()
48
    {
49
50
        //Let's build that calendar.
51
        $date = $this->start;
52
        $iterator = 0;
53
54
        $list = $this->callClass();
55
56
        while ($date <= $this->end) {
57
            $currentDate = $date->format('Y-m-d');
58
            $this->test = false;
59
            $list2 = [];
60
61
            foreach ($this->lists as $functionName => $columns) {
62
                $list2[$functionName] = $this->buildArrayByDate($list[$functionName], $columns, $currentDate);
63
            }
64
65
            if ($this->test || $currentDate == Carbon::today()->format('Y-m-d')) {
66
                $this->calendarArray[$iterator] = [
67
                    'date'         => $currentDate,
68
                    'separated'    => $list2[Separated::class],
69
                    'travel'       => $list2[Travels::class],
70
                    'trainingUser' => $this->groupUsersForTraining($list2[TrainingUsers::class]),
71
                    'newUser'      => $list2[NewUser::class],
72
                ];
73
            }
74
75
            $date->addDay();
76
            $iterator++;
77
        }
78
    }
79
80
    private function buildArrayByDate($list, array $columnName, $date)
81
    {
82
        $array = [];
83
84
        foreach ($list as $item) {
85
            foreach ($columnName as $column) {
86
                $dbDate = $this->dateFormat($item[$column]);
87
                if ($date == $dbDate) {
88
                    $this->test = true;
89
                    array_push($array, $item);
90
                }
91
            }
92
        }
93
94
        return $array;
95
    }
96
97
    private function groupUsersForTraining($trainingUsers)
98
    {
99
        $array = [];
100
        foreach ($trainingUsers as $key => $item) {
101
            $array[$item['training_id']][$key] = $item;
102
        }
103
        ksort($array, SORT_NUMERIC);
104
105
        foreach ($array as $training) {
106
            foreach ($training as $trainingUser) {
107
                $trainingUser['userLink'] = "<a href='".url('user', $trainingUser->user_id)."'>".$trainingUser->user->userFullName.'</a>';
108
            }
109
        }
110
111
        return $array;
112
    }
113
114
    /**
115
     * @return array
116
     */
117
    private function callClass()
118
    {
119
        $array = [];
120
        foreach ($this->lists as $class => $columns) {
121
            $array[$class] = (new $class($this->start, $this->end))->getList();
122
        }
123
124
        return $array;
125
    }
126
}
127