@@ -27,14 +27,25 @@ class DiffEntry
2727 /** @var string */
2828 private $ type ;
2929
30+ /** @var string|null */
31+ private $ compareUrl ;
32+
33+ /** @var string|null */
34+ private $ projectUrl ;
35+
3036 /**
31- * @param bool $direct
37+ * @param UrlGenerator|null $urlGenerator
38+ * @param bool $direct
3239 */
33- public function __construct (OperationInterface $ operation , $ direct = false )
40+ public function __construct (OperationInterface $ operation , $ urlGenerator = null , $ direct = false )
3441 {
3542 $ this ->operation = $ operation ;
3643 $ this ->direct = $ direct ;
3744 $ this ->type = $ this ->determineType ();
45+
46+ if ($ urlGenerator instanceof UrlGenerator) {
47+ $ this ->setUrls ($ urlGenerator );
48+ }
3849 }
3950
4051 /**
@@ -102,7 +113,7 @@ public function isChange()
102113 }
103114
104115 /**
105- * @return PackageInterface|null
116+ * @return PackageInterface
106117 */
107118 public function getPackage ()
108119 {
@@ -116,7 +127,7 @@ public function getPackage()
116127 return $ operation ->getPackage ();
117128 }
118129
119- return null ;
130+ throw new \ InvalidArgumentException ( ' Invalid operation ' ) ;
120131 }
121132
122133 /**
@@ -134,87 +145,101 @@ public function getLicenses()
134145 }
135146
136147 /**
137- * @return array<string, string|bool|null|string[]>
148+ * @return array{
149+ * name: string,
150+ * direct: bool,
151+ * operation: string,
152+ * version_base: string|null,
153+ * version_target: string|null,
154+ * licenses: string[],
155+ * compare: string|null,
156+ * link: string|null,
157+ * }
138158 */
139- public function toArray (UrlGenerator $ generator )
159+ public function toArray ()
140160 {
141- $ array = $ this ->toBaseArray ();
142- $ array ['compare ' ] = $ this ->getUrl ($ generator );
143- $ array ['link ' ] = $ this ->getProjectUrl ($ generator );
144-
145- return $ array ;
161+ return array (
162+ 'name ' => $ this ->getPackage ()->getName (),
163+ 'direct ' => $ this ->isDirect (),
164+ 'operation ' => $ this ->getType (),
165+ 'version_base ' => $ this ->getBaseVersion (),
166+ 'version_target ' => $ this ->getTargetVersion (),
167+ 'licenses ' => $ this ->getLicenses (),
168+ 'compare ' => $ this ->getUrl (),
169+ 'link ' => $ this ->getProjectUrl (),
170+ );
146171 }
147172
148173 /**
149- * @return array< string, string|bool| null|string[]>
174+ * @return string| null
150175 */
151- public function toBaseArray ()
176+ public function getBaseVersion ()
152177 {
153- $ operation = $ this ->getOperation ();
154-
155- if ($ operation instanceof InstallOperation) {
156- return array (
157- 'name ' => $ operation ->getPackage ()->getName (),
158- 'direct ' => $ this ->isDirect (),
159- 'operation ' => $ this ->getType (),
160- 'version_base ' => null ,
161- 'version_target ' => $ operation ->getPackage ()->getFullPrettyVersion (),
162- 'licenses ' => $ this ->getLicenses (),
163- );
164- }
165-
166- if ($ operation instanceof UpdateOperation) {
167- return array (
168- 'name ' => $ operation ->getInitialPackage ()->getName (),
169- 'direct ' => $ this ->isDirect (),
170- 'operation ' => $ this ->getType (),
171- 'version_base ' => $ operation ->getInitialPackage ()->getFullPrettyVersion (),
172- 'version_target ' => $ operation ->getTargetPackage ()->getFullPrettyVersion (),
173- 'licenses ' => $ this ->getLicenses (),
174- );
178+ if ($ this ->operation instanceof UpdateOperation) {
179+ return $ this ->operation ->getInitialPackage ()->getFullPrettyVersion ();
175180 }
176181
177- if ($ operation instanceof UninstallOperation) {
178- return array (
179- 'name ' => $ operation ->getPackage ()->getName (),
180- 'direct ' => $ this ->isDirect (),
181- 'operation ' => $ this ->getType (),
182- 'version_base ' => $ operation ->getPackage ()->getFullPrettyVersion (),
183- 'version_target ' => null ,
184- 'licenses ' => $ this ->getLicenses (),
185- );
182+ if ($ this ->operation instanceof UninstallOperation) {
183+ return $ this ->operation ->getPackage ()->getFullPrettyVersion ();
186184 }
187185
188- throw new \ InvalidArgumentException ( ' Invalid operation ' ) ;
186+ return null ;
189187 }
190188
191189 /**
192190 * @return string|null
193191 */
194- public function getUrl ( UrlGenerator $ generator )
192+ public function getTargetVersion ( )
195193 {
196- $ operation = $ this ->getOperation ();
197-
198- if ($ operation instanceof UpdateOperation) {
199- return $ generator ->getCompareUrl ($ operation ->getInitialPackage (), $ operation ->getTargetPackage ());
194+ if ($ this ->operation instanceof UpdateOperation) {
195+ return $ this ->operation ->getTargetPackage ()->getFullPrettyVersion ();
200196 }
201197
202- if ($ operation instanceof InstallOperation || $ operation instanceof UninstallOperation ) {
203- return $ generator -> getReleaseUrl ( $ operation ->getPackage ());
198+ if ($ this -> operation instanceof InstallOperation) {
199+ return $ this -> operation ->getPackage ()-> getFullPrettyVersion ( );
204200 }
205201
206202 return null ;
207203 }
208204
209- public function getProjectUrl (UrlGenerator $ generator )
205+ /**
206+ * @return string|null
207+ */
208+ public function getUrl ()
209+ {
210+ return $ this ->compareUrl ;
211+ }
212+
213+ /**
214+ * @return string|null
215+ */
216+ public function getProjectUrl ()
217+ {
218+ return $ this ->projectUrl ;
219+ }
220+
221+ /**
222+ * @return void
223+ */
224+ private function setUrls (UrlGenerator $ generator )
210225 {
211226 $ package = $ this ->getPackage ();
212227
213- if (! isset ($ package )) {
214- return null ;
228+ if (isset ($ package )) {
229+ $ this -> projectUrl = $ generator -> getProjectUrl ( $ package ) ;
215230 }
216231
217- return $ generator ->getProjectUrl ($ package );
232+ $ operation = $ this ->getOperation ();
233+
234+ if ($ operation instanceof UpdateOperation) {
235+ $ this ->compareUrl = $ generator ->getCompareUrl ($ operation ->getInitialPackage (), $ operation ->getTargetPackage ());
236+
237+ return ;
238+ }
239+
240+ if ($ operation instanceof InstallOperation || $ operation instanceof UninstallOperation) {
241+ $ this ->compareUrl = $ generator ->getReleaseUrl ($ operation ->getPackage ());
242+ }
218243 }
219244
220245 /**
0 commit comments