Completed
Push — master ( 6cf466...e3dd7f )
by Freek
03:31
created

NewsItemRepository::getAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
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 getAll(): Collection
12
    {
13
        return NewsItem::orderBy('publish_date', 'desc')->get();
14
    }
15
16
    public static function getLatest(int $amount): Collection
17
    {
18
        return NewsItem::orderBy('publish_date', 'desc')
19
            ->take($amount)
20
            ->get();
21
    }
22
23
    public static function findById(int $id): NewsItem
24
    {
25
        return NewsItem::findOrFail($id);
26
    }
27
28
    public static function findBySlug(string $slug): NewsItem
29
    {
30
        return NewsItem::where('slug->'.content_locale(), $slug)->firstOrFail();
31
    }
32
33
    /**
34
     * @return \App\Models\NewsItem|null
35
     */
36
    public static function findNext(NewsItem $newsItem)
37
    {
38
        return NewsItem::online()
39
            ->where('publish_date', '>', $newsItem->publish_date)
40
            ->orderBy('publish_date', 'desc')
41
            ->first();
42
    }
43
44
    /**
45
     * @return \App\Models\NewsItem|null
46
     */
47
    public static function findPrevious(NewsItem $newsItem)
48
    {
49
        return NewsItem::online()
50
            ->where('publish_date', '<', $newsItem->publish_date)
51
            ->orderBy('publish_date', 'desc')
52
            ->first();
53
    }
54
55
    public static function paginate(int $perPage): Paginator
56
    {
57
        return NewsItem::online()->paginate($perPage);
58
    }
59
}
60