SilverStripeVersionUpdater::update()   C
last analyzed

Complexity

Conditions 7
Paths 9

Size

Total Lines 47
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 29
nc 9
nop 0
1
<?php
2
/**
3
 * Updates the available SilverStripe versions.
4
 */
5
6
use Composer\Package\Version\VersionParser;
7
8
class SilverStripeVersionUpdater
9
{
10
11
    /**
12
     * @var PackagistService
13
     */
14
    private $packagist;
15
16
    /**
17
     * @config
18
     * @var array
19
     */
20
    protected $versionBlacklist = array('2.5.x-dev');
21
22
    public function __construct(PackagistService $packagist)
23
    {
24
        $this->packagist = $packagist;
25
    }
26
27
    public function update()
28
    {
29
        $versions = $this->packagist->getPackageVersions('silverstripe/framework');
30
31
        foreach ($versions as $package) {
32
            $version = $package->getVersion();
33
34
            // Replace version by branch alias if applicable
35
            $extra = $package->getExtra();
36
            if (isset($extra['branch-alias'][$version])) {
37
                $version = $extra['branch-alias'][$version];
38
            }
39
            $stability = VersionParser::parseStability($version);
40
41
            $isDev = $stability === 'dev';
42
            
43
            if (!$isDev || in_array($version, $this->versionBlacklist)) {
44
                continue;
45
            }
46
47
            $match = preg_match(
48
                '/^([0-9]+)\.([0-9]+)\.x-dev$/',
49
                $version,
50
                $matches
51
            );
52
53
            if (!$match) {
54
                continue;
55
            }
56
57
            $major = $matches[1];
58
            $minor = $matches[2];
59
60
            $record = SilverStripeVersion::get()
61
                ->filter('Major', $major)
62
                ->filter('Minor', $minor)
63
                ->first();
64
65
            if (!$record) {
66
                $record = new SilverStripeVersion();
67
                $record->Name = "$major.$minor";
0 ignored issues
show
Documentation introduced by
The property Name does not exist on object<SilverStripeVersion>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
68
                $record->Major = $major;
0 ignored issues
show
Documentation introduced by
The property Major does not exist on object<SilverStripeVersion>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
69
                $record->Minor = $minor;
0 ignored issues
show
Documentation introduced by
The property Minor does not exist on object<SilverStripeVersion>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
70
                $record->write();
71
            }
72
        }
73
    }
74
75
    public function setVersionBlacklist($list)
76
    {
77
        $this->versionBlacklist = $list;
78
    }
79
80
    public function getVersionBlacklist()
81
    {
82
        return $this->versionBlacklist;
83
    }
84
}
85