1
1
<?php
2
2
3
+ /*
4
+ * This file is part of fof/links.
5
+ *
6
+ * Copyright (c) FriendsOfFlarum.
7
+ *
8
+ * For the full copyright and license information, please view the LICENSE
9
+ * file that was distributed with this source code.
10
+ */
11
+
3
12
namespace FoF \Links ;
4
13
5
14
use Flarum \Locale \Translator ;
@@ -54,7 +63,7 @@ public function queryVisibleTo(?User $actor = null): Builder
54
63
/**
55
64
* Find a link by ID.
56
65
*
57
- * @param int $id
66
+ * @param int $id
58
67
* @param User|null $actor
59
68
*
60
69
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
@@ -66,6 +75,7 @@ public function findOrFail($id, ?User $actor = null): Link
66
75
$ query = Link::where ('id ' , $ id );
67
76
/** @var Link $link */
68
77
$ link = $ this ->scopeVisibleTo ($ query , $ actor )->firstOrFail ();
78
+
69
79
return $ link ;
70
80
}
71
81
@@ -81,13 +91,14 @@ public function all(?User $user = null): EloquentCollection
81
91
$ query = Link::query ();
82
92
/** @var EloquentCollection<Link> $links */
83
93
$ links = $ this ->scopeVisibleTo ($ query , $ user )->get ();
94
+
84
95
return $ links ;
85
96
}
86
97
87
98
/**
88
99
* Scope a query to only include records that are visible to a user.
89
100
*
90
- * @param Builder $query
101
+ * @param Builder $query
91
102
* @param User|null $user
92
103
*
93
104
* @return Builder
@@ -97,6 +108,7 @@ protected function scopeVisibleTo(Builder $query, ?User $user = null): Builder
97
108
if ($ user !== null ) {
98
109
$ query ->whereVisibleTo ($ user );
99
110
}
111
+
100
112
return $ query ;
101
113
}
102
114
@@ -112,7 +124,7 @@ protected function scopeVisibleTo(Builder $query, ?User $user = null): Builder
112
124
public function cacheKey (User $ actor ): string
113
125
{
114
126
if ($ actor ->isGuest ()) {
115
- return self ::$ cacheKeyPrefix . self ::$ cacheGuestLinksKey ;
127
+ return self ::$ cacheKeyPrefix. self ::$ cacheGuestLinksKey ;
116
128
} else {
117
129
throw new \InvalidArgumentException ('Only guests can have cached links at this time. ' );
118
130
}
@@ -137,8 +149,10 @@ public function getLinks(User $actor): EloquentCollection
137
149
return $ link ->guest_only ;
138
150
});
139
151
}
152
+
140
153
return new EloquentCollection ($ links ->all ());
141
154
}
155
+
142
156
return $ this ->getLinksFromDatabase ($ actor );
143
157
}
144
158
@@ -167,6 +181,7 @@ public function getGuestLinks(User $actor): EloquentCollection
167
181
} else {
168
182
$ links = $ this ->getLinksFromDatabase ($ actor );
169
183
$ this ->cache ->forever ($ this ->cacheKey ($ actor ), $ links );
184
+
170
185
return $ links ;
171
186
}
172
187
}
@@ -190,7 +205,7 @@ protected function getLinksFromDatabase(User $actor): EloquentCollection
190
205
*/
191
206
public function clearLinksCache (): void
192
207
{
193
- $ this ->cache ->forget (self ::$ cacheKeyPrefix . self ::$ cacheGuestLinksKey );
208
+ $ this ->cache ->forget (self ::$ cacheKeyPrefix. self ::$ cacheGuestLinksKey );
194
209
}
195
210
196
211
/**
@@ -233,6 +248,7 @@ protected function buildLinkFromDefinition(LinkDefinition $definition): Link
233
248
$ link ->exists = true ;
234
249
$ link ->syncOriginal ();
235
250
$ link ->makeVisible ('id ' );
251
+
236
252
return $ link ;
237
253
}
238
254
}
0 commit comments