Issues (20)

src/Extensions/PackageSecurityExtension.php (2 issues)

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