Passed
Push — master ( 49f1ef...b2146d )
by John
05:49 queued 11s
created

ProblemSearchModel   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
eloc 20
c 2
b 0
f 2
dl 0
loc 31
rs 10
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A search() 0 26 6
1
<?php
2
3
namespace App\Models\Search;
4
5
use App\Models\ProblemModel;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Facades\DB;
8
9
class ProblemSearchModel extends Model
10
{
11
    protected $table='problem';
12
    protected $primaryKey='pid';
13
14
    public function search($key)
15
    {
16
        $result = [];
17
        //problem code find
18
        $ret = self::where('pcode', $key)
19
            ->select('pcode', 'title')
20
            ->first();
21
        if(!empty($ret)){
22
            array_push($result,$ret);
23
        }
24
        //problem name find
25
        if(strlen($key) >= 2){
26
            $ret = self::whereRaw('MATCH(`title`) AGAINST (? IN BOOLEAN MODE)',[$key])
27
                ->select('pcode', 'title')
28
                ->get()->all();
29
            if(!empty($ret)){
30
                $result += $ret;
31
            }
32
        }
33
        $problemModel = new ProblemModel();
34
        foreach ($result as $p_index => $p) {
35
            if($problemModel->isBlocked($p['pid'])){
36
                unset($result[$p_index]);
37
            }
38
        }
39
        return $result;
40
    }
41
}
42