Completed
Push — master ( fa7735...d98913 )
by Andrii
03:39 queued 01:35
created

AssetPackage::getFullName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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\Composer;
15
use Composer\Factory;
16
use Composer\IO\NullIO;
17
use Exception;
18
use Fxp\Composer\AssetPlugin\Repository\AssetVcsRepository;
19
use hiqdev\assetpackagist\registry\RegistryFactory;
20
21
class AssetPackage
22
{
23
    protected $_type;
24
    protected $_name;
25
    protected $_hash;
26
    protected $_releases = [];
27
    protected $_saved;
28
29
    /**
30
     * @var NullIO
31
     */
32
    protected $_io;
33
    /**
34
     * @var Composer
35
     */
36
    protected $_composer;
37
    /**
38
     * @var Storage
39
     */
40
    protected $_storage;
41
    /**
42
     * @var Composer
43
     */
44
    protected static $_commonComposer;
45
46 1
    public function __construct($type, $name)
47
    {
48 1
        if (!$this->checkType($type)) {
49
            throw new Exception('wrong type');
50
        }
51 1
        if (!$this->checkName($name)) {
52
            throw new Exception('wrong name');
53
        }
54 1
        $this->_type = $type;
55 1
        $this->_name = $name;
56 1
    }
57
58 1
    public function checkType($type)
59
    {
60 1
        return $type === 'bower' || $type === 'npm';
61
    }
62
63 1
    public function checkName($name)
64
    {
65 1
        return strlen($name) > 1;
66
    }
67
68 1
    public function getFullName()
69
    {
70 1
        return static::buildFullName($this->_type, $this->_name);
71
    }
72
73 1
    public static function buildFullName($type, $name)
74
    {
75 1
        return $type . '-asset/' . $name;
76
    }
77
78
    public static function splitFullName($full)
79
    {
80
        list($temp, $name) = explode('/', $full);
81
        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...
82
83
        return [$type, $name];
84
    }
85
86
    public function getType()
87
    {
88
        return $this->_type;
89
    }
90
91
    public function getName()
92
    {
93
        return $this->_name;
94
    }
95
96
    public function getHash()
97
    {
98
        return $this->_hash;
99
    }
100
101
    /**
102
     * @return Composer
103
     */
104
    public static function getCommonComposer()
105
    {
106
        if (static::$_commonComposer === null) {
107
            static::$_commonComposer = Factory::create(new NullIO());
108
        }
109
110
        return static::$_commonComposer;
111
    }
112
113
    public function setComposer($value)
114
    {
115
        $this->_composer = $value;
116
    }
117
118
    /**
119
     * @return Composer
120
     */
121
    public function getComposer()
122
    {
123
        if ($this->_composer === null) {
124
            $this->_composer = static::getCommonComposer();
125
        }
126
127
        return $this->_composer;
128
    }
129
130
    public function getIO()
131
    {
132
        if ($this->_io === null) {
133
            $this->_io = new NullIO();
134
        }
135
136
        return $this->_io;
137
    }
138
139
    /**
140
     * findOne.
141
     *
142
     * @param string $type
143
     * @param string $name
144
     * @return static|null
145
     */
146
    public static function findOne($type, $name)
147
    {
148
        $package = new static($type, $name);
149
        $package->load();
150
151
        return $package;
152
    }
153
154
    public function load()
155
    {
156
        $data = $this->getStorage()->readPackage($this);
157
        $this->_hash = $data['hash'];
158
        $this->_releases = $data['releases'];
159
    }
160
161
    public function update()
162
    {
163
        $registry = RegistryFactory::getRegistry($this->getType(), $this->getComposer()->getRepositoryManager());
164
        $repo = $registry->buildVcsRepository($this->getName());
165
        $this->_releases = $this->prepareReleases($repo);
166
        $this->_hash = $this->getStorage()->writePackage($this);
167
    }
168
169
    /**
170
     * @param AssetVcsRepository $repository
171
     * @return array
172
     */
173
    public function prepareReleases($repository)
174
    {
175
        $releases = [];
176
        foreach ($repository->getPackages() as $package) {
177
            $version = $package->getVersion();
178
            $release = [
179
                'uid'       => $this->prepareUid($version),
180
                'name'      => $this->getFullName(),
181
                'version'   => $version,
182
            ];
183
            if ($package->getDistUrl()) {
184
                $release['dist'] = [
185
                    'type'      => $package->getDistType(),
186
                    'url'       => $package->getDistUrl(),
187
                    'reference' => $package->getDistReference(),
188
                ];
189
            }
190
            if ($package->getSourceUrl()) {
191
                $release['source'] = [
192
                    'type'      => $package->getSourceType(),
193
                    'url'       => $package->getSourceUrl(),
194
                    'reference' => $package->getSourceReference(),
195
                ];
196
            }
197
            if ($release['dist'] || $release['source']) {
198
                $releases[$version] = $release;
199
            }
200
        }
201
202
        return $releases;
203
    }
204
205
    public function prepareUid($version)
206
    {
207
        $known = $this->getSaved()->getRelease($version);
208
209
        return isset($known['uid']) ? $known['uid'] : $this->getStorage()->getNextID();
210
    }
211
212
    public function getReleases()
213
    {
214
        return $this->_releases;
215
    }
216
217
    public function getRelease($version)
218
    {
219
        return isset($this->_releases[$version]) ? $this->_releases[$version] : [];
220
    }
221
222
    public function getSaved()
223
    {
224
        if ($this->_saved === null) {
225
            $this->_saved = static::findOne($this->getType(), $this->getName());
226
        }
227
228
        return $this->_saved;
229
    }
230
231
    public function getStorage()
232
    {
233
        if ($this->_storage === null) {
234
            $this->_storage = Storage::getInstance();
235
        }
236
237
        return $this->_storage;
238
    }
239
}
240