55namespace NuonicPluginInstaller \Action ;
66
77use Composer \Semver \Semver ;
8+ use NuonicPluginInstaller \Core \Framework \Plugin \AvailableOpensourcePlugin \AvailableOpensourcePluginCollection ;
89use NuonicPluginInstaller \Core \Framework \Plugin \AvailableOpensourcePlugin \AvailableOpensourcePluginEntity ;
910use NuonicPluginInstaller \Service \IndexFileServiceInterface ;
1011use NuonicPluginInstaller \Struct \PackageIndexEntry ;
12+ use Shopware \Administration \Notification \NotificationCollection ;
1113use Shopware \Core \Framework \Adapter \Cache \CacheValueCompressor ;
1214use Shopware \Core \Framework \Context ;
1315use Shopware \Core \Framework \DataAbstractionLayer \EntityRepository ;
1416use Shopware \Core \Framework \DataAbstractionLayer \Search \Criteria ;
1517use Shopware \Core \Framework \DataAbstractionLayer \Search \Filter \EqualsFilter ;
1618use Shopware \Core \Framework \Uuid \Uuid ;
19+ use Shopware \Core \System \Language \LanguageCollection ;
1720use Symfony \Component \Yaml \Yaml ;
1821use Symfony \Contracts \Cache \CacheInterface ;
1922use Symfony \Contracts \Cache \ItemInterface ;
2528
2629readonly class LoadPluginAction
2730{
31+
32+ /**
33+ * @param EntityRepository<AvailableOpensourcePluginCollection> $availableOpensourcePluginRepository
34+ * @param EntityRepository<LanguageCollection> $languageRepository
35+ * @param EntityRepository<NotificationCollection> $notificationRepository
36+ */
2837 public function __construct (
2938 private EntityRepository $ availableOpensourcePluginRepository ,
3039 private IndexFileServiceInterface $ indexFileService ,
3140 private HttpClientInterface $ httpClient ,
3241 private CacheInterface $ cache ,
3342 private EntityRepository $ languageRepository ,
43+ private EntityRepository $ notificationRepository ,
3444 private string $ shopwareVersion ,
3545 ) {
3646 }
3747
38- public function execute (string |PackageIndexEntry $ packageInformation , \DateTimeInterface $ now ): void
48+ public function execute (string |PackageIndexEntry $ packageInformation , \DateTimeInterface $ now, Context $ context ): void
3949 {
4050 if (!$ packageInformation instanceof PackageIndexEntry) {
4151 $ packageInformation = $ this ->indexFileService ->getPackageInformation ($ packageInformation );
@@ -104,8 +114,10 @@ public function execute(string|PackageIndexEntry $packageInformation, \DateTimeI
104114
105115 /** @var AvailableOpensourcePluginEntity|null $plugin */
106116 $ plugin = $ this ->availableOpensourcePluginRepository ->search (
107- (new Criteria ())->addFilter (new EqualsFilter ('packageName ' , $ packageInformation ->packageName )),
108- Context::createDefaultContext ()
117+ (new Criteria ())
118+ ->addFilter (new EqualsFilter ('packageName ' , $ packageInformation ->packageName ))
119+ ->addAssociation ('plugin ' ),
120+ $ context
109121 )->first ();
110122
111123 $ id = $ plugin ?->getId() ?? Uuid::randomHex ();
@@ -114,7 +126,15 @@ public function execute(string|PackageIndexEntry $packageInformation, \DateTimeI
114126 $ pluginData ['lastSeenAt ' ] = $ now ;
115127 $ pluginData ['lastCommitTime ' ] = $ packageInformation ->latestCommitTime ;
116128
117- $ this ->availableOpensourcePluginRepository ->upsert ([$ pluginData ], Context::createDefaultContext ());
129+ $ this ->availableOpensourcePluginRepository ->upsert ([$ pluginData ], $ context );
130+
131+ if (!is_null ($ plugin )) {
132+ $ this ->handleUpdateNotification (
133+ $ plugin ,
134+ $ pluginData ,
135+ $ context
136+ );
137+ }
118138 }
119139
120140 private function fetchExtensionYmlData (string $ mainExtensionYmlUrl , string $ githubUrl ): array
@@ -227,4 +247,27 @@ private function loadLanguageId(string $locale): string
227247
228248 return CacheValueCompressor::uncompress ($ value );
229249 }
250+
251+ private function handleUpdateNotification (
252+ AvailableOpensourcePluginEntity $ plugin ,
253+ array $ pluginData ,
254+ Context $ context
255+ ): void {
256+ if (is_null ($ swPlugin = $ plugin ->getPlugin ()) || is_null ($ swPlugin ->getInstalledAt ())) {
257+ return ;
258+ }
259+
260+ if (version_compare ($ pluginData ['availableVersion ' ], $ swPlugin ->getVersion ()) !== 1 ) {
261+ return ;
262+ }
263+
264+ $ context ->scope (Context::SYSTEM_SCOPE , function (Context $ context ) use ($ swPlugin ): void {
265+ $ this ->notificationRepository ->create ([[
266+ 'id ' => Uuid::randomHex (),
267+ 'status ' => 'info ' ,
268+ 'message ' => 'Update available for plugin ' . $ swPlugin ->getName (),
269+ 'requiredPrivileges ' => [],
270+ ]], $ context );
271+ });
272+ }
230273}
0 commit comments