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

ProblemSearchModel   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
eloc 17
c 2
b 0
f 2
dl 0
loc 25
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A search() 0 20 5
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