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