CandidatesRepository   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 39
c 2
b 0
f 0
dl 0
loc 66
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
B search() 0 34 6
A getOtherApplications() 0 11 1
A getTypeaheadCandidatesNameCollection() 0 15 1
1
<?php
2
3
namespace App\Repositories;
4
5
use App\Models\Candidate;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Database\Eloquent\Collection;
8
9
class CandidatesRepository
10
{
11
    public static function getOtherApplications(Candidate $candidate)
12
    {
13
        return Candidate::where('id', '!=', $candidate->id)
14
            ->where(function ($query) use ($candidate) {
15
                $query->where('email', '=', $candidate->email)
16
                    ->orWhere('email', '=', sha1($candidate->email))
17
                    ->orWhere('phone_number', '=', $candidate->phone_number)
18
                    ->orWhere('phone_number', '=', sha1($candidate->phone_number));
19
            })
20
            ->with('recruitment')
21
            ->get();
22
    }
23
24
    public static function getTypeaheadCandidatesNameCollection()
25
    {
26
        return Candidate::query()
27
            ->get(['name'])
28
            ->map(function ($candidate) {
29
                return trim(
30
                    join(' ', [
31
                        htmlspecialchars(data_get($candidate, 'name')),
32
                    ])
33
                );
34
            })
35
            ->filter()
36
            ->unique()
37
            ->sort()
38
            ->values();
39
    }
40
41
    public static function search(array $data, array $columns = null): Collection
42
    {
43
        $query = Candidate::query();
44
45
        if ($search = data_get($data, 'search')) {
46
            $array = explode(' ', $search);
47
48
            if (count($array) === 2) {
49
                $query = $query
50
                    ->where('name', 'like', '%'.$array[0].'%');
51
            } else {
52
                $query = $query
53
                    ->where(function (Builder $query) use ($array) {
54
                        $query
55
                            ->where(function (Builder $query) use ($array) {
56
                                foreach ($array as $item) {
57
                                    $query->orWhere('name', 'like', "%{$item}%");
58
                                }
59
                            });
60
                    });
61
            }
62
        }
63
64
        if ($recruitment = data_get($data, 'recruitment')) {
65
            $query = $query->where('recruitment_id', $recruitment);
66
        }
67
68
        if (!empty($columns)) {
69
            $query->select($columns);
70
        } else {
71
            $query->with('recruitment');
72
        }
73
74
        return $query->get();
75
    }
76
}
77