Completed
Push — master ( 55accb...6b95b9 )
by
unknown
17:55
created

CheckComposerUpdatesExtension::getUpdateChecker()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace BringYourOwnIdeas\UpdateChecker\Extensions;
4
5
use BringYourOwnIdeas\UpdateChecker\UpdateChecker;
6
use Config;
7
use Extension;
8
use UpdatePackageInfoTask;
9
10
/**
11
 * Task which does the actual checking of updates
12
 *
13
 * Originally from https://github.com/XploreNet/silverstripe-composerupdates
14
 *
15
 * @author Matt Dwen
16
 * @license MIT
17
 */
18
class CheckComposerUpdatesExtension extends Extension
19
{
20
    private static $dependencies = [
0 ignored issues
show
Unused Code introduced by
The property $dependencies is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
21
        'UpdateChecker' => '%$BringYourOwnIdeas\\UpdateChecker\\UpdateChecker',
22
    ];
23
24
    /**
25
     * @var UpdateChecker
26
     */
27
    protected $updateChecker;
28
29
    /**
30
     * Runs the actual steps to verify if there are updates available
31
     *
32
     * @param array[] $installedPackageList
33
     */
34
    public function updatePackageInfo(array &$installedPackageList)
35
    {
36
        // Fetch types of packages that are "allowed" - ie. dependencies that we actually care about
37
        $allowedTypes = (array) Config::inst()->get(UpdatePackageInfoTask::class, 'allowed_types');
38
        $composerPackagesAndConstraints = $this->owner->getComposerLoader()->getPackages($allowedTypes);
39
40
        // Loop list of packages given by owner task
41
        foreach ($installedPackageList as &$installedPackage) {
42
            /** @var array $installedPackage */
43
            if (empty($installedPackage['Name'])) {
44
                continue;
45
            }
46
            $packageName = $installedPackage['Name'];
47
48
            // Continue if we have no composer constraint details
49
            if (!isset($composerPackagesAndConstraints[$packageName])) {
50
                continue;
51
            }
52
            $packageData = $composerPackagesAndConstraints[$packageName];
53
54
            // Check for a relevant update version to recommend returned as keyed array and add to existing package
55
            // details array
56
            $updates = $this->getUpdateChecker()->checkForUpdates($packageData['package'], $packageData['constraint']);
57
            $installedPackage = array_merge($installedPackage, $updates);
58
        }
59
    }
60
61
    /**
62
     * @param UpdateChecker $updateChecker
63
     * @return $this
64
     */
65
    public function setUpdateChecker(UpdateChecker $updateChecker)
66
    {
67
        $this->updateChecker = $updateChecker;
68
        return $this;
69
    }
70
71
    /**
72
     * @return UpdateChecker
73
     */
74
    public function getUpdateChecker()
75
    {
76
        return $this->updateChecker;
77
    }
78
}
79