Passed
Push — master ( 37f6b0...2886a4 )
by
unknown
02:13
created

Package::getDataSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 15
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(__CLASS__ . '/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
     * Returns a JSON data schema for the frontend React components to use
115
     *
116
     * @return array
117
     */
118
    public function getDataSchema()
119
    {
120
        $schema = [
121
            'description' => $this->Description,
0 ignored issues
show
Bug Best Practice introduced by
The property Description does not exist on BringYourOwnIdeas\Maintenance\Model\Package. Since you implemented __get, consider adding a @property annotation.
Loading history...
122
            'link' => 'https://addons.silverstripe.org/add-ons/' . $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...
123
            'linkTitle' => _t(
124
                __CLASS__ . '.ADDONS_LINK_TITLE',
125
                'View {package} on addons.silverstripe.org',
126
                ['package' => $this->Title]
127
            )
128
        ];
129
130
        $this->extend('updateDataSchema', $schema);
131
132
        return $schema;
133
    }
134
135
    /**
136
     * Queue up a job to check for updates to packages if there isn't a pending job in the queue already
137
     */
138
    public function requireDefaultRecords()
139
    {
140
        parent::requireDefaultRecords();
141
142
        $pendingJobs = QueuedJobDescriptor::get()->filter([
143
            'Implementation' => CheckForUpdatesJob::class,
144
            'JobStatus' => [
145
                QueuedJob::STATUS_NEW,
146
                QueuedJob::STATUS_INIT,
147
                QueuedJob::STATUS_RUN,
148
            ],
149
        ]);
150
        if ($pendingJobs->count()) {
151
            return;
152
        }
153
154
        /** @var QueuedJobService $jobService */
155
        $jobService = QueuedJobService::singleton();
156
        $jobService->queueJob(Injector::inst()->create(CheckForUpdatesJob::class));
157
    }
158
}
159