findMissingBookingsForUser()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 3
eloc 7
nc 3
nop 2
1
<?php
2
3
namespace JhFlexiTime\Service;
4
5
use JhFlexiTime\Notification\MissingBookingsNotification;
6
use JhFlexiTime\Options\NotificationOptions;
7
use JhFlexiTime\Repository\BookingRepositoryInterface;
8
use JhFlexiTime\Repository\UserSettingsRepositoryInterface;
9
use JhHubBase\Notification\NotificationService;
10
use JhUser\Repository\UserRepositoryInterface;
11
use Zend\View\Model\ViewModel;
12
use ZfcUser\Entity\UserInterface;
13
14
/**
15
 * Class MissingBookingReminderService
16
 * @package JhFlexiTime\Service
17
 * @author  Aydin Hassan <[email protected]>
18
 */
19
class MissingBookingReminderService
20
{
21
    /**
22
     * @var NotificationService
23
     */
24
    private $notificationService;
25
26
    /**
27
     * @var UserRepositoryInterface
28
     */
29
    private $userRepository;
30
31
    /**
32
     * @var UserSettingsRepositoryInterface
33
     */
34
    private $userSettingsRepository;
35
36
    /**
37
     * @var BookingRepositoryInterface
38
     */
39
    private $bookingRepository;
40
41
    /**
42
     * @var NotificationOptions
43
     */
44
    private $options;
45
46
    /**
47
     * @param NotificationService               $notificationService
48
     * @param UserRepositoryInterface           $userRepository
49
     * @param UserSettingsRepositoryInterface   $userSettingsRepository
50
     * @param BookingRepositoryInterface        $bookingRepository
51
     * @param NotificationOptions               $options
52
     */
53
    public function __construct(
54
        NotificationService $notificationService,
55
        UserRepositoryInterface $userRepository,
56
        UserSettingsRepositoryInterface $userSettingsRepository,
57
        BookingRepositoryInterface $bookingRepository,
58
        NotificationOptions $options
59
    ) {
60
        $this->notificationService = $notificationService;
61
        $this->userRepository = $userRepository;
62
        $this->userSettingsRepository = $userSettingsRepository;
63
        $this->bookingRepository = $bookingRepository;
64
        $this->options = $options;
65
    }
66
67
    /**
68
     * Find And Notify Users of any missing bookings
69
     */
70
    public function findAndNotifyMissingBookings()
71
    {
72
        $period = $this->options->getRemindPeriod();
73
        foreach ($this->userRepository->findAll() as $user) {
74
            $missingBookings = $this->findMissingBookingsForUser($user, $period);
75
76
            if (count($missingBookings) > 0) {
77
                $notification = new MissingBookingsNotification($period, $missingBookings);
78
                $this->notificationService->notify($notification, $user);
79
            }
80
        }
81
    }
82
83
    /**
84
     * @param UserInterface $user
85
     * @param array         $period
86
     *
87
     * @return array
88
     */
89
    public function findMissingBookingsForUser(UserInterface $user, array $period)
90
    {
91
        $missingBookings = [];
92
        foreach ($period as $date) {
93
            $booking = $this->bookingRepository->findOneBy(['user' => $user, 'date' => $date]);
94
95
            if (null === $booking) {
96
                $missingBookings[] = $date;
97
            }
98
        }
99
100
        return $missingBookings;
101
    }
102
}
103