Completed
Push — master ( cf2d54...6e59f3 )
by Andrii
05:31
created

AssetPackage::getHash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * Asset Packagist
5
 *
6
 * @link      https://github.com/hiqdev/asset-packagist
7
 * @package   asset-packagist
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiqdev\assetpackagist\models;
13
14
use Composer\Factory;
15
use Composer\IO\NullIO;
16
use Exception;
17
use hiqdev\assetpackagist\registry\RegistryFactory;
18
19
class AssetPackage
20
{
21
    protected $_type;
22
    protected $_name;
23
    protected $_hash;
24
    protected $_releases = [];
25
    protected $_saved;
26
27
    protected $_io;
28
    protected $_composer;
29
    protected $_storage;
30
31
    protected static $_commonComposer;
32
33 1
    public function __construct($type, $name)
34
    {
35 1
        if (!$this->checkType($type)) {
36
            throw new Exception('wrong type');
37
        }
38 1
        if (!$this->checkName($name)) {
39
            throw new Exception('wrong name');
40
        }
41 1
        $this->_type = $type;
42 1
        $this->_name = $name;
43 1
    }
44
45 1
    public function checkType($type)
46
    {
47 1
        return $type === 'bower' || $type === 'npm';
48
    }
49
50 1
    public function checkName($name)
51
    {
52 1
        return strlen($name) > 1;
53
    }
54
55 1
    public function getFullName()
56
    {
57 1
        return static::buildFullName($this->_type, $this->_name);
58
    }
59
60 1
    public static function buildFullName($type, $name)
61
    {
62 1
        return $type . '-asset/' . $name;
63
    }
64
65
    public static function splitFullName($full)
66
    {
67
        list($temp, $name) = explode('/', $full);
68
        list($type, $temp) = explode('-', $temp);
0 ignored issues
show
Unused Code introduced by
The assignment to $temp is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
69
70
        return [$type, $name];
71
    }
72
73
    public function getType()
74
    {
75
        return $this->_type;
76
    }
77
78
    public function getName()
79
    {
80
        return $this->_name;
81
    }
82
83
    public function getHash()
84
    {
85
        return $this->_hash;
86
    }
87
88
    public static function getCommonComposer()
89
    {
90
        if (static::$_commonComposer === null) {
91
            static::$_commonComposer = Factory::create(new NullIO());
92
            #$factory = new Factory();
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
93
            #static::$_commonComposer = $factory->createComposer(new NullIO(), null, false, null, false);
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
94
        }
95
96
        return static::$_commonComposer;
97
    }
98
99
    public function setComposer($value)
100
    {
101
        $this->_composer = $value;
102
    }
103
104
    public function getComposer()
105
    {
106
        if ($this->_composer === null) {
107
            $this->_composer = static::getCommonComposer();
108
        }
109
110
        return $this->_composer;
111
    }
112
113
    public function getIO()
114
    {
115
        if ($this->_io === null) {
116
            $this->_io = new NullIO();
117
        }
118
119
        return $this->_io;
120
    }
121
122
    /**
123
     * findOne.
124
     *
125
     * @param string $type
126
     * @param string $name
127
     * @return static|null
128
     */
129
    public static function findOne($type, $name)
130
    {
131
        $package = new static($type, $name);
132
        $package->load();
133
134
        return $package;
135
    }
136
137
    public function load()
138
    {
139
        $data = $this->getStorage()->readPackage($this);
140
        $this->_hash = $data['hash'];
141
        $this->_releases = $data['releases'];
142
    }
143
144
    public function update()
145
    {
146
        $registry = RegistryFactory::getRegistry($this->getType(), $this->getComposer()->getRepositoryManager());
147
        $repo = $registry->buildVcsRepository($this->getName());
148
        $this->_releases = $this->prepareReleases($repo);
149
        $this->_hash = $this->getStorage()->writePackage($this);
150
    }
151
152
    public function prepareReleases($repo)
153
    {
154
        $releases = [];
155
        foreach ($repo->getPackages() as $package) {
156
            $version = $package->getVersion();
157
            $release = [
158
                'uid'       => $this->prepareUid($version),
159
                'name'      => $this->getFullName(),
160
                'version'   => $version,
161
            ];
162
            if ($package->getDistUrl()) {
163
                $release['dist'] = [
164
                    'type'      => $package->getDistType(),
165
                    'url'       => $package->getDistUrl(),
166
                    'reference' => $package->getDistReference(),
167
                ];
168
            }
169
            if ($package->getSourceUrl()) {
170
                $release['source'] = [
171
                    'type'      => $package->getSourceType(),
172
                    'url'       => $package->getSourceUrl(),
173
                    'reference' => $package->getSourceReference(),
174
                ];
175
            }
176
            if ($release['dist'] || $release['source']) {
177
                $releases[$version] = $release;
178
            }
179
        }
180
181
        return $releases;
182
    }
183
184
    public function prepareUid($version)
185
    {
186
        $known = $this->getSaved()->getRelease($version);
187
188
        return isset($known['uid']) ? $known['uid'] : $this->getStorage()->getNextID();
189
    }
190
191
    public function getReleases()
192
    {
193
        return $this->_releases;
194
    }
195
196
    public function getRelease($version)
197
    {
198
        return isset($this->_releases[$version]) ? $this->_releases[$version] : [];
199
    }
200
201
    public function getSaved()
202
    {
203
        if ($this->_saved === null) {
204
            $this->_saved = static::findOne($this->getType(), $this->getName());
205
        }
206
207
        return $this->_saved;
208
    }
209
210
    public function getStorage()
211
    {
212
        if ($this->_storage === null) {
213
            $this->_storage = Storage::getInstance();
214
        }
215
216
        return $this->_storage;
217
    }
218
}
219