• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

php-bug-catcher / bug-catcher / 27812067434

19 Jun 2026 07:31AM UTC coverage: 84.561% (-1.2%) from 85.729%
27812067434

push

github

tito10047
Refactor `NotifyCalculateListener` to use project mapping for improved performance and clarity

25 of 27 new or added lines in 1 file covered. (92.59%)

8 existing lines in 1 file now uncovered.

838 of 991 relevant lines covered (84.56%)

12.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.15
/src/EventSubscriber/NotifyCalculateListener.php
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Jozef Môstka
5
 * Date: 26. 7. 2024
6
 * Time: 16:08
7
 */
8
namespace BugCatcher\EventSubscriber;
9

10
use BugCatcher\DTO\NotifierStatus;
11
use BugCatcher\Entity\Project;
12
use BugCatcher\Enum\Importance;
13
use BugCatcher\Event\NotifyCalculateEvent;
14
use BugCatcher\Repository\RecordRepository;
15
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
16

17
#[AsEventListener]
18
final class NotifyCalculateListener
19
{
20

21

22
        public function __construct(
23
                private readonly RecordRepository $recordRepo,
24
        ) {}
24✔
25

26
        public function __invoke(NotifyCalculateEvent $event): void {
27

28

29
                $projects = array_filter($event->notifier->getProjects()->toArray(), fn(Project $p) => $p->isEnabled());
24✔
30
                $projects = array_map(fn(Project $p) => $p->getId()->toBinary(), $projects);
24✔
31

32
                switch ($event->notifier->getComponent()) {
24✔
33
                        case 'project-error-count':
24✔
34
                                $this->calculateProjectErrors($event, $projects);
6✔
35
                                break;
6✔
36
                        case 'same-error-count':
18✔
37
                                $this->calculateSameErrors($event, $projects);
18✔
38
                                break;
18✔
39
                }
40
        }
41

42
        /** @return array<string, Project> keyed by binary UUID */
43
        private function buildProjectMap(NotifyCalculateEvent $event): array {
44
                $map = [];
24✔
45
                foreach ($event->notifier->getProjects() as $p) {
24✔
46
                        if ($p->isEnabled()) {
24✔
47
                                $map[$p->getId()->toBinary()] = $p;
24✔
48
                        }
49
                }
50
                return $map;
24✔
51
        }
52

53
        private function calculateProjectErrors(NotifyCalculateEvent $event, array $projects): void {
54
                $rows = $this->recordRepo->createQueryBuilder("record")
6✔
55
                        ->select("IDENTITY(record.project) as projectId, COUNT(record.id) as count")
6✔
56
                        ->where("record.status = :status")
6✔
57
                        ->andWhere("record.project IN (:projects)")
6✔
58
                        ->setParameter("status", 'new')
6✔
59
                        ->setParameter('projects', $projects)
6✔
60
                        ->groupBy("record.project")
6✔
61
                        ->getQuery()->enableResultCache(10)->getResult();
6✔
62

63
                $projectMap = $this->buildProjectMap($event);
6✔
64
                foreach ($rows as $row) {
6✔
65
                        $project = $projectMap[$row['projectId']] ?? null;
6✔
66
                        if (!$project) {
6✔
NEW
67
                                continue;
×
68
                        }
69
                        $status = new NotifierStatus($project);
6✔
70
                        $event->addStatus($status);
6✔
71
                        $status->incrementImportance(Importance::Normal, $row['count'], $event->notifier->getThreshold());
6✔
72
                }
73
        }
74

75
        private function calculateSameErrors(NotifyCalculateEvent $event, array $projects): void {
76
                $rows = $this->recordRepo->createQueryBuilder("record")
18✔
77
                        ->select("IDENTITY(record.project) as projectId, COUNT(record.id) as count")
18✔
78
                        ->where("record.status = :status")
18✔
79
                        ->andWhere("record.project IN (:projects)")
18✔
80
                        ->setParameter("status", 'new')
18✔
81
                        ->setParameter('projects', $projects)
18✔
82
                        ->groupBy("record.project, record.hash")
18✔
83
                        ->getQuery()->enableResultCache(10)->getResult();
18✔
84

85
                $projectMap = $this->buildProjectMap($event);
18✔
86
                $statuses   = [];
18✔
87
                foreach ($rows as $row) {
18✔
88
                        $project = $projectMap[$row['projectId']] ?? null;
18✔
89
                        if (!$project) {
18✔
NEW
90
                                continue;
×
91
                        }
92
                        $key = $row['projectId'];
18✔
93
                        $status = $statuses[$key] ?? null;
18✔
94
                        if (!$status) {
18✔
95
                                $status = new NotifierStatus($project);
18✔
96
                                $statuses[$key] = $status;
18✔
97
                                $event->addStatus($status);
18✔
98
                        }
99
                        $status->incrementImportance(Importance::High, $row['count'], $event->notifier->getThreshold());
18✔
100
                }
101
        }
102

103

104
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc