Passed
Push — sheepy/elevation-configuration ( bdeab0...bcf7bc )
by Marco
18:34
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;
45
    }
46
}
47