Completed
Pull Request — master (#40)
by
unknown
13:57
created

CheckComposerUpdatesExtension::getUpdateChecker()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
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 RuntimeException;
9
use SS_Log;
10
use UpdatePackageInfoTask;
11
12
/**
13
 * Task which does the actual checking of updates
14
 *
15
 * Originally from https://github.com/XploreNet/silverstripe-composerupdates
16
 *
17
 * @author Matt Dwen
18
 * @license MIT
19
 */
20
class CheckComposerUpdatesExtension extends Extension
21
{
22
    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...
23
        'UpdateChecker' => '%$BringYourOwnIdeas\\UpdateChecker\\UpdateChecker',
24
    ];
25
26
    /**
27
     * @var UpdateChecker
28
     */
29
    protected $updateChecker;
30
31
    /**
32
     * Runs the actual steps to verify if there are updates available
33
     *
34
     * @param array[] $installedPackageList
35
     */
36
    public function updatePackageInfo(array &$installedPackageList)
37
    {
38
        // Fetch types of packages that are "allowed" - ie. dependencies that we actually care about
39
        $allowedTypes = (array) Config::inst()->get(UpdatePackageInfoTask::class, 'allowed_types');
40
        $composerPackagesAndConstraints = $this->owner->getComposerLoader()->getPackages($allowedTypes);
41
42
        // Loop list of packages given by owner task
43
        foreach ($installedPackageList as &$installedPackage) {
44
            /** @var array $installedPackage */
45
            if (empty($installedPackage['Name'])) {
46
                continue;
47
            }
48
            $packageName = $installedPackage['Name'];
49
50
            // Continue if we have no composer constraint details
51
            if (!isset($composerPackagesAndConstraints[$packageName])) {
52
                continue;
53
            }
54
            $packageData = $composerPackagesAndConstraints[$packageName];
55
56
            try {
57
                // Check for a relevant update version to recommend returned as keyed array and add to existing package
58
                // details array
59
                $updates = $this->getUpdateChecker()
60
                    ->checkForUpdates($packageData['package'], $packageData['constraint']);
61
            } catch (RuntimeException $ex) {
62
                // If exceptions are thrown during execution, fail gracefully and allow the rest of the report
63
                // generation to continue
64
                $updates = [];
65
                SS_Log::log($ex->getMessage(), SS_Log::WARN);
66
            }
67
68
            $installedPackage = array_merge($installedPackage, $updates);
69
        }
70
    }
71
72
    /**
73
     * @param UpdateChecker $updateChecker
74
     * @return $this
75
     */
76
    public function setUpdateChecker(UpdateChecker $updateChecker)
77
    {
78
        $this->updateChecker = $updateChecker;
79
        return $this;
80
    }
81
82
    /**
83
     * @return UpdateChecker
84
     */
85
    public function getUpdateChecker()
86
    {
87
        return $this->updateChecker;
88
    }
89
}
90