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

HomeController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
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