|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Coyote\Repositories\Eloquent; |
|
4
|
|
|
|
|
5
|
|
|
use Coyote\Feature; |
|
6
|
|
|
use Coyote\Repositories\Contracts\JobRepositoryInterface; |
|
7
|
|
|
use Coyote\Job; |
|
8
|
|
|
use Coyote\Repositories\Contracts\SubscribableInterface; |
|
9
|
|
|
use Coyote\Str; |
|
10
|
|
|
use Illuminate\Database\Query\JoinClause; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @method mixed search(\Coyote\Services\Elasticsearch\QueryBuilderInterface $queryBuilder) |
|
14
|
|
|
* @method $this withTrashed() |
|
15
|
|
|
*/ |
|
16
|
|
|
class JobRepository extends Repository implements JobRepositoryInterface, SubscribableInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @return string |
|
20
|
|
|
*/ |
|
21
|
|
|
public function model() |
|
22
|
|
|
{ |
|
23
|
|
|
return 'Coyote\Job'; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @inheritdoc |
|
28
|
|
|
*/ |
|
29
|
|
|
public function count() |
|
30
|
|
|
{ |
|
31
|
|
|
return $this->applyCriteria(function () { |
|
32
|
|
|
return $this->model->count(); |
|
33
|
|
|
}); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @inheritdoc |
|
38
|
|
|
*/ |
|
39
|
|
|
public function countCityOffers(string $city) |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->applyCriteria(function () use ($city) { |
|
|
|
|
|
|
42
|
|
|
return $this |
|
43
|
|
|
->model |
|
44
|
|
|
->join('job_locations', 'jobs.id', '=', 'job_locations.job_id') |
|
45
|
|
|
->where('city', $city) |
|
46
|
|
|
->count(); |
|
47
|
|
|
}); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @inheritdoc |
|
52
|
|
|
*/ |
|
53
|
|
|
public function counterUserOffers(int $userId) |
|
54
|
|
|
{ |
|
55
|
|
|
return (int) $this->applyCriteria(function () use ($userId) { |
|
56
|
|
|
return $this->model->forUser($userId)->count(); |
|
57
|
|
|
}); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @inheritdoc |
|
62
|
|
|
*/ |
|
63
|
|
|
public function subscribes($userId) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->applyCriteria(); |
|
66
|
|
|
|
|
67
|
|
|
$result = $this |
|
68
|
|
|
->model |
|
69
|
|
|
->select(['jobs.*', 'firms.name AS firm.name', 'firms.logo AS firm.logo', 'currencies.name AS currency_name']) |
|
70
|
|
|
->join('job_subscribers', function (JoinClause $join) use ($userId) { |
|
71
|
|
|
$join->on('job_id', '=', 'jobs.id')->on('job_subscribers.user_id', '=', $this->raw($userId)); |
|
72
|
|
|
}) |
|
73
|
|
|
->leftJoin('firms', 'firms.id', '=', 'firm_id') |
|
74
|
|
|
->join('currencies', 'currencies.id', '=', 'currency_id') |
|
75
|
|
|
->with('locations') |
|
76
|
|
|
->with('tags') |
|
77
|
|
|
->get(); |
|
78
|
|
|
|
|
79
|
|
|
$this->resetModel(); |
|
80
|
|
|
|
|
81
|
|
|
return $result; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @inheritdoc |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getPopularTags($limit = 500) |
|
88
|
|
|
{ |
|
89
|
|
|
return $this |
|
90
|
|
|
->getTagsQueryBuilder() |
|
91
|
|
|
->orderBy($this->raw('COUNT(*)'), 'DESC') |
|
92
|
|
|
->limit($limit) |
|
93
|
|
|
->get() |
|
94
|
|
|
->pluck('count', 'name'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @inheritdoc |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getDefaultFeatures($userId) |
|
101
|
|
|
{ |
|
102
|
|
|
$sub = $this->toSql( |
|
103
|
|
|
$this->model->select('id')->where('user_id', $userId)->orderBy('id', 'DESC')->limit(1) |
|
104
|
|
|
); |
|
105
|
|
|
|
|
106
|
|
|
return $this |
|
107
|
|
|
->app |
|
108
|
|
|
->make(Feature::class) |
|
109
|
|
|
->selectRaw( |
|
110
|
|
|
'features.*, COALESCE(job_features.checked, 0) AS checked, COALESCE(job_features.value, \'\') AS value' |
|
111
|
|
|
) |
|
112
|
|
|
->leftJoin('job_features', function (JoinClause $join) use ($sub) { |
|
113
|
|
|
return $join->on('job_id', '=', $this->raw("($sub)"))->on('feature_id', '=', 'features.id'); |
|
114
|
|
|
}) |
|
115
|
|
|
->orderBy('order') |
|
116
|
|
|
->get(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @inheritdoc |
|
121
|
|
|
*/ |
|
122
|
|
|
public function getTagsWeight(array $tagsId) |
|
123
|
|
|
{ |
|
124
|
|
|
$this->applyCriteria(); |
|
125
|
|
|
|
|
126
|
|
|
return $this |
|
127
|
|
|
->getTagsQueryBuilder() |
|
128
|
|
|
->whereIn('job_tags.tag_id', $tagsId) |
|
129
|
|
|
->get() |
|
130
|
|
|
->pluck('count', 'name'); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @inheritdoc |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getSubscribed($userId) |
|
137
|
|
|
{ |
|
138
|
|
|
return $this |
|
139
|
|
|
->app |
|
140
|
|
|
->make(Job\Subscriber::class) |
|
141
|
|
|
->select(['jobs.id', 'title', 'slug', 'job_subscribers.created_at']) |
|
142
|
|
|
->join('jobs', 'jobs.id', '=', 'job_subscribers.job_id') |
|
143
|
|
|
->where('job_subscribers.user_id', $userId) |
|
144
|
|
|
->whereNull('deleted_at') |
|
145
|
|
|
->orderBy('job_subscribers.id', 'DESC') |
|
146
|
|
|
->paginate(); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @inheritdoc |
|
151
|
|
|
*/ |
|
152
|
|
|
public function getMyOffers($userId) |
|
153
|
|
|
{ |
|
154
|
|
|
$this->applyCriteria(); |
|
155
|
|
|
|
|
156
|
|
|
$result = $this |
|
157
|
|
|
->model |
|
158
|
|
|
->select(['jobs.*', 'firms.name AS firm_name']) |
|
159
|
|
|
->leftJoin('firms', 'firms.id', '=', 'firm_id') |
|
160
|
|
|
->where('jobs.user_id', $userId) |
|
161
|
|
|
->get(); |
|
162
|
|
|
|
|
163
|
|
|
$this->resetModel(); |
|
164
|
|
|
|
|
165
|
|
|
return $result; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @inheritdoc |
|
170
|
|
|
*/ |
|
171
|
|
|
public function getTagSuggestions(array $tags): array |
|
172
|
|
|
{ |
|
173
|
|
|
$tags = array_map(function ($tag) { |
|
174
|
|
|
return new Str($tag); |
|
175
|
|
|
}, $tags); |
|
176
|
|
|
|
|
177
|
|
|
$sub = $this->toSql( |
|
178
|
|
|
$this |
|
179
|
|
|
->model |
|
180
|
|
|
->select(['job_id']) |
|
181
|
|
|
->from('tags') |
|
182
|
|
|
->whereIn('name', $tags) |
|
183
|
|
|
->join('job_tags', 'job_tags.tag_id', '=', 'tags.id') |
|
184
|
|
|
->join('jobs', 'jobs.id', '=', 'job_tags.job_id') // required for eloquent's scope (deleted_at column) |
|
185
|
|
|
); |
|
186
|
|
|
|
|
187
|
|
|
return $this |
|
188
|
|
|
->model |
|
189
|
|
|
->select(['tags.name']) |
|
190
|
|
|
->from($this->raw("($sub) AS t")) |
|
191
|
|
|
->join('job_tags', 'job_tags.job_id', '=', 't.job_id') |
|
192
|
|
|
->join('tags', 'tags.id', '=', 'job_tags.tag_id') |
|
193
|
|
|
->join('jobs', 'jobs.id', '=', 'job_tags.job_id') // required for eloquent's scope (deleted_at column) |
|
194
|
|
|
->whereNotIn('name', $tags) |
|
195
|
|
|
->groupBy('tags.name') |
|
196
|
|
|
->orderByRaw('COUNT(*) DESC') |
|
197
|
|
|
->limit(5) |
|
198
|
|
|
->pluck('name') |
|
199
|
|
|
->toArray(); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @return mixed |
|
204
|
|
|
*/ |
|
205
|
|
|
private function getTagsQueryBuilder() |
|
206
|
|
|
{ |
|
207
|
|
|
return $this |
|
208
|
|
|
->app |
|
209
|
|
|
->make(Job\Tag::class) |
|
210
|
|
|
->select(['name', $this->raw('COUNT(*) AS count')]) |
|
211
|
|
|
->join('tags', 'tags.id', '=', 'tag_id') |
|
212
|
|
|
->join('jobs', 'jobs.id', '=', 'job_id') |
|
213
|
|
|
->whereNull('jobs.deleted_at') |
|
214
|
|
|
->whereNull('tags.deleted_at') |
|
215
|
|
|
->where('deadline_at', '>', $this->raw('NOW()')) |
|
216
|
|
|
->groupBy('name'); |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.