Completed
Push — master ( 3da775...1e6bd3 )
by Robbie
9s
created

UpdatePackageInfoTask::run()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 1
dl 0
loc 20
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace BringYourOwnIdeas\Maintenance\Tasks;
4
5
use BringYourOwnIdeas\Maintenance\Util\ComposerLoader;
6
use BringYourOwnIdeas\Maintenance\Util\SupportedAddonsLoader;
7
use RuntimeException;
8
use SilverStripe\Control\HTTPRequest;
9
use SilverStripe\ORM\Queries\SQLDelete;
10
use BringYourOwnIdeas\Maintenance\Model\Package;
11
use SilverStripe\Dev\BuildTask;
12
13
/**
14
 * Parses a composer lock file in order to cache information about the installation.
15
 */
16
class UpdatePackageInfoTask extends BuildTask
17
{
18
19
    /**
20
     * @var array Injector configuration
21
     * @config
22
     */
23
    private static $dependencies = [
24
        'ComposerLoader' => '%$BringYourOwnIdeas\\Maintenance\\Util\\ComposerLoader',
25
        'SupportedAddonsLoader' => '%$BringYourOwnIdeas\\Maintenance\\Util\\SupportedAddonsLoader',
26
    ];
27
28
    /**
29
     * The "types" of composer libraries that will be processed. Anything without these types will be ignored.
30
     *
31
     * @config
32
     * @var array
33
     */
34
    private static $allowed_types = [
35
        'silverstripe-module',
36
        'silverstripe-vendormodule',
37
    ];
38
39
    /**
40
     * @var ComposerLoader
41
     */
42
    protected $composerLoader;
43
44
    /**
45
     * @var SupportedAddonsLoader
46
     */
47
    protected $supportedAddonsLoader;
48
49
    /**
50
     * Fetch the composer loader
51
     *
52
     * @return ComposerLoader
53
     */
54
    public function getComposerLoader()
55
    {
56
        return $this->composerLoader;
57
    }
58
59
    /**
60
     * set composer loader - provided for use with Injector {@see Injector}
61
     *
62
     * @param ComposerLoader $composerLoader
63
     *
64
     * @return UpdatePackageInfoTask $this
65
     */
66
    public function setComposerLoader($composerLoader)
67
    {
68
        $this->composerLoader = $composerLoader;
69
        return $this;
70
    }
71
72
    /**
73
     * @return SupportedAddonsLoader
74
     */
75
    public function getSupportedAddonsLoader()
76
    {
77
        return $this->supportedAddonsLoader;
78
    }
79
80
    /**
81
     * @param SupportedAddonsLoader $supportedAddonsLoader
82
     * @return $this
83
     */
84
    public function setSupportedAddonsLoader(SupportedAddonsLoader $supportedAddonsLoader)
85
    {
86
        $this->supportedAddonsLoader = $supportedAddonsLoader;
87
        return $this;
88
    }
89
90
    public function getTitle()
91
    {
92
        return _t(__CLASS__ . '.TITLE', 'Refresh installed package info');
93
    }
94
95
    public function getDescription()
96
    {
97
        return _t(
98
            __CLASS__ . '.DESCRIPTION',
99
            'Repopulates installation summary, listing installed modules'.
100
                ' and information associated with each.'
101
        );
102
    }
103
104
    /**
105
     * Update database cached information about this site.
106
     *
107
     * @param HTTPRequest $request unused, can be null (must match signature of parent function).
108
     */
109
    public function run($request)
110
    {
111
        $composerLock = $this->getComposerLoader()->getLock();
112
        $rawPackages = array_merge($composerLock->packages, (array) $composerLock->{'packages-dev'});
113
        $packages = $this->getPackageInfo($rawPackages);
114
115
        $supportedPackages = $this->getSupportedPackages();
116
117
        // Extensions to the process that add data may rely on external services.
118
        // There may be a communication issue between the site and the external service,
119
        // so if there are 'none' we should assume this is untrue and _not_ proceed
120
        // to remove everything. Stale information is better than no information.
121
        if ($packages) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $packages of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
122
            // There is no onBeforeDelete for Package
123
            SQLDelete::create('"Package"')->execute();
124
            foreach ($packages as $package) {
125
                if (is_array($supportedPackages)) {
126
                    $package['Supported'] = in_array($package['Name'], $supportedPackages);
127
                }
128
                Package::create()->update($package)->write();
129
            }
130
        }
131
    }
132
133
    /**
134
     * Fetch information about the installed packages.
135
     *
136
     * @param array $packageList list of packages as objects, formatted as one finds in a composer.lock
137
     *
138
     * @return array indexed array of package information, represented as associative arrays.
139
     */
140
    public function getPackageInfo($packageList)
141
    {
142
        $formatInfo = function ($package) {
143
            // Convert object to array, with Capitalised keys
144
            $package = get_object_vars($package);
145
            return array_combine(
146
                array_map('ucfirst', array_keys($package)),
147
                $package
148
            );
149
        };
150
151
        $packageList = array_map($formatInfo, $packageList);
152
        $this->extend('updatePackageInfo', $packageList);
153
        return $packageList;
154
    }
155
156
    /**
157
     * Return an array of supported modules as fetched from addons.silverstripe.org. Outputs a message and returns null
158
     * if an error occurs
159
     *
160
     * @return null|array
161
     */
162
    public function getSupportedPackages()
163
    {
164
        try {
165
            return $this->getSupportedAddonsLoader()->getAddonNames() ?: [];
166
        } catch (RuntimeException $exception) {
167
            echo $exception->getMessage() . PHP_EOL;
168
        }
169
170
        return null;
171
    }
172
}
173