Passed
Pull Request — master (#39)
by Robbie
01:44
created

PackageSecurityExtension::updateBadges()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace BringYourOwnIdeas\SecurityChecker\Extensions;
4
5
use function array_intersect_key;
6
use BringYourOwnIdeas\SecurityChecker\Models\SecurityAlert;
7
use SilverStripe\Core\Convert;
8
use SilverStripe\ORM\ArrayList;
9
use SilverStripe\ORM\DataExtension;
10
use SilverStripe\View\ArrayData;
11
12
class PackageSecurityExtension extends DataExtension
13
{
14
    private static $has_many = [
0 ignored issues
show
introduced by
The private property $has_many is not used, and could be removed.
Loading history...
15
        'SecurityAlerts' => SecurityAlert::class
16
    ];
17
18
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
19
        'listSecurityAlertIdentifiers' => 'Security alerts',
20
    ];
21
22
    /**
23
     * Simply returns a comma separated list of active SecurityAlert Identifiers for this record.
24
     * Used in CSV exports as a type of brief indication (as opposed to full info)
25
     */
26
    public function listSecurityAlertIdentifiers()
27
    {
28
        $alerts = $this->owner->SecurityAlerts()->Column('Identifier');
29
        return $alerts ? implode(', ', $alerts) : null;
30
    }
31
32
    /**
33
     * updates the badges that render as part of the screen targeted
34
     * summary for this Package
35
     *
36
     * @param ArrayList $badges
37
     */
38
    public function updateBadges($badges)
39
    {
40
        if ($this->owner->SecurityAlerts()->exists()) {
41
            $badges->push(ArrayData::create([
42
                'Title' => _t(__CLASS__ . '.BADGE_SECURITY', 'RISK: Security'),
43
                'Type' => 'warning security-alerts__toggler',
44
            ]));
45
        }
46
    }
47
48
    /**
49
     * Adds security alert notifications into the schema
50
     *
51
     * @param array &$schema
52
     * @return string
53
     */
54
    public function updateDataSchema(&$schema)
55
    {
56
        // The keys from the SecurityAlert model that we need in the React component
57
        $keysToPass = ['Identifier', 'ExternalLink'];
58
59
        $alerts = [];
60
        foreach ($this->owner->SecurityAlerts()->toNestedArray() as $alert) {
61
            $alerts[] = array_intersect_key($alert, array_flip($keysToPass));
62
        }
63
64
        $schema['securityAlerts'] = $alerts;
65
    }
66
}
67