PreferenceService   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 18
eloc 39
c 2
b 0
f 2
dl 0
loc 90
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A shouldSendToChannel() 0 15 4
A getDefaultPreferences() 0 19 5
A getUserPreferences() 0 17 4
A getChannelsForEvent() 0 19 5
1
<?php
2
3
namespace Usamamuneerchaudhary\Notifier\Services;
4
5
use Usamamuneerchaudhary\Notifier\Models\NotificationChannel;
6
use Usamamuneerchaudhary\Notifier\Models\NotificationEvent;
7
use Usamamuneerchaudhary\Notifier\Models\NotificationPreference;
8
use Usamamuneerchaudhary\Notifier\Models\NotificationSetting;
9
10
class PreferenceService
11
{
12
    /**
13
     * Get user preferences for a specific event
14
     */
15
    public function getUserPreferences($user, string $eventKey): array
16
    {
17
        $event = NotificationEvent::where('key', $eventKey)->first();
18
19
        if (!$event) {
20
            return [];
21
        }
22
23
        $preference = NotificationPreference::where('user_id', $user->id)
24
            ->where('notification_event_id', $event->id)
25
            ->first();
26
27
        if ($preference && isset($preference->channels)) {
28
            return $preference->channels;
29
        }
30
31
        return $this->getDefaultPreferences($event);
32
    }
33
34
    /**
35
     * Get channels configuration for an event (includes all active channels)
36
     */
37
    public function getChannelsForEvent(NotificationEvent $event, ?NotificationPreference $preference): array
38
    {
39
        if ($preference && isset($preference->channels)) {
40
            $channels = $preference->channels;
41
        } else {
42
            $channels = $this->getDefaultPreferences($event);
43
        }
44
45
        $activeChannels = NotificationChannel::where('is_active', true)
46
            ->pluck('type')
47
            ->toArray();
48
49
        foreach ($activeChannels as $channelType) {
50
            if (!isset($channels[$channelType])) {
51
                $channels[$channelType] = false;
52
            }
53
        }
54
55
        return $channels;
56
    }
57
58
    /**
59
     * Get default preferences for an event based on settings
60
     */
61
    protected function getDefaultPreferences(NotificationEvent $event): array
62
    {
63
        $defaultChannels = NotificationSetting::get(
64
            'preferences.default_channels',
65
            config('notifier.settings.preferences.default_channels', ['email'])
66
        );
67
68
        $preferences = [];
69
        foreach ($defaultChannels as $channel) {
70
            $preferences[$channel] = true;
71
        }
72
73
        if (isset($event->settings['channels']) && is_array($event->settings['channels'])) {
74
            foreach ($event->settings['channels'] as $channel) {
75
                $preferences[$channel] = true;
76
            }
77
        }
78
79
        return $preferences;
80
    }
81
82
    /**
83
     * Check if notification should be sent to a specific channel
84
     */
85
    public function shouldSendToChannel($user, string $channelType, array $preferences): bool
0 ignored issues
show
Unused Code introduced by
The parameter $user is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

85
    public function shouldSendToChannel(/** @scrutinizer ignore-unused */ $user, string $channelType, array $preferences): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
86
    {
87
        $channel = NotificationChannel::where('type', $channelType)
88
            ->where('is_active', true)
89
            ->first();
90
91
        if (!$channel) {
92
            return false;
93
        }
94
95
        if (isset($preferences[$channelType]) && !$preferences[$channelType]) {
96
            return false;
97
        }
98
99
        return true;
100
    }
101
}
102