ContestSearchModel   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 41
rs 10
c 0
b 0
f 0
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B search() 0 34 7
1
<?php
2
3
namespace App\Models\Search;
4
5
use App\Models\ContestModel;
6
use Illuminate\Database\Eloquent\Model;
7
use Auth;
8
9
class ContestSearchModel extends Model
10
{
11
    protected $table='contest';
12
    protected $primaryKey='cid';
13
14
    private $rule=["Unknown", "ICPC", "IOI", "Custom ICPC", "Custom IOI"];
15
16
    public function search($key)
17
    {
18
        $result=[];
19
        //contest name find
20
        if (strlen($key)>=2) {
21
            $ret=self::whereRaw('MATCH(`name`) AGAINST (? IN BOOLEAN MODE)', [$key])
22
                ->select('cid', 'gid', 'name', 'rule', 'public', 'verified', 'practice', 'rated', 'anticheated', 'begin_time', 'end_time')
23
                ->orderBy('end_time', 'DESC')
24
                ->limit(120)
25
                ->get()->all();
26
            $user_id=Auth::user()->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
27
            $contestModel=new ContestModel();
28
            foreach ($ret as $c_index => $c) {
29
                if (!$contestModel->judgeClearance($c['cid'], $user_id)) {
30
                    unset($ret[$c_index]);
31
                }
32
            }
33
            if (!empty($ret)) {
34
                $result+=$ret;
35
            }
36
        }
37
        if (!empty($result)) {
38
            foreach ($result as &$contest) {
39
                $contest["rule_parsed"]=$this->rule[$contest["rule"]];
40
                $contest["date_parsed"]=[
41
                    "date"=>date_format(date_create($contest["begin_time"]), 'j'),
42
                    "month_year"=>date_format(date_create($contest["begin_time"]), 'M, Y'),
43
                ];
44
                $contest["length"]=$contestModel->calcLength($contest["begin_time"], $contest["end_time"]);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $contestModel does not seem to be defined for all execution paths leading up to this point.
Loading history...
45
            }
46
            unset($contest);
47
        }
48
49
        return $result;
50
    }
51
}
52