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

NewsItemRepository   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getAll() 0 4 1
A getLatest() 0 6 1
A findById() 0 4 1
A findBySlug() 0 4 1
A findNext() 0 7 1
A findPrevious() 0 7 1
A paginate() 0 4 1
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