Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
66 / 66
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
Shifts
100.00% covered (success)
100.00%
66 / 66
100.00% covered (success)
100.00%
4 / 4
18
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 deletingCreateWorklogs
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
4
 deletingSendEmails
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
3
 updatedSendEmail
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
1 / 1
10
1<?php
2
3declare(strict_types=1);
4
5namespace Engelsystem\Events\Listener;
6
7use Carbon\Carbon;
8use Engelsystem\Mail\EngelsystemMailer;
9use Engelsystem\Models\Shifts\Shift;
10use Engelsystem\Models\Shifts\ShiftEntry;
11use Engelsystem\Models\Worklog;
12use Illuminate\Database\Eloquent\Collection;
13use Psr\Log\LoggerInterface;
14
15class Shifts
16{
17    public function __construct(
18        protected LoggerInterface $log,
19        protected EngelsystemMailer $mailer
20    ) {
21    }
22
23    public function deletingCreateWorklogs(Shift $shift): void
24    {
25        foreach ($shift->shiftEntries as $entry) {
26            if ($entry->freeloaded || $shift->start > Carbon::now()) {
27                continue;
28            }
29
30            $worklog = new Worklog();
31            $worklog->user()->associate($entry->user);
32            $worklog->creator()->associate(auth()->user());
33            $worklog->worked_at = $shift->start->copy()->startOfDay();
34            $worklog->hours =
35                (($shift->end->timestamp - $shift->start->timestamp) / 60 / 60)
36                * $shift->getNightShiftMultiplier();
37            $worklog->comment = sprintf(
38                __('%s (%s as %s) in %s, %s - %s'),
39                $shift->shiftType->name,
40                $shift->title,
41                $entry->angelType->name,
42                $shift->location->name,
43                $shift->start->format(__('general.datetime')),
44                $shift->end->format(__('general.datetime'))
45            );
46            $worklog->save();
47
48            $this->log->info(
49                'Created worklog entry from shift for {user} ({uid}): {worklog})',
50                ['user' => $worklog->user->name, 'uid' => $worklog->user->id, 'worklog' => $worklog->comment]
51            );
52        }
53    }
54
55    public function deletingSendEmails(Shift $shift): void
56    {
57        foreach ($shift->shiftEntries as $entry) {
58            if (!$entry->user->settings->email_shiftinfo) {
59                continue;
60            }
61
62            $this->mailer->sendViewTranslated(
63                $entry->user,
64                'notification.shift.deleted',
65                'emails/worklog-from-shift',
66                [
67                    'shift' => $shift,
68                    'entry' => $entry,
69                    'username' => $entry->user->displayName,
70                ]
71            );
72        }
73    }
74
75    public function updatedSendEmail(Shift $shift, Shift $oldShift): void
76    {
77        // Only send e-mail on relevant changes
78        if (
79            $oldShift->shift_type_id == $shift->shift_type_id
80            && $oldShift->title == $shift->title
81            && $oldShift->start == $shift->start
82            && $oldShift->end == $shift->end
83            && $oldShift->location_id == $shift->location_id
84        ) {
85            return;
86        }
87
88        $shift->load(['shiftType', 'location']);
89        $oldShift->load(['shiftType', 'location']);
90        /** @var ShiftEntry[]|Collection $shiftEntries */
91        $shiftEntries = $shift->shiftEntries()
92            ->with(['angelType', 'user.settings'])
93            ->get();
94
95        foreach ($shiftEntries as $shiftEntry) {
96            $user = $shiftEntry->user;
97            $angelType = $shiftEntry->angelType;
98
99            if (
100                !$user->settings->email_shiftinfo
101                || $shift->end < Carbon::now() && $oldShift->end < Carbon::now()
102            ) {
103                continue;
104            }
105
106            $this->mailer->sendViewTranslated(
107                $user,
108                'notification.shift.updated',
109                'emails/updated-shift',
110                [
111                    'shift' => $shift,
112                    'oldShift' => $oldShift,
113                    'angelType' => $angelType,
114                    'username' => $user->displayName,
115                ]
116            );
117        }
118    }
119}