Passed
Push — master ( 3b518a...cd2a02 )
by Adam
11:23
created

HomeController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 41
rs 10
c 2
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 3 1
A filterByTags() 0 7 1
A load() 0 18 2
A __construct() 0 5 1
1
<?php
2
3
namespace Coyote\Http\Controllers\Guide;
4
5
use Coyote\Http\Resources\GuideResource;
6
use Coyote\Repositories\Contracts\GuideRepositoryInterface as GuideRepository;
7
use Coyote\Repositories\Contracts\TagRepositoryInterface as TagRepository;
8
use Coyote\Repositories\Criteria\EagerLoading;
9
use Coyote\Repositories\Criteria\WithTags;
10
11
class HomeController extends BaseController
12
{
13
    public function __construct(
14
        protected TagRepository $tagRepository,
15
        protected GuideRepository $guideRepository
16
    ) {
17
        parent::__construct($this->tagRepository);
18
    }
19
20
    public function index()
21
    {
22
        return $this->load();
23
    }
24
25
    public function filterByTags(string $tag)
26
    {
27
        $this->breadcrumb->push($tag, route('guide.tag', [$tag]));
28
29
        $this->guideRepository->pushCriteria(new WithTags([$tag]));
30
31
        return $this->load()->with('tag', $tag);
32
    }
33
34
    private function load()
35
    {
36
        if ($this->userId) {
37
            $userScope = fn ($builder) => $builder->where('user_id', $this->userId);
38
39
            $this->guideRepository->pushCriteria(
40
                new EagerLoading([
41
                    'subscribers' => $userScope,
42
                    'voters' => $userScope,
43
                    'roles' => $userScope
44
                ])
45
            );
46
        }
47
48
        $paginator = $this->guideRepository->paginate();
49
50
        return $this->view('guide.home', [
51
            'pagination'                => GuideResource::collection($paginator)->response()->getData(true)
52
        ]);
53
    }
54
}
55