-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathDiffEntry.php
More file actions
273 lines (227 loc) · 6.07 KB
/
DiffEntry.php
File metadata and controls
273 lines (227 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
namespace IonBazan\ComposerDiff\Diff;
use Composer\DependencyResolver\Operation\InstallOperation;
use Composer\DependencyResolver\Operation\OperationInterface;
use Composer\DependencyResolver\Operation\UninstallOperation;
use Composer\DependencyResolver\Operation\UpdateOperation;
use Composer\Package\CompletePackageInterface;
use Composer\Package\PackageInterface;
use IonBazan\ComposerDiff\Url\UrlGenerator;
class DiffEntry
{
const TYPE_INSTALL = 'install';
const TYPE_UPGRADE = 'upgrade';
const TYPE_DOWNGRADE = 'downgrade';
const TYPE_REMOVE = 'remove';
const TYPE_CHANGE = 'change';
/** @var OperationInterface */
private $operation;
/** @var bool */
private $direct;
/** @var string */
private $type;
/** @var string|null */
private $compareUrl;
/** @var string|null */
private $projectUrl;
/**
* @param UrlGenerator|null $urlGenerator
* @param bool $direct
*/
public function __construct(OperationInterface $operation, $urlGenerator = null, $direct = false)
{
$this->operation = $operation;
$this->direct = $direct;
$this->type = $this->determineType();
if ($urlGenerator instanceof UrlGenerator) {
$this->setUrls($urlGenerator);
}
}
/**
* @return OperationInterface
*/
public function getOperation()
{
return $this->operation;
}
/**
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* @return bool
*/
public function isDirect()
{
return $this->direct;
}
/**
* @return bool
*/
public function isInstall()
{
return self::TYPE_INSTALL === $this->type;
}
/**
* @return bool
*/
public function isUpgrade()
{
return self::TYPE_UPGRADE === $this->type;
}
/**
* @return bool
*/
public function isDowngrade()
{
return self::TYPE_DOWNGRADE === $this->type;
}
/**
* @return bool
*/
public function isRemove()
{
return self::TYPE_REMOVE === $this->type;
}
/**
* @return bool
*/
public function isChange()
{
return self::TYPE_CHANGE === $this->type;
}
/**
* @return string
*/
public function getPackageName()
{
return $this->getPackage()->getName();
}
/**
* @return PackageInterface
*/
public function getPackage()
{
$operation = $this->getOperation();
if ($operation instanceof UpdateOperation) {
return $operation->getInitialPackage();
}
if ($operation instanceof InstallOperation || $operation instanceof UninstallOperation) {
return $operation->getPackage();
}
throw new \InvalidArgumentException('Invalid operation');
}
/**
* @return string[]
*/
public function getLicenses()
{
$package = $this->getPackage();
if (!$package instanceof CompletePackageInterface) {
return array();
}
return $package->getLicense();
}
/**
* @return array{
* name: string,
* direct: bool,
* operation: string,
* version_base: string|null,
* version_target: string|null,
* licenses: string[],
* compare: string|null,
* link: string|null,
* }
*/
public function toArray()
{
return array(
'name' => $this->getPackageName(),
'direct' => $this->isDirect(),
'operation' => $this->getType(),
'version_base' => $this->getBaseVersion(),
'version_target' => $this->getTargetVersion(),
'licenses' => $this->getLicenses(),
'compare' => $this->getUrl(),
'link' => $this->getProjectUrl(),
);
}
/**
* @return string|null
*/
public function getBaseVersion()
{
if ($this->operation instanceof UpdateOperation) {
return $this->operation->getInitialPackage()->getFullPrettyVersion();
}
if ($this->operation instanceof UninstallOperation) {
return $this->operation->getPackage()->getFullPrettyVersion();
}
return null;
}
/**
* @return string|null
*/
public function getTargetVersion()
{
if ($this->operation instanceof UpdateOperation) {
return $this->operation->getTargetPackage()->getFullPrettyVersion();
}
if ($this->operation instanceof InstallOperation) {
return $this->operation->getPackage()->getFullPrettyVersion();
}
return null;
}
/**
* @return string|null
*/
public function getUrl()
{
return $this->compareUrl;
}
/**
* @return string|null
*/
public function getProjectUrl()
{
return $this->projectUrl;
}
/**
* @return void
*/
private function setUrls(UrlGenerator $generator)
{
$package = $this->getPackage();
$this->projectUrl = $generator->getProjectUrl($package);
$operation = $this->getOperation();
if ($operation instanceof UpdateOperation) {
$this->compareUrl = $generator->getCompareUrl($operation->getInitialPackage(), $operation->getTargetPackage());
return;
}
$this->compareUrl = $generator->getReleaseUrl($package);
}
/**
* @return string
*/
private function determineType()
{
if ($this->operation instanceof InstallOperation) {
return self::TYPE_INSTALL;
}
if ($this->operation instanceof UninstallOperation) {
return self::TYPE_REMOVE;
}
if ($this->operation instanceof UpdateOperation) {
$upgrade = VersionComparator::isUpgrade($this->operation);
if (null === $upgrade) {
return self::TYPE_CHANGE;
}
return $upgrade ? self::TYPE_UPGRADE : self::TYPE_DOWNGRADE;
}
return self::TYPE_CHANGE;
}
}