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

heimrichhannot / contao-utils-bundle / 13942910598

19 Mar 2025 09:16AM UTC coverage: 73.034% (-0.7%) from 73.745%
13942910598

Pull #93

github

koertho
updated tests
Pull Request #93: Forwardport #87

85 of 140 new or added lines in 6 files covered. (60.71%)

2 existing lines in 1 file now uncovered.

1040 of 1424 relevant lines covered (73.03%)

3.13 hits per line

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

90.0
/src/EntityFinder/Finder.php
1
<?php
2

3
namespace HeimrichHannot\UtilsBundle\EntityFinder;
4

5
use Contao\ContentModel;
6
use Contao\Controller;
7
use Contao\CoreBundle\Framework\ContaoFramework;
8
use Contao\DC_Table;
9
use Contao\FormFieldModel;
10
use Contao\FormModel;
11
use Contao\ModuleModel;
12
use HeimrichHannot\UtilsBundle\Event\EntityFinderFindEvent;
13
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
14
use function Symfony\Component\String\u;
15

16
/**
17
 * @internal
18
 */
19
class Finder
20
{
21

22
    public function __construct(
23
        private readonly EntityFinderHelper       $helper,
24
        private readonly EventDispatcherInterface $eventDispatcher,
25
        private readonly ContaoFramework          $framework
26

27
    )
28
    {
29
    }
6✔
30

31
    public function find(string $table, int $id): ?Element
32
    {
33
        if (in_array($table, ['find', 'tl_find', 'fallback', 'tl_fallback'])) {
6✔
34
            return null;
1✔
35
        }
36

37
        $method = u(str_starts_with($table, 'tl_') ? substr($table, 3) : $table)->camel()->toString();
6✔
38

39
        if (method_exists($this, $method)) {
6✔
40
            return $this->$method($id);
3✔
41
        }
42

43
        $event = $this->eventDispatcher->dispatch(new EntityFinderFindEvent($table, $id));
3✔
44
        if ($element = $event->getElement()) {
3✔
NEW
45
            return $element;
×
46
        }
47

48
        return $this->fallback($table, $id);
3✔
49
    }
50

51
    private function fallback(string $table, $idOrAlias): ?Element
52
    {
53
        $model = $this->helper->fetchModelOrData($table, $idOrAlias);
3✔
54

55
        if (null === $model) {
3✔
56
            return null;
1✔
57
        }
58

59
        $elementData = [
2✔
60
            'id' => $model->id,
2✔
61
            'table' => $table,
2✔
62
            'description' => null,
2✔
63
            'parents' => null,
2✔
64
        ];
2✔
65

66
        $this->framework->getAdapter(Controller::class)->loadDataContainer($table);
2✔
67

68
        $dca = &$GLOBALS['TL_DCA'][$table];
2✔
69

70
        if (!empty($model->pid)) {
2✔
71
            if (isset($dca['config']['ptable'])) {
2✔
72
                $elementData['parents'] = (function () use ($model, $dca): \Iterator {
1✔
73
                    yield ['table' => $dca['config']['ptable'], 'id' => $model->pid];
1✔
74
                })();
1✔
75
            } elseif (isset($dca['config']['dynamicPtable']) && isset($dca['fields']['pid']) && $model->ptable) {
2✔
76
                $elementData['parents'] = (function () use ($model, $dca): \Iterator {
1✔
77
                    yield ['table' => $model->ptable, 'id' => $model->pid];
1✔
78
                })();
1✔
79
            }
80
        }
81

82
        return new Element(
2✔
83
            $elementData['id'],
2✔
84
            $elementData['table'],
2✔
85
            $elementData['description'],
2✔
86
            $elementData['parents']
2✔
87
        );
2✔
88
    }
89

90
    private function form(int $id): ?Element
91
    {
92
        $model = $this->helper->fetchModelOrData('tl_form', $id);
1✔
93

94
        if (null === $model) {
1✔
95
            return null;
1✔
96
        }
97

98
        return new Element(
1✔
99
            $model->id,
1✔
100
            'tl_form',
1✔
101
            'Form ' . $model->title . ' (ID: ' . $model->id . ')',
1✔
102
            (function () use ($model): \Iterator {
1✔
NEW
103
                foreach (ModuleModel::findByForm($model->id) as $model) {
×
NEW
104
                    yield ['table' => $model::getTable(), 'id' => $model->id];
×
105
                }
NEW
106
                foreach (ContentModel::findByForm($model->id) as $model) {
×
NEW
107
                    yield ['table' => $model::getTable(), 'id' => $model->id];
×
108
                }
NEW
109
                foreach ($this->helper->findModulesByInserttag('html', 'html', 'insert_form', $model->id) as $model) {
×
NEW
110
                    yield ['table' => $model::getTable(), 'id' => $model->id];
×
111
                }
NEW
112
                foreach ($this->helper->findContentElementByInserttag('html', 'html', 'insert_form', $model->id) as $model) {
×
NEW
113
                    yield ['table' => $model::getTable(), 'id' => $model->id];
×
114
                }
115
            })()
1✔
116
        );
1✔
117
    }
118

119
    private function formField(int $id): ?Element
120
    {
121
        $model = $this->helper->fetchModelOrData('tl_form_field', $id);
1✔
122

123
        if (null === $model) {
1✔
124
            return null;
1✔
125
        }
126
        return new Element(
1✔
127
            $model->id,
1✔
128
            'tl_form_field',
1✔
129
            'Form field ' . $model->name . ' (ID: ' . $model->id . ')',
1✔
130
            (function () use ($model): \Generator {
1✔
131
                yield ['table' => FormModel::getTable(), 'id' => $model->pid];
1✔
132
            })()
1✔
133
        );
1✔
134
    }
135

136
    private function listConfig(int $id): ?Element
137
    {
138
        $model = $this->helper->fetchModelOrData('tl_list_config', $id);
1✔
139
        if ($model === null) {
1✔
140
            return null;
1✔
141
        }
142

143
        return new Element(
1✔
144
            $model->id,
1✔
145
            'tl_list_config',
1✔
146
            'List config ' . $model->title . ' (ID: ' . $model->id . ')',
1✔
147
            (function () use ($model): \Iterator {
1✔
148
                $t = ModuleModel::getTable();
1✔
149
                $modules = $this->framework->getAdapter(ModuleModel::class)->findBy(["$t.type=?", "$t.listConfig=?"], ['listConfig', $model->id]);
1✔
150
                foreach ($modules as $module) {
1✔
151
                    yield ['table' => ModuleModel::getTable(), 'id' => $module->id];
1✔
152
                }
153
            })()
1✔
154
        );
1✔
155
    }
156

157
    private function listConfigElement(int $id): ?Element
158
    {
159
        $model = $this->helper->fetchModelOrData('tl_list_config_element', $id);
1✔
160
        if (null === $model) {
1✔
161
            return null;
1✔
162
        }
163

164
        return new Element(
1✔
165
            $model->id,
1✔
166
            'tl_list_config_element',
1✔
167
            'List config element ' . $model->title . ' (ID: ' . $model->id . ')',
1✔
168
            (function () use ($model): \Iterator {
1✔
169
                yield ['table' => 'tl_list_config', 'id' => $model->pid];
1✔
170
            })()
1✔
171
        );
1✔
172
    }
173
}
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