Passed
Push — trunk ( 7ae588...5f8f01 )
by Christian
13:45 queued 12s
created

UpdateSubscriber::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Administration\Notification\Subscriber;
4
5
use Shopware\Administration\Notification\NotificationService;
6
use Shopware\Core\Framework\Api\Context\AdminApiSource;
7
use Shopware\Core\Framework\Log\Package;
8
use Shopware\Core\Framework\Update\Event\UpdatePostFinishEvent;
9
use Shopware\Core\Framework\Uuid\Uuid;
10
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
11
12
/**
13
 * @internal
14
 */
15
#[Package('system-settings')]
16
class UpdateSubscriber implements EventSubscriberInterface
17
{
18
    /**
19
     * @internal
20
     */
21
    public function __construct(
22
        private readonly NotificationService $notificationService
23
    ) {
24
    }
25
26
    /**
27
     * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, string|arr...y{0: string, 1?: int}>> at position 17 could not be parsed: Expected '>' at position 17, but found 'list'.
Loading history...
28
     */
29
    public static function getSubscribedEvents(): array
30
    {
31
        return [
32
            UpdatePostFinishEvent::class => [
33
                ['updateFinishedDone', -9999],
34
            ],
35
        ];
36
    }
37
38
    /**
39
     * @internal
40
     */
41
    public function updateFinishedDone(UpdatePostFinishEvent $event): void
42
    {
43
        $status = 'success';
44
        $message = 'Updated successfully to version ' . $event->getNewVersion();
45
        if ($event->getPostUpdateMessage() !== '') {
46
            $status = 'warning';
47
            $message .= \PHP_EOL . $event->getPostUpdateMessage();
48
        }
49
50
        $source = $event->getContext()->getSource();
51
        $integrationId = null;
52
        $createdByUserId = null;
53
        if ($source instanceof AdminApiSource) {
54
            $integrationId = $source->getIntegrationId();
55
            $createdByUserId = $source->getUserId();
56
        }
57
58
        $this->notificationService->createNotification(
59
            [
60
                'id' => Uuid::randomHex(),
61
                'status' => $status,
62
                'message' => $message,
63
                'adminOnly' => true,
64
                'requiredPrivileges' => [],
65
                'createdByIntegrationId' => $integrationId,
66
                'createdByUserId' => $createdByUserId,
67
            ],
68
            $event->getContext()
69
        );
70
    }
71
}
72