Passed
Pull Request — master (#235)
by John
05:34
created

ProblemSearchModel::search()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
eloc 14
c 1
b 0
f 1
nc 9
nop 1
dl 0
loc 20
rs 9.4888
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
        if(strlen($key) >= 2){
18
            $ret = self::where('pcode', $key)
19
                ->orWhereRaw('MATCH(`title`) AGAINST (? IN BOOLEAN MODE)',[$key])
20
                ->select('pcode', 'title')
21
                ->limit(120)
22
                ->get()->all();
23
            if(!empty($ret)){
24
                $result += $ret;
25
            }
26
        }
27
        $problemModel = new ProblemModel();
28
        foreach ($result as $p_index => $p) {
29
            if($problemModel->isBlocked($p['pid'])){
30
                unset($result[$p_index]);
31
            }
32
        }
33
        return $result;
34
    }
35
}
36