Passed
Push — master ( 753e62...f14647 )
by Adam
11:15
created

AdController::boost()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A AdController::getMajorTag() 0 7 2
1
<?php
2
3
namespace Coyote\Http\Controllers\Job;
4
5
use Coyote\Http\Controllers\Controller;
6
use Coyote\Repositories\Contracts\JobRepositoryInterface as JobRepository;
7
use Coyote\Repositories\Contracts\TagRepositoryInterface as TagRepository;
8
use Coyote\Services\Elasticsearch\Builders\Job\AdBuilder;
9
use Coyote\Services\Elasticsearch\Raw;
10
use Coyote\Services\Skills\Predictions;
11
use Coyote\Tag;
12
13
class AdController extends Controller
14
{
15
    /**
16
     * @var JobRepository
17
     */
18
    private $job;
19
20
    /**
21
     * @var TagRepository
22
     */
23
    private $tag;
24
25
    /**
26
     * @param JobRepository $job
27
     * @param TagRepository $tag
28
     */
29
    public function __construct(JobRepository $job, TagRepository $tag)
30
    {
31
        debugbar()->disable();
32
        parent::__construct();
33
34
        $this->job = $job;
35
        $this->tag = $tag;
36
37
        $this->middleware('geocode');
38
    }
39
40
    /**
41
     * @param Predictions $predictions
42
     * @return string
43
     */
44
    public function index(Predictions $predictions)
45
    {
46
        $builder = new AdBuilder($this->request);
47
        $builder->boostLocation($this->request->attributes->get('geocode'));
48
49
        $data = [];
50
        $tags = $predictions->getTags();
51
        $majorTag = $this->getMajorTag($tags);
52
53
        if ($majorTag->exists) {
54
            $builder->boostTags([sprintf('%s^%.1F', Raw::escape($majorTag->name), 1)]);
55
        }
56
57
        $result = $this->job->search($builder);
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

57
        /** @scrutinizer ignore-call */ 
58
        $result = $this->job->search($builder);
Loading history...
58
        if (!$result->total()) {
59
            return '';
60
        }
61
62
        // search jobs that might be interesting for user
63
        return (string) view(
64
            'job.ad',
65
            $data,
66
            ['jobs' => $result->getSource(), 'inverse_tags' => [$majorTag->name], 'major_tag' => $majorTag]
67
        );
68
    }
69
70
    /**
71
     * @param \Coyote\Tag[] $tags
72
     * @return \Coyote\Tag
73
     */
74
    private function getMajorTag($tags)
75
    {
76
        if (empty($tags)) {
77
            return new Tag();
78
        }
79
80
        return $tags->random();
81
    }
82
}
83