Passed
Push — master ( a52450...ac01fb )
by Adam
09:58
created

OfferController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
dl 0
loc 90
rs 10
c 1
b 0
f 0
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A flags() 0 5 1
A __construct() 0 5 1
A applications() 0 7 2
A subscriptions() 0 3 2
B index() 0 43 6
1
<?php
2
3
namespace Coyote\Http\Controllers\Job;
4
5
use Coyote\Http\Controllers\Controller;
6
use Coyote\Http\Resources\AssetsResource;
7
use Coyote\Http\Resources\CommentCollection;
8
use Coyote\Http\Resources\FlagResource;
9
use Coyote\Http\Resources\JobResource;
10
use Coyote\Repositories\Contracts\JobRepositoryInterface as JobRepository;
11
use Coyote\Firm;
12
use Coyote\Job;
13
use Coyote\Services\Elasticsearch\Builders\Job\MoreLikeThisBuilder;
14
use Coyote\Services\Flags;
15
use Coyote\Services\UrlBuilder;
16
17
class OfferController extends Controller
18
{
19
    /**
20
     * @var JobRepository
21
     */
22
    private $job;
23
24
    /**
25
     * @param JobRepository $job
26
     */
27
    public function __construct(JobRepository $job)
28
    {
29
        parent::__construct();
30
31
        $this->job = $job;
32
    }
33
34
    /**
35
     * @param \Coyote\Job $job
36
     * @return \Illuminate\View\View
37
     */
38
    public function index($job)
39
    {
40
        $this->breadcrumb->push('Praca', route('job.home'));
41
        $this->breadcrumb->push($job->title, UrlBuilder::job($job, true));
42
43
        $parser = app('parser.job');
44
45
        foreach (['description', 'requirements', 'recruitment'] as $name) {
46
            if (!empty($job->{$name})) {
47
                $job->{$name} = $parser->parse($job->{$name});
48
            }
49
        }
50
51
        if ($job->firm_id) {
52
            $job->firm->description = $parser->parse((string) $job->firm->description);
53
        }
54
55
        $job->addReferer(url()->previous());
56
57
        // search related offers
58
        $mlt = $this->job->search(new MoreLikeThisBuilder($job))->getSource();
0 ignored issues
show
Bug introduced by
The method search() does not exist on Coyote\Repositories\Cont...\JobRepositoryInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Coyote\Repositories\Cont...\JobRepositoryInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
        $mlt = $this->job->/** @scrutinizer ignore-call */ search(new MoreLikeThisBuilder($job))->getSource();
Loading history...
59
60
        $comments = new CommentCollection($job->commentsWithChildren);
61
        $comments->job = $job;
62
63
        return $this->view('job.offer', [
64
            'rates_list'        => Job::getRatesList(),
65
            'employment_list'   => Job::getEmploymentList(),
66
            'employees_list'    => Firm::getEmployeesList(),
67
            'seniority_list'    => Job::getSeniorityList(),
68
            'subscribed'        => $this->userId ? $job->subscribers()->forUser($this->userId)->exists() : false,
69
            'is_applied'        => $job->applications()->forGuest($this->guestId)->exists(),
70
            'previous_url'      => $this->request->session()->get('current_url'),
71
            'payment'           => $this->userId === $job->user_id ? $job->getUnpaidPayment() : null,
72
            // tags along with grouped category
73
            'tags'              => $job->tags()->orderBy('priority', 'DESC')->with('category')->get()->groupCategory(),
74
            'comments'          => $comments->toArray($this->request),
75
            'applications'      => $this->applications($job),
76
            'flags'             => $this->flags(),
77
            'assets'            => AssetsResource::collection($job->firm->assets)->toArray($this->request),
78
            'subscriptions'     => $this->subscriptions()
79
        ])->with(
80
            compact('job', 'mlt')
81
        );
82
    }
83
84
    /**
85
     * @param Job $job
86
     * @return array|\Illuminate\Support\Collection
87
     */
88
    private function applications(Job $job)
89
    {
90
        if ($this->userId !== $job->user_id) {
91
            return [];
92
        }
93
94
        return $job->applications()->get();
95
    }
96
97
    private function flags()
98
    {
99
        $flags = resolve(Flags::class)->fromModels([Job::class, Job\Comment::class])->permission('job-delete')->get();
100
101
        return FlagResource::collection($flags)->toArray($this->request);
102
    }
103
104
    private function subscriptions(): array
105
    {
106
        return $this->userId ? JobResource::collection($this->job->subscribes($this->userId))->toArray($this->request) : [];
107
    }
108
}
109