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

heimrichhannot / contao-utils-bundle / 8111841604

01 Mar 2024 01:05PM UTC coverage: 72.668% (+0.09%) from 72.575%
8111841604

push

github

koertho
fix test

779 of 1072 relevant lines covered (72.67%)

3.5 hits per line

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

0.0
/src/EventListener/ExtendEntityFinderSubscriber.php
1
<?php
2

3
/*
4
 * Copyright (c) 2022 Heimrich & Hannot GmbH
5
 *
6
 * @license LGPL-3.0-or-later
7
 */
8

9
namespace HeimrichHannot\UtilsBundle\EventListener;
10

11
use Contao\Model\Collection;
12
use Contao\ModuleModel;
13
use Contao\NewsArchiveModel;
14
use Contao\NewsBundle\ContaoNewsBundle;
15
use Contao\NewsModel;
16
use HeimrichHannot\Blocks\BlockModel;
17
use HeimrichHannot\Blocks\BlockModuleModel;
18
use HeimrichHannot\UtilsBundle\Event\ExtendEntityFinderEvent;
19
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20

21
class ExtendEntityFinderSubscriber implements EventSubscriberInterface
22
{
23
    public static function getSubscribedEvents()
24
    {
25
        return [
×
26
            ExtendEntityFinderEvent::class => 'onExtendEntityFinderEvent',
×
27
        ];
×
28
    }
29

30
    public function onExtendEntityFinderEvent(ExtendEntityFinderEvent $event)
31
    {
32
        $this->findNewsEntity($event);
×
33
        $this->findBlockElements($event);
×
34
    }
35

36
    private function findNewsEntity(ExtendEntityFinderEvent $event): void
37
    {
38
        if (!class_exists(ContaoNewsBundle::class)) {
×
39
            return;
×
40
        }
41

42
        switch ($event->getTable()) {
×
43
            case NewsModel::getTable():
×
44
                $element = NewsModel::findByPk($event->getId());
×
45

46
                if (!$element) {
×
47
                    return;
×
48
                }
49

50
                $event->addParent(NewsArchiveModel::getTable(), $element->pid);
×
51
                $event->setOutput('News: '.$element->headline.' (ID: '.$element->id.')');
×
52

53
                break;
×
54

55
            case NewsArchiveModel::getTable():
×
56
                $element = NewsArchiveModel::findByPk($event->getId());
×
57

58
                if (!$element) {
×
59
                    return;
×
60
                }
61

62
                if (!$event->isOnlyText()) {
×
63
                    if ($modules = $event->getEntityFinderHelper()->findModulesByTypeAndSerializedValue('newslist', 'news_archives', [$element->id])) {
×
64
                        while ($modules->next()) {
×
65
                            $event->addParent(ModuleModel::getTable(), $modules->id);
×
66
                        }
67
                    }
68
                }
69

70
                $event->setOutput('News Archive: '.$element->title.' (ID: '.$element->id.')');
×
71

72
                break;
×
73
        }
74
    }
75

76
    private function findBlockElements(ExtendEntityFinderEvent $event): void
77
    {
78
        if (!class_exists(BlockModel::class)) {
×
79
            return;
×
80
        }
81

82
        switch ($event->getTable()) {
×
83
            case BlockModuleModel::getTable():
×
84
                $element = BlockModuleModel::findByPk($event->getId());
×
85

86
                if (!$element) {
×
87
                    return;
×
88
                }
89
                $event->addParent(BlockModel::getTable(), $element->pid);
×
90
                $event->setOutput('Block module: '.$element->title.' (ID: '.$element->id.')');
×
91

92
                break;
×
93

94
            case BlockModel::getTable():
×
95
                $block = BlockModel::findByPk($event->getId());
×
96

97
                if ($block) {
×
98
                    $event->addParent(ModuleModel::getTable(), $block->module);
×
99
                    $event->setOutput('Block: '.$block->title.' (ID: '.$block->id.')');
×
100
                }
101

102
                break;
×
103

104
            case ModuleModel::getTable():
×
105
                if ($event->isOnlyText()) {
×
106
                    break;
×
107
                }
108

109
                if (is_numeric($event->getId())) {
×
110
                    /** @var BlockModuleModel[]|Collection|null $blockModules */
111
                    $blockModules = BlockModuleModel::findByModule($event->getId());
×
112

113
                    if ($blockModules) {
×
114
                        foreach ($blockModules as $blockModule) {
×
115
                            $event->addParent(BlockModuleModel::getTable(), $blockModule->id);
×
116
                        }
117
                    }
118
                }
119

120
                break;
×
121
        }
122
    }
123
}
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

© 2025 Coveralls, Inc