Passed
Push — hans/addAllFields ( 30dc29...48c3f4 )
by Simon
07:26 queued 01:24
created

SolrLog::onBeforeWrite()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
4
namespace Firesphere\SolrSearch\Models;
5
6
use SilverStripe\Control\Director;
7
use SilverStripe\ORM\DataObject;
8
use SilverStripe\ORM\FieldType\DBDatetime;
9
use SilverStripe\Security\Member;
10
11
/**
12
 * Class \Firesphere\SolrSearch\Models\SolrError
13
 *
14
 * @property string $Timestamp
15
 * @property string $Message
16
 * @property string $Index
17
 * @property string $Type
18
 * @property string $Level
19
 */
20
class SolrLog extends DataObject
21
{
22
    /**
23
     * Used to give the Gridfield rows a corresponding colour
24
     * @var array
25
     */
26
    protected static $row_color = [
27
        'ERROR' => 'alert alert-danger',
28
        'WARN'  => 'alert alert-warning',
29
        'INFO'  => 'alert alert-info',
30
    ];
31
    private static $table_name = 'SolrLog';
32
    private static $db = [
33
        'Timestamp' => 'Datetime',
34
        'Message'   => 'Text',
35
        'Index'     => 'Varchar(255)',
36
        'Type'      => 'Enum("Config,Index,Query")',
37
        'Level'     => 'Varchar(10)'
38
    ];
39
    private static $summary_fields = [
40
        'Timestamp',
41
        'Index',
42
        'Type',
43
        'Level'
44
    ];
45
    private static $searchable_fields = [
46
        'Created',
47
        'Timestamp',
48
        'Index',
49
        'Type',
50
        'Level'
51
    ];
52
    private static $indexes = [
53
        'Timestamp' => true,
54
    ];
55
    private static $sort = 'Timestamp DESC';
0 ignored issues
show
introduced by
The private property $sort is not used, and could be removed.
Loading history...
56
57 32
    public function onBeforeWrite()
58
    {
59 32
        parent::onBeforeWrite();
60
61 32
        $this->Timestamp = DBDatetime::create()->setValue(strtotime($this->Timestamp));
62 32
    }
63
64
    /**
65
     * @return mixed
66
     */
67 1
    public function getLastErrorLine()
68
    {
69 1
        $lines = explode(PHP_EOL, $this->Message);
70
71 1
        return $lines[0];
72
    }
73
74
    /**
75
     * @param null|Member $member
76
     * @return bool|mixed
77
     */
78 1
    public function canCreate($member = null, $context = [])
79
    {
80 1
        return false;
81
    }
82
83
    /**
84
     * @param null|Member $member
85
     * @return bool|mixed
86
     */
87 1
    public function canEdit($member = null)
88
    {
89 1
        return false;
90
    }
91
92
    /**
93
     * @param null|Member $member
94
     * @return bool|mixed
95
     */
96 33
    public function canView($member = null)
97
    {
98 33
        return parent::canView($member);
99
    }
100
101
    /**
102
     * @param null|Member $member
103
     * @return bool|mixed
104
     */
105 1
    public function canDelete($member = null)
106
    {
107 1
        if ($member) {
108
            return $member->inGroup('administrators') || Director::isDev();
109
        }
110
111 1
        return parent::canDelete($member) || Director::isDev();
112
    }
113
114
    /**
115
     * Get the extra classes to colour the gridfield rows
116
     * @return mixed|string
117
     */
118 1
    public function getExtraClass()
119
    {
120 1
        $classMap = static::$row_color;
121
122 1
        return $classMap[$this->Level] ?? 'alert alert-info';
123
    }
124
}
125