Passed
Push — master ( 0f7c6a...f2dd02 )
by Adam
11:07
created

NotificationRepository::takeForUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Coyote\Repositories\Eloquent;
4
5
use Carbon\Carbon;
6
use Coyote\Notification;
7
use Coyote\Notification\Setting;
8
use Coyote\Repositories\Contracts\NotificationRepositoryInterface;
9
use Illuminate\Database\Eloquent\Relations\HasMany;
10
11
/**
12
 * @package Coyote\Repositories\Eloquent
13
 */
14
class NotificationRepository extends Repository implements NotificationRepositoryInterface
15
{
16
    /**
17
     * @return string
18
     */
19
    public function model()
20
    {
21
        return Notification::class;
22
    }
23
24
    /**
25
     * @param int $userId
26
     * @param int $perPage
27
     * @return mixed
28
     */
29
    public function lengthAwarePaginate($userId, $perPage = 20)
30
    {
31
        return $this->prepare($userId)->paginate($perPage);
32
    }
33
34
    /**
35
     * @param int $userId
36
     * @param int $limit
37
     * @param int $offset
38
     * @return mixed
39
     */
40
    public function takeForUser($userId, $limit = 10, $offset = 0)
41
    {
42
        return $this->prepare($userId)->take($limit)->skip($offset)->get();
43
    }
44
45
    /**
46
     * Mark notifications as read
47
     *
48
     * @use Coyote\Http\Controllers\User\NotificationsController
49
     * @param array $id
50
     */
51
    public function markAsRead(array $id)
52
    {
53
        $this->model->whereIn('id', $id)->update(['read_at' => Carbon::now()]);
54
    }
55
56
    /**
57
     * Find notification by url and mark it as read
58
     *
59
     * @param int $userId
60
     * @param mixed $model
61
     */
62
    public function markAsReadByModel($userId, $model)
63
    {
64
        $this
65
            ->model
66
            ->where('user_id', $userId)
67
            ->whereNull('read_at')
68
            ->where('content_id', $model->id)
69
            ->where('content_type', class_basename($model))
70
            ->update(['read_at' => Carbon::now()]);
71
    }
72
73
    /**
74
     * Build query for repository
75
     *
76
     * @param int $userId
77
     * @return mixed
78
     */
79
    private function prepare($userId)
80
    {
81
        return $this
82
                ->model
83
                ->select(['notifications.*', 'notification_types.headline'])
84
                ->where('user_id', $userId)
85
                ->with(['senders' => function (HasMany $sql) {
86
                    $sql
87
                        ->select([
88
                            'notification_id',
89
                            'user_id',
90
                            $this->raw('COALESCE(users.name, notification_senders.name) AS name'),
91
                            'photo',
92
                            'is_blocked',
93
                            $this->raw('users.deleted_at IS NULL AS is_active')
94
                        ])
95
                        ->orderBy('notification_senders.id');
96
                }])
97
                ->join('notification_types', 'notification_types.id', '=', 'type_id')
98
                ->orderBy('notifications.created_at', 'DESC');
99
    }
100
101
    /**
102
     * Gets public notification types
103
     *
104
     * @return mixed
105
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
106
     */
107
    public function notificationTypes()
108
    {
109
        return $this
110
            ->app
111
            ->make(Notification\Type::class)
112
            ->select()
113
            ->where('is_public', 1)
114
            ->orderBy('notification_types.id')
115
            ->get();
116
    }
117
118
    /**
119
     * @param int $userId
120
     * @param array $data
121
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
122
     */
123
    public function updateSettings($userId, array $data)
124
    {
125
        $model = $this->app->make(Setting::class);
126
127
        foreach ($data as $id => $value) {
128
            $model->where('user_id', $userId)->where('id', $id)->update(['is_enabled' => $value]);
129
        }
130
    }
131
132
    public function purge(): void
133
    {
134
        $this->model->whereNotNull('read_at')->where('is_clicked', true)->where('created_at', '<', now()->subYear())->delete();
135
    }
136
}
137