Completed
Pull Request — master (#83)
by Sebastian
04:01
created

NewsItemRepository::findByUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Repositories;
4
5
use App\Models\NewsItem;
6
use Illuminate\Contracts\Pagination\Paginator;
7
use Illuminate\Support\Collection;
8
9
class NewsItemRepository
10
{
11
    public static function getAllOnline(): Collection
12
    {
13
        return NewsItem::online()
14
            ->orderBy('publish_date', 'desc')
15
            ->get();
16
    }
17
18
    public static function getLatest(int $amount): Collection
19
    {
20
        return NewsItem::online()
21
            ->orderBy('publish_date', 'desc')
22
            ->take($amount)
23
            ->get();
24
    }
25
26
    public static function findOnline(int $id): NewsItem
27
    {
28
        return NewsItem::online()->findOrFail($id);
29
    }
30
31
    public static function findByUrl(string $url): NewsItem
32
    {
33
        return NewsItem::online()
34
            ->where('url->'.content_locale(), $url)
35
            ->firstOrFail();
36
    }
37
38
    /**
39
     * @return \App\Models\NewsItem|null
40
     */
41
    public static function findNext(NewsItem $newsItem)
42
    {
43
        return NewsItem::online()
44
            ->where('publish_date', '>', $newsItem->publish_date)
45
            ->orderBy('publish_date', 'desc')
46
            ->first();
47
    }
48
49
    /**
50
     * @return \App\Models\NewsItem|null
51
     */
52
    public static function findPrevious(NewsItem $newsItem)
53
    {
54
        return NewsItem::online()
55
            ->where('publish_date', '<', $newsItem->publish_date)
56
            ->orderBy('publish_date', 'desc')
57
            ->first();
58
    }
59
60
    public static function paginate(int $perPage): Paginator
61
    {
62
        return NewsItem::online()->paginate($perPage);
63
    }
64
}
65