Passed
Push — master ( 12811f...03962a )
by Nicolaas
03:42
created

ObjectsUpdated::classes_edited()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 14
rs 10
1
<?php
2
3
namespace Sunnysideup\SimpleTemplateCaching\Model;
4
5
use SilverStripe\Core\Injector\Injector;
6
use SilverStripe\Forms\ReadonlyField;
7
use SilverStripe\ORM\DataObject;
8
use SilverStripe\ORM\DB;
9
10
/**
11
 * A blog category for generalising blog posts.
12
 *
13
 * @property string $ClassNameLastEdited
14
 */
15
class ObjectsUpdated extends DataObject
16
{
17
18
    public static function classes_edited(): string
19
    {
20
        $query = DB::query('
21
            SELECT "ClassNameLastEdited", COUNT(*) AS "Count"
22
            FROM "ObjectsUpdated"
23
            GROUP BY "ClassNameLastEdited"
24
            ORDER BY "ClassNameLastEdited" ASC
25
        ');
26
        foreach ($query as $row) {
27
            $array[$row['ClassNameLastEdited']] =
28
                Injector::inst()->get($row['ClassNameLastEdited'])->i18n_singular_name() .
29
                ' (' . $row['Count'] . ')';
30
        }
31
        return implode(', ', $array);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $array seems to be defined by a foreach iteration on line 26. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
32
    }
33
34
    /**
35
     * @var string
36
     */
37
    private static $table_name = 'ObjectsUpdated';
38
39
    /**
40
     * @var array
41
     */
42
    private static $db = [
43
        'ClassNameLastEdited' => 'Varchar(255)',
44
    ];
45
46
    private static $default_sort = [
47
        'ID' => 'DESC',
48
    ];
49
50
    /**
51
     * @var array
52
     */
53
    private static $summary_fields = [
54
        'Created' => 'Updated',
55
        'Title' => 'Record name    ',
56
        'LastEdited' => 'Last Edited',
57
    ];
58
59
    /**
60
     * @var array
61
     */
62
    private static $field_labels = [
63
        'Created' => 'Updated',
64
        'Title' => 'Human readable name',
65
        'LastEdited' => 'Code name',
66
    ];
67
68
    /**
69
     * @var array
70
     */
71
    private static $casting = [
72
        'Title' => 'Varchar',
73
    ];
74
75
    public function getTitle(): string
76
    {
77
        if (class_exists((string) $this->ClassNameLastEdited)) {
78
            return Injector::inst()->get($this->ClassNameLastEdited)->i18n_singular_name();
79
        }
80
81
        return 'ERROR: class not found';
82
    }
83
84
    public function getCMSFields()
85
    {
86
        $fields = parent::getCMSFields();
87
        $fields->removeByName('ClassNameLastEdited');
88
        $fields->addFieldsToTab(
89
            'Root.Main',
90
            [
91
                ReadonlyField::create('Title', 'Title', $this->getTitle()),
92
                ReadonlyField::create('Created', 'Recorded'),
93
            ]
94
        );
95
        return $fields;
96
    }
97
}
98