@@ -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,15 @@ public function isChange()
102113 }
103114
104115 /**
105- * @return PackageInterface|null
116+ * @return string
117+ */
118+ public function getPackageName ()
119+ {
120+ return $ this ->getPackage ()->getName ();
121+ }
122+
123+ /**
124+ * @return PackageInterface
106125 */
107126 public function getPackage ()
108127 {
@@ -116,7 +135,7 @@ public function getPackage()
116135 return $ operation ->getPackage ();
117136 }
118137
119- return null ;
138+ throw new \ InvalidArgumentException ( ' Invalid operation ' ) ;
120139 }
121140
122141 /**
@@ -134,87 +153,97 @@ public function getLicenses()
134153 }
135154
136155 /**
137- * @return array<string, string|bool|null|string[]>
156+ * @return array{
157+ * name: string,
158+ * direct: bool,
159+ * operation: string,
160+ * version_base: string|null,
161+ * version_target: string|null,
162+ * licenses: string[],
163+ * compare: string|null,
164+ * link: string|null,
165+ * }
138166 */
139- public function toArray (UrlGenerator $ generator )
167+ public function toArray ()
140168 {
141- $ array = $ this ->toBaseArray ();
142- $ array ['compare ' ] = $ this ->getUrl ($ generator );
143- $ array ['link ' ] = $ this ->getProjectUrl ($ generator );
144-
145- return $ array ;
169+ return array (
170+ 'name ' => $ this ->getPackageName (),
171+ 'direct ' => $ this ->isDirect (),
172+ 'operation ' => $ this ->getType (),
173+ 'version_base ' => $ this ->getBaseVersion (),
174+ 'version_target ' => $ this ->getTargetVersion (),
175+ 'licenses ' => $ this ->getLicenses (),
176+ 'compare ' => $ this ->getUrl (),
177+ 'link ' => $ this ->getProjectUrl (),
178+ );
146179 }
147180
148181 /**
149- * @return array< string, string|bool| null|string[]>
182+ * @return string| null
150183 */
151- public function toBaseArray ()
184+ public function getBaseVersion ()
152185 {
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- );
186+ if ($ this ->operation instanceof UpdateOperation) {
187+ return $ this ->operation ->getInitialPackage ()->getFullPrettyVersion ();
175188 }
176189
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- );
190+ if ($ this ->operation instanceof UninstallOperation) {
191+ return $ this ->operation ->getPackage ()->getFullPrettyVersion ();
186192 }
187193
188- throw new \ InvalidArgumentException ( ' Invalid operation ' ) ;
194+ return null ;
189195 }
190196
191197 /**
192198 * @return string|null
193199 */
194- public function getUrl ( UrlGenerator $ generator )
200+ public function getTargetVersion ( )
195201 {
196- $ operation = $ this ->getOperation ();
197-
198- if ($ operation instanceof UpdateOperation) {
199- return $ generator ->getCompareUrl ($ operation ->getInitialPackage (), $ operation ->getTargetPackage ());
202+ if ($ this ->operation instanceof UpdateOperation) {
203+ return $ this ->operation ->getTargetPackage ()->getFullPrettyVersion ();
200204 }
201205
202- if ($ operation instanceof InstallOperation || $ operation instanceof UninstallOperation ) {
203- return $ generator -> getReleaseUrl ( $ operation ->getPackage ());
206+ if ($ this -> operation instanceof InstallOperation) {
207+ return $ this -> operation ->getPackage ()-> getFullPrettyVersion ( );
204208 }
205209
206210 return null ;
207211 }
208212
209- public function getProjectUrl (UrlGenerator $ generator )
213+ /**
214+ * @return string|null
215+ */
216+ public function getUrl ()
210217 {
211- $ package = $ this ->getPackage ();
218+ return $ this ->compareUrl ;
219+ }
212220
213- if (!isset ($ package )) {
214- return null ;
221+ /**
222+ * @return string|null
223+ */
224+ public function getProjectUrl ()
225+ {
226+ return $ this ->projectUrl ;
227+ }
228+
229+ /**
230+ * @return void
231+ */
232+ private function setUrls (UrlGenerator $ generator )
233+ {
234+ $ this ->projectUrl = $ generator ->getProjectUrl ($ this ->getPackage ());
235+
236+ $ operation = $ this ->getOperation ();
237+
238+ if ($ operation instanceof UpdateOperation) {
239+ $ this ->compareUrl = $ generator ->getCompareUrl ($ operation ->getInitialPackage (), $ operation ->getTargetPackage ());
240+
241+ return ;
215242 }
216243
217- return $ generator ->getProjectUrl ($ package );
244+ if ($ operation instanceof InstallOperation || $ operation instanceof UninstallOperation) {
245+ $ this ->compareUrl = $ generator ->getReleaseUrl ($ operation ->getPackage ());
246+ }
218247 }
219248
220249 /**
0 commit comments