Completed
Push — feature/ss4-upgrade ( f41a3f )
by
unknown
10:12
created

SilverStripeVersionUpdater::update()   C

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
c 0
b 0
f 0
rs 6.7272
cc 7
eloc 29
nc 9
nop 0
1
<?php
2
3
namespace SilverStripe\Addons\Services;
4
5
use SilverStripe\Addons\Model\SilverStripeVersion;
6
use Composer\Package\Version\VersionParser;
7
8
/**
9
 * Updates the available SilverStripe versions.
10
 */
11
class SilverStripeVersionUpdater 
12
{
13
14
	/**
15
	 * @var PackagistService
16
	 */
17
	private $packagist;
18
19
	/**
20
	 * @config
21
	 * @var array
22
	 */
23
	protected $versionBlacklist = ['2.5.x-dev'];
24
25
	public function __construct(PackagistService $packagist) 
26
	{
27
		$this->packagist = $packagist;
28
	}
29
30
	public function update() 
31
	{
32
		$versions = $this->packagist->getPackageVersions('silverstripe/framework');
33
34
		foreach ($versions as $package) {
35
			$version = $package->getVersion();
36
37
			// Replace version by branch alias if applicable
38
			$extra = $package->getExtra();
39
			if(isset($extra['branch-alias'][$version])) {
40
				$version = $extra['branch-alias'][$version];
41
			}
42
			$stability = VersionParser::parseStability($version);
43
44
			$isDev = $stability === 'dev';
45
			
46
			if (!$isDev || in_array($version, $this->versionBlacklist)) {
47
				continue;
48
			}
49
50
			$match = preg_match(
51
				'/^([0-9]+)\.([0-9]+)\.x-dev$/',
52
				$version,
53
				$matches
54
			);
55
56
			if (!$match) {
57
				continue;
58
			}
59
60
			$major = $matches[1];
61
			$minor = $matches[2];
62
63
			$record = SilverStripeVersion::get()
64
				->filter('Major', $major)
65
				->filter('Minor', $minor)
66
				->first();
67
68
			if (!$record) {
69
				$record = new SilverStripeVersion();
70
				$record->Name = "$major.$minor";
0 ignored issues
show
Documentation introduced by
The property Name does not exist on object<SilverStripe\Addo...el\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...
71
				$record->Major = $major;
0 ignored issues
show
Documentation introduced by
The property Major does not exist on object<SilverStripe\Addo...el\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...
72
				$record->Minor = $minor;
0 ignored issues
show
Documentation introduced by
The property Minor does not exist on object<SilverStripe\Addo...el\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...
73
				$record->write();
74
			}
75
		}
76
	}
77
78
	public function setVersionBlacklist($list) 
79
	{
80
		$this->versionBlacklist = $list;
81
	}
82
83
	public function getVersionBlacklist() 
84
	{
85
		return $this->versionBlacklist;
86
	}
87
88
}
89