Skip to content

Commit 7f7f1d7

Browse files
committed
Fix 16 DashboardImportance.php work per project
1 parent 37ff341 commit 7f7f1d7

File tree

6 files changed

+219
-85
lines changed

6 files changed

+219
-85
lines changed

src/EventSubscriber/Notifiers/DashboardNotifyListener.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ public function __construct(
2626

2727
public function __invoke(NotifyEvent $event): void {
2828
if ($event->notifier instanceof NotifierFavicon || $event->notifier instanceof NotifierSound) {
29-
$this->importance->upgradeHigher($event->notifier::class, $event->importance, $event->notifier);
29+
$this->importance->upgradeHigher($event->notifier::class, $event->project, $event->importance,
30+
$event->notifier);
3031
}
3132
}
3233

src/Service/DashboardImportance.php

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
namespace BugCatcher\Service;
99

10+
use BugCatcher\Entity\Project;
1011
use JetBrains\PhpStorm\ArrayShape;
1112
use BugCatcher\Entity\Notifier;
1213
use BugCatcher\Enum\Importance;
@@ -22,15 +23,17 @@ public function __construct(
2223
private readonly string $cacheDir
2324
) {}
2425

25-
public function upgradeHigher(string $group, Importance $importance, Notifier $notifier): void {
26-
$current = $this->importance[$group] ?? $this->load($group, $importance, $notifier);
26+
public function upgradeHigher(string $group, Project $project, Importance $importance, Notifier $notifier): void
27+
{
28+
$current = $this->importance[$group][$project->getId()->toString()] ??
29+
$this->load($group, $project, $importance, $notifier)[$project->getId()->toString()];
2730
if ($importance->isHigherThan($current["importance"])) {
2831
$current = [
2932
"importance" => $importance,
3033
"notifier" => $notifier,
3134
];
3235
}
33-
$this->importance[$group] = $current;
36+
$this->importance[$group][$project->getId()->toString()] = $current;
3437
}
3538

3639
public function save(string $group): void {
@@ -42,12 +45,30 @@ public function save(string $group): void {
4245
file_put_contents($this->cacheDir . "/importance-$group.txt", serialize($importance));
4346
}
4447

45-
#[ArrayShape(['importance' => "BugCatcher\Enum\Importance", 'notifier' => "BugCatcher\Entity\Notifier"])]
46-
public function load(string $group, $defaultImportance = null, $defaultNotifier = null): ?array
48+
#[ArrayShape([
49+
"string" => [
50+
'importance' => "BugCatcher\Enum\Importance",
51+
'notifier' => "BugCatcher\Entity\Notifier"
52+
]
53+
])]
54+
public function load(
55+
string $group,
56+
Project $defaultProject = null,
57+
$defaultImportance = null,
58+
$defaultNotifier = null
59+
): ?array
4760
{
4861
$group = substr(md5($group), 0, 8);
4962
if (!file_exists($this->cacheDir . "/importance-$group.txt")) {
50-
return ["importance" => $defaultImportance, "notifier" => $defaultNotifier];
63+
if ($defaultProject === null) {
64+
return [];
65+
}
66+
return [
67+
$defaultProject->getId()->toString() => [
68+
"importance" => $defaultImportance,
69+
"notifier" => $defaultNotifier
70+
]
71+
];
5172
}
5273

5374
return unserialize(file_get_contents($this->cacheDir . "/importance-$group.txt"));
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Jozef Môstka
5+
* Date: 4. 10. 2024
6+
* Time: 15:06
7+
*/
8+
9+
namespace BugCatcher\Twig\Components;
10+
11+
use BugCatcher\Entity\NotifierFavicon;
12+
use BugCatcher\Entity\User;
13+
use BugCatcher\Enum\Importance;
14+
use JetBrains\PhpStorm\ArrayShape;
15+
16+
trait DashboardImportance
17+
{
18+
#[ArrayShape(['importance' => "BugCatcher\Enum\Importance", 'notifier' => "BugCatcher\Entity\Notifier"])]
19+
public function getMaxImportance(): array
20+
{
21+
/** @var User|null $user */
22+
$user = $this->security->getUser();
23+
/** @var NotifierFavicon $notifier */
24+
$projects = $this->importance->load(NotifierFavicon::class);
25+
if ($user) {
26+
$tmpProjects = $projects;
27+
$projects = [];
28+
foreach ($tmpProjects as $projectId => $data) {
29+
foreach ($user->getProjects() as $project) {
30+
if ($project->getId()->toString() == $projectId) {
31+
$projects[$projectId] = $data;
32+
}
33+
}
34+
}
35+
}
36+
$maxImportance = Importance::min();
37+
$maxNotifier = null;
38+
foreach ($projects as [$importance, $notifier]) {
39+
if ($importance->isHigherThan($maxImportance)) {
40+
$maxImportance = $importance;
41+
$maxNotifier = $notifier;
42+
}
43+
}
44+
return [$maxImportance, $maxNotifier];
45+
}
46+
}

src/Twig/Components/Favicon.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,21 @@
77
*/
88
namespace BugCatcher\Twig\Components;
99

10-
use BugCatcher\Entity\NotifierFavicon;
11-
use BugCatcher\Entity\NotifierSound;
12-
use BugCatcher\Enum\Importance;
13-
use BugCatcher\Service\DashboardImportance;
10+
use Symfony\Bundle\SecurityBundle\Security;
1411
use Symfony\Component\Asset\Packages;
1512
use Symfony\Component\DependencyInjection\Attribute\Autowire;
1613
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
1714

1815
#[AsTwigComponent]
1916
final class Favicon
2017
{
21-
18+
use DashboardImportance;
2219
public string $id = "";
2320

2421
public function __construct(
25-
private readonly DashboardImportance $importance,
22+
private readonly \BugCatcher\Service\DashboardImportance $importance,
2623
private readonly Packages $assetManager,
24+
private readonly Security $security,
2725
#[Autowire(param: 'logo')]
2826
private readonly string $logo
2927
) {
@@ -32,12 +30,9 @@ public function __construct(
3230

3331

3432
public function getIcon(): string {
35-
/** @var NotifierFavicon $notifier */
36-
[$importance, $notifier] = array_values($this->importance->load(NotifierFavicon::class));
37-
$color = Importance::min()->getColor()->value;
38-
if ($importance) {
39-
$color = $importance->getColor()->value;
40-
}
33+
34+
[$importance, $notifier] = $this->getMaxImportance();
35+
$color = $importance->getColor()->value;
4136

4237
return $this->assetManager->getUrl("/assets/logo/{$this->logo}/icon-{$color}.svg", 'bug_catcher');
4338
}

src/Twig/Components/WarningSound.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,29 @@
99

1010
use BugCatcher\Entity\NotifierSound;
1111
use BugCatcher\Service\DashboardImportance;
12+
use Symfony\Bundle\SecurityBundle\Security;
1213
use Symfony\Component\Uid\Uuid;
1314
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
1415

1516
#[AsTwigComponent]
1617
final class WarningSound extends AbsComponent
1718
{
1819

20+
use \BugCatcher\Twig\Components\DashboardImportance;
1921
public string $id;
2022

2123
public function __construct(
22-
private readonly DashboardImportance $importance,
24+
private readonly DashboardImportance $importance,
25+
private readonly Security $security,
2326
) {
2427
$this->id = Uuid::v6()->toRfc4122();
2528
}
2629

2730

2831
public function getSound(): ?string {
2932
/** @var NotifierSound $notifier */
30-
[$importance, $notifier] = array_values($this->importance->load(NotifierSound::class));
31-
if (!$importance) {
33+
[$importance, $notifier] = $this->getMaxImportance();
34+
if (!$notifier) {
3235
return null;
3336
}
3437

0 commit comments

Comments
 (0)