Passed
Push — sheepy/introspection ( 851cdd...69e16c )
by Marco
06:22
created

QueryRecord::findOrCreate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 13
rs 10
cc 3
nc 2
nop 2
1
<?php
2
3
4
namespace Firesphere\SolrSearch\Models;
5
6
use SilverStripe\ORM\DataObject;
7
use SilverStripe\ORM\ValidationException;
8
9
/**
10
 * Class \Firesphere\SolrSearch\Models\QueryRecording
11
 *
12
 * @property string $Query
13
 * @property int $Results
14
 * @property int $PagesVisited
15
 */
16
class QueryRecord extends DataObject
17
{
18
    private static $table_name = 'QueryRecording';
19
20
    private static $db = [
21
        'Query' => 'Varchar(255)',
22
        'Results' => 'Int',
23
        'PagesVisited' => 'Int',
24
    ];
25
26
    /**
27
     * @param $query
28
     * @param $count
29
     * @return QueryRecord
30
     * @throws ValidationException
31
     */
32
    public static function findOrCreate($query, $count): QueryRecord
33
    {
34
        $item = static::get()->filter(['Query' => trim($query)])->first();
35
        if (!$item || !$item->exists()) {
36
            $item = static::create([
37
                'Query' => $query,
38
            ]);
39
        }
40
        $item->Results = $count;
41
42
        $item->write();
43
44
        return $item;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $item returns the type SilverStripe\ORM\DataObject which includes types incompatible with the type-hinted return Firesphere\SolrSearch\Models\QueryRecord.
Loading history...
45
    }
46
}
47