Skip to content

Commit 2d1022b

Browse files
committed
feat(notification): add notification when update is available for installed plugin
implements #9
1 parent 72cd84f commit 2d1022b

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

src/Action/LoadPluginAction.php

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@
55
namespace NuonicPluginInstaller\Action;
66

77
use Composer\Semver\Semver;
8+
use NuonicPluginInstaller\Core\Framework\Plugin\AvailableOpensourcePlugin\AvailableOpensourcePluginCollection;
89
use NuonicPluginInstaller\Core\Framework\Plugin\AvailableOpensourcePlugin\AvailableOpensourcePluginEntity;
910
use NuonicPluginInstaller\Service\IndexFileServiceInterface;
1011
use NuonicPluginInstaller\Struct\PackageIndexEntry;
12+
use Shopware\Administration\Notification\NotificationCollection;
1113
use Shopware\Core\Framework\Adapter\Cache\CacheValueCompressor;
1214
use Shopware\Core\Framework\Context;
1315
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
1416
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
1517
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
1618
use Shopware\Core\Framework\Uuid\Uuid;
19+
use Shopware\Core\System\Language\LanguageCollection;
1720
use Symfony\Component\Yaml\Yaml;
1821
use Symfony\Contracts\Cache\CacheInterface;
1922
use Symfony\Contracts\Cache\ItemInterface;
@@ -25,17 +28,24 @@
2528

2629
readonly 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
}

src/Action/RefreshAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function execute(Context $context, bool $async = true): void
3939
continue;
4040
}
4141

42-
$this->loadPluginAction->execute($package, new \DateTime());
42+
$this->loadPluginAction->execute($package, new \DateTime(), $context);
4343
}
4444
}
4545
}

src/Infrastructure/Handler/LoadSinglePluginInfoHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use NuonicPluginInstaller\Action\LoadPluginAction;
88
use NuonicPluginInstaller\Infrastructure\Message\LoadSinglePluginInfoMessage;
9+
use Shopware\Core\Framework\Context;
910
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
1011

1112
#[AsMessageHandler(handles: LoadSinglePluginInfoMessage::class)]
@@ -18,6 +19,6 @@ public function __construct(
1819

1920
public function __invoke(LoadSinglePluginInfoMessage $message): void
2021
{
21-
$this->loadPluginAction->execute($message->package, $message->now);
22+
$this->loadPluginAction->execute($message->package, $message->now, Context::createCLIContext());
2223
}
2324
}

src/Resources/config/services.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<argument type="service" id="http_client" />
2727
<argument type="service" id="cache.app"/>
2828
<argument type="service" id="language.repository"/>
29+
<argument type="service" id="notification.repository" />
2930
<argument>%kernel.shopware_version%</argument>
3031
</service>
3132

0 commit comments

Comments
 (0)