Passed
Push — sheepy/rebased-intrtospection ( aaa2f9...92af37 )
by Simon
09:28
created

QueryRecording::getOrCreate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 7
c 1
b 1
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 QueryRecording extends DataObject
17
{
18
    private static $db = [
19
        'Query' => 'Varchar(255)',
20
        'Results' => 'Int',
21
        'PagesVisited' => 'Int',
22
    ];
23
24
    /**
25
     * @param $query
26
     * @param $count
27
     * @return QueryRecording
28
     * @throws ValidationException
29
     */
30
    public static function getOrCreate($query, $count): QueryRecording
31
    {
32
        $item = static::get()->filter(['Query' => trim($query)])->first();
33
        if (!$item || !$item->exists()) {
1 ignored issue
show
introduced by
$item is of type SilverStripe\ORM\DataObject, thus it always evaluated to true.
Loading history...
34
            $item = static::create([
35
                'Query' => $query,
36
            ]);
37
        }
38
        $item->Results = $count;
39
40
        $item->write();
41
42
        return $item;
43
    }
44
}
45