Completed
Pull Request — master (#83)
by Sebastian
04:16
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
use Spatie\Blender\Model\Repository;
9
10
class NewsItemRepository extends Repository
11
{
12
    public function getAll(): Collection
13
    {
14
        return NewsItem::orderBy('publish_date', 'desc')->get();
15
    }
16
17
    public function getAllOnline(): Collection
18
    {
19
        return NewsItem::online()
20
            ->orderBy('publish_date', 'desc')
21
            ->get();
22
    }
23
24
    public function getLatest(int $amount): Collection
25
    {
26
        return NewsItem::online()
27
            ->orderBy('publish_date', 'desc')
28
            ->take($amount)
29
            ->get();
30
    }
31
32
    public function findOnline(int $id): NewsItem
33
    {
34
        return NewsItem::online()->findOrFail($id);
35
    }
36
37
    public function findByUrl(string $url): NewsItem
38
    {
39
        return NewsItem::online()
40
            ->where('url->'.content_locale(), $url)
41
            ->firstOrFail();
42
    }
43
44
    /**
45
     * @return \App\Models\NewsItem|null
46
     */
47
    public function findNext(NewsItem $newsItem)
48
    {
49
        return NewsItem::online()
50
            ->where('publish_date', '>', $newsItem->publish_date)
51
            ->orderBy('publish_date', 'desc')
52
            ->first();
53
    }
54
55
    /**
56
     * @return \App\Models\NewsItem|null
57
     */
58
    public function findPrevious(NewsItem $newsItem)
59
    {
60
        return NewsItem::online()
61
            ->where('publish_date', '<', $newsItem->publish_date)
62
            ->orderBy('publish_date', 'desc')
63
            ->first();
64
    }
65
66
    public function paginate(int $perPage): Paginator
67
    {
68
        return NewsItem::online()->paginate($perPage);
69
    }
70
}
71