Completed
Push — master ( 9f244d...a6cc2e )
by Robbie
02:20
created

Package::getBadges()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace BringYourOwnIdeas\Maintenance\Model;
4
5
use BringYourOwnIdeas\Maintenance\Jobs\CheckForUpdatesJob;
6
use SilverStripe\Core\Injector\Injector;
7
use SilverStripe\ORM\ArrayList;
8
use SilverStripe\ORM\DataObject;
9
use SilverStripe\View\ArrayData;
10
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor;
11
use Symbiote\QueuedJobs\Services\QueuedJob;
12
use Symbiote\QueuedJobs\Services\QueuedJobService;
13
14
/**
15
 * Describes an installed composer package version.
16
 */
17
class Package extends DataObject
18
{
19
    private static $table_name = 'Package';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
20
21
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
22
        'Name' => 'Varchar(255)',
23
        'Description' => 'Varchar(255)',
24
        'Version' => 'Varchar(255)',
25
        'Type' => 'Varchar(255)',
26
        'Supported' => 'Boolean',
27
    ];
28
29
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
30
        'Title' => 'Title',
31
        'Description' => 'Description',
32
        'Version' => 'Version',
33
    ];
34
35
    /**
36
     * @var array badge definitions - a keyed array in the format of [Title => Type] {@see getBadges()}
37
     */
38
    protected $badges = [];
39
40
    /**
41
     * Strips vendor and 'silverstripe-' prefix from Name property
42
     * @return string More easily digestable module name for human consumers
43
     */
44
    public function getTitle()
45
    {
46
        return preg_replace('#^[^/]+/(silverstripe-)?#', '', $this->Name);
0 ignored issues
show
Bug Best Practice introduced by
The property Name does not exist on BringYourOwnIdeas\Maintenance\Model\Package. Since you implemented __get, consider adding a @property annotation.
Loading history...
47
    }
48
49
    /**
50
     * Returns HTML formatted summary of this object, uses a template to do this.
51
     * @return string
52
     */
53
    public function getSummary()
54
    {
55
        $summary = $this->renderWith('Package_summary');
56
        $this->extend('updateSummary', $summary);
57
        return $summary;
58
    }
59
60
    /**
61
     * Gives the summary template {@see getSummary()} a list of badges to show against a package
62
     *
63
     * badgeDefinitions are in the format [$title => $type] where:
64
     *   title is the unique string to display
65
     *   type is an optional class attribute (applied as a BEM modifier, by default)
66
     *
67
     * @param array $extraBadges allow a user to include extra badges at call time
68
     *
69
     * @return ArrayList
70
     */
71
    public function getBadges($extraBadges = [])
72
    {
73
        $badgeDefinitions = array_merge($this->badges, $extraBadges);
74
        $badges = ArrayList::create();
75
        foreach ($badgeDefinitions as $title => $type) {
76
            $badges->push(ArrayData::create([
77
                'Title' => $title,
78
                'Type' => $type,
79
            ]));
80
        }
81
82
        $this->extend('updateBadges', $badges);
83
        return $badges;
84
    }
85
86
    /**
87
     * Adds a badge to the list of badges {@see $badges}
88
     *
89
     * @param string $title
90
     * @param string $type
91
     *
92
     * @return $this
93
     */
94
    public function addBadge($title, $type)
95
    {
96
        $this->badges[$title] = $type;
97
        return $this;
98
    }
99
100
    /**
101
     * Replaces the list of badges
102
     *
103
     * @param array $badges {@see $badges}
104
     *
105
     * @return $this
106
     */
107
    public function setBadges($badges)
108
    {
109
        $this->badges = $badges;
110
        return $this;
111
    }
112
113
    /**
114
     * Queue up a job to check for updates to packages if there isn't a pending job in the queue already
115
     */
116
    public function requireDefaultRecords()
117
    {
118
        parent::requireDefaultRecords();
119
120
        $pendingJobs = QueuedJobDescriptor::get()->filter([
121
            'Implementation' => CheckForUpdatesJob::class,
122
            'JobStatus' => [
123
                QueuedJob::STATUS_NEW,
124
                QueuedJob::STATUS_INIT,
125
                QueuedJob::STATUS_RUN,
126
            ],
127
        ]);
128
        if ($pendingJobs->count()) {
129
            return;
130
        }
131
132
        /** @var QueuedJobService $jobService */
133
        $jobService = QueuedJobService::singleton();
134
        $jobService->queueJob(Injector::inst()->create(CheckForUpdatesJob::class));
135
    }
136
}
137