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

heimrichhannot / contao-utils-bundle / 13947257304

19 Mar 2025 01:05PM UTC coverage: 72.524% (-1.2%) from 73.745%
13947257304

Pull #93

github

koertho
update tests
Pull Request #93: Forwardport #87

85 of 229 new or added lines in 6 files covered. (37.12%)

2 existing lines in 1 file now uncovered.

1040 of 1434 relevant lines covered (72.52%)

3.11 hits per line

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

46.29
/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\FormModel;
9
use Contao\LayoutModel;
10
use Contao\ModuleModel;
11
use Contao\NewsArchiveModel;
12
use Contao\NewsModel;
13
use Contao\ThemeModel;
14
use Doctrine\DBAL\Connection;
15
use HeimrichHannot\Blocks\BlockModel;
16
use HeimrichHannot\Blocks\BlockModuleModel;
17
use HeimrichHannot\UtilsBundle\Event\EntityFinderFindEvent;
18
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
19
use function Symfony\Component\String\u;
20

21
/**
22
 * @internal
23
 */
24
class Finder
25
{
26

27
    public function __construct(
28
        private readonly EntityFinderHelper       $helper,
29
        private readonly EventDispatcherInterface $eventDispatcher,
30
        private readonly ContaoFramework          $framework,
31
        private readonly Connection $connection,
32

33
    )
34
    {
35
    }
6✔
36

37
    public function find(string $table, int $id): ?Element
38
    {
39
        if (in_array($table, ['find', 'tl_find', 'fallback', 'tl_fallback'])) {
6✔
40
            return null;
1✔
41
        }
42

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

45
        if (method_exists($this, $method)) {
6✔
46
            return $this->$method($id);
3✔
47
        }
48

49
        $event = $this->eventDispatcher->dispatch(new EntityFinderFindEvent($table, $id));
3✔
50
        if ($element = $event->getElement()) {
3✔
NEW
51
            return $element;
×
52
        }
53

54
        return $this->fallback($table, $id);
3✔
55
    }
56

57
    private function fallback(string $table, $idOrAlias): ?Element
58
    {
59
        $model = $this->helper->fetchModelOrData($table, $idOrAlias);
3✔
60

61
        if (null === $model) {
3✔
62
            return null;
1✔
63
        }
64

65
        $elementData = [
2✔
66
            'id' => $model->id,
2✔
67
            'table' => $table,
2✔
68
            'description' => null,
2✔
69
            'parents' => null,
2✔
70
        ];
2✔
71

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

74
        $dca = &$GLOBALS['TL_DCA'][$table];
2✔
75

76
        if (!empty($model->pid)) {
2✔
77
            if (isset($dca['config']['ptable'])) {
2✔
78
                $elementData['parents'] = (function () use ($model, $dca): \Iterator {
1✔
79
                    yield ['table' => $dca['config']['ptable'], 'id' => $model->pid];
1✔
80
                })();
1✔
81
            } elseif (isset($dca['config']['dynamicPtable']) && isset($dca['fields']['pid']) && $model->ptable) {
2✔
82
                $elementData['parents'] = (function () use ($model): \Iterator {
1✔
83
                    yield ['table' => $model->ptable, 'id' => $model->pid];
1✔
84
                })();
1✔
85
            }
86
        }
87

88
        return new Element(
2✔
89
            $elementData['id'],
2✔
90
            $elementData['table'],
2✔
91
            $elementData['description'],
2✔
92
            $elementData['parents']
2✔
93
        );
2✔
94
    }
95

96
    private function block(int $id): ?Element
97
    {
NEW
98
        if (!class_exists(BlockModel::class)) {
×
NEW
99
            return null;
×
100
        }
101

NEW
102
        $model = $this->helper->fetchModelOrData(BlockModel::getTable(), $id);
×
103

NEW
104
        if (null === $model) {
×
NEW
105
            return null;
×
106
        }
107

NEW
108
        return new Element(
×
NEW
109
            $model->id,
×
NEW
110
            BlockModel::getTable(),
×
NEW
111
            'Block ' . $model->title . ' (ID: ' . $model->id . ')',
×
NEW
112
            (function () use ($model): \Iterator {
×
NEW
113
                yield ['table' => ModuleModel::getTable(), 'id' => $model->module];
×
NEW
114
            })()
×
NEW
115
        );
×
116
    }
117

118
    private function blockModule(int $id): ?Element
119
    {
NEW
120
        if (!class_exists(BlockModuleModel::class)) {
×
NEW
121
            return null;
×
122
        }
123

NEW
124
        $model = $this->helper->fetchModelOrData(BlockModuleModel::getTable(), $id);
×
125

NEW
126
        if (null === $model) {
×
NEW
127
            return null;
×
128
        }
129

NEW
130
        return new Element(
×
NEW
131
            $model->id,
×
NEW
132
            BlockModuleModel::getTable(),
×
NEW
133
            'Block module ' . $model->title . ' (ID: ' . $model->id . ')',
×
NEW
134
            (function () use ($model): \Iterator {
×
135
                /* @phpstan-ignore class.notFound */
NEW
136
                yield ['table' => BlockModel::getTable(), 'id' => $model->pid];
×
NEW
137
            })()
×
NEW
138
        );
×
139
    }
140

141
    private function form(int $id): ?Element
142
    {
143
        $model = $this->helper->fetchModelOrData('tl_form', $id);
1✔
144

145
        if (null === $model) {
1✔
146
            return null;
1✔
147
        }
148

149
        return new Element(
1✔
150
            $model->id,
1✔
151
            'tl_form',
1✔
152
            'Form ' . $model->title . ' (ID: ' . $model->id . ')',
1✔
153
            (function () use ($model): \Iterator {
1✔
NEW
154
                foreach (ModuleModel::findByForm($model->id) as $model) {
×
NEW
155
                    yield ['table' => $model::getTable(), 'id' => $model->id];
×
156
                }
NEW
157
                foreach (ContentModel::findByForm($model->id) as $model) {
×
NEW
158
                    yield ['table' => $model::getTable(), 'id' => $model->id];
×
159
                }
NEW
160
                foreach ($this->helper->findModulesByInserttag('html', 'html', 'insert_form', $model->id) as $model) {
×
NEW
161
                    yield ['table' => $model::getTable(), 'id' => $model->id];
×
162
                }
NEW
163
                foreach ($this->helper->findContentElementByInserttag('html', 'html', 'insert_form', $model->id) as $model) {
×
NEW
164
                    yield ['table' => $model::getTable(), 'id' => $model->id];
×
165
                }
166
            })()
1✔
167
        );
1✔
168
    }
169

170
    private function formField(int $id): ?Element
171
    {
172
        $model = $this->helper->fetchModelOrData('tl_form_field', $id);
1✔
173

174
        if (null === $model) {
1✔
175
            return null;
1✔
176
        }
177
        return new Element(
1✔
178
            $model->id,
1✔
179
            'tl_form_field',
1✔
180
            'Form field ' . $model->name . ' (ID: ' . $model->id . ')',
1✔
181
            (function () use ($model): \Generator {
1✔
182
                yield ['table' => FormModel::getTable(), 'id' => $model->pid];
1✔
183
            })()
1✔
184
        );
1✔
185
    }
186

187
    private function module(int $id): ?Element
188
    {
NEW
189
        $model = $this->helper->fetchModelOrData(ModuleModel::getTable(), $id);
×
190

NEW
191
        if (null === $model) {
×
NEW
192
            return null;
×
193
        }
194

NEW
195
        return new Element(
×
NEW
196
            $model->id,
×
NEW
197
            ModuleModel::getTable(),
×
NEW
198
            'Module ' . $model->type . ' (ID: ' . $model->id . ')',
×
NEW
199
            (function () use ($model): \Generator {
×
200

NEW
201
                yield ['table' => ThemeModel::getTable(), 'id' => $model->pid];
×
202

NEW
203
                foreach (ContentModel::findBy(
×
NEW
204
                    ['tl_content.type=?', 'tl_content.module=?'],
×
NEW
205
                    ['module', $model->id]
×
NEW
206
                ) ?? [] as $contentelement) {
×
NEW
207
                    yield ['table' => ContentModel::getTable(), 'id' => $contentelement->id];
×
208
                }
209

NEW
210
                $result = $this->connection->executeQuery(
×
NEW
211
                    "SELECT id FROM tl_layout WHERE modules LIKE '%:\"".((int) $model->id)."\"%'"
×
NEW
212
                );
×
NEW
213
                foreach ($result->fetchAssociative() as $row) {
×
NEW
214
                    yield ['id' => $row['id'], 'table' => LayoutModel::getTable()];
×
215
                }
216

NEW
217
                foreach ($this->helper->findModulesByInserttag('html', 'html', 'insert_module', $model->id) as $id) {
×
NEW
218
                    yield ['id' => $id, 'table' => ModuleModel::getTable()];
×
219
                }
220

NEW
221
                if ( class_exists(BlockModuleModel::class) && $blockModules = BlockModuleModel::findByModule($model->id)) {
×
NEW
222
                    foreach ($blockModules as $blockModule) {
×
NEW
223
                        yield ['table' => BlockModuleModel::getTable(), 'id' => $blockModule->id];
×
224
                    }
225
                }
NEW
226
            })()
×
NEW
227
        );
×
228
    }
229

230
    private function news(int $id): ?Element
231
    {
NEW
232
        if (!class_exists(NewsModel::class)) {
×
NEW
233
            return null;
×
234
        }
235

NEW
236
        $model = $this->helper->fetchModelOrData(NewsModel::getTable(), $id);
×
237

NEW
238
        if (null === $model) {
×
NEW
239
            return null;
×
240
        }
241

NEW
242
        return new Element(
×
NEW
243
            $model->id,
×
NEW
244
            NewsModel::getTable(),
×
NEW
245
            'News ' . $model->headline . ' (ID: ' . $model->id . ')',
×
NEW
246
            (function () use ($model): \Generator {
×
247
                /* @phpstan-ignore class.notFound */
NEW
248
                yield ['table' => NewsArchiveModel::getTable(), 'id' => $model->pid];
×
NEW
249
            })()
×
NEW
250
        );
×
251
    }
252

253
    private function newsArchive(int $id): ?Element
254
    {
NEW
255
        if (!class_exists(NewsArchiveModel::class)) {
×
NEW
256
            return null;
×
257
        }
258

NEW
259
        $model = $this->helper->fetchModelOrData(NewsArchiveModel::getTable(), $id);
×
260

NEW
261
        if (null === $model) {
×
NEW
262
            return null;
×
263
        }
264

NEW
265
        return new Element(
×
NEW
266
            $model->id,
×
NEW
267
            NewsArchiveModel::getTable(),
×
NEW
268
            'News Archive ' . $model->title . ' (ID: ' . $model->id . ')',
×
NEW
269
            (function () use ($model): \Generator {
×
NEW
270
                $modules = $this->helper->findModulesByTypeAndSerializedValue(
×
NEW
271
                    'newslist',
×
NEW
272
                    'news_archives',
×
NEW
273
                    [$model->id]
×
NEW
274
                );
×
NEW
275
                if ($modules) {
×
NEW
276
                    foreach ($modules as $module) {
×
NEW
277
                        yield ['table' => ModuleModel::getTable(), 'id' => $module->id];
×
278
                    }
279
                }
NEW
280
            })()
×
NEW
281
        );
×
282
    }
283

284
    private function listConfig(int $id): ?Element
285
    {
286
        $model = $this->helper->fetchModelOrData('tl_list_config', $id);
1✔
287
        if ($model === null) {
1✔
288
            return null;
1✔
289
        }
290

291
        return new Element(
1✔
292
            $model->id,
1✔
293
            'tl_list_config',
1✔
294
            'List config ' . $model->title . ' (ID: ' . $model->id . ')',
1✔
295
            (function () use ($model): \Iterator {
1✔
296
                $t = ModuleModel::getTable();
1✔
297
                $modules = $this->framework->getAdapter(ModuleModel::class)->findBy(["$t.type=?", "$t.listConfig=?"], ['listConfig', $model->id]);
1✔
298
                foreach ($modules as $module) {
1✔
299
                    yield ['table' => ModuleModel::getTable(), 'id' => $module->id];
1✔
300
                }
301
            })()
1✔
302
        );
1✔
303
    }
304

305
    private function listConfigElement(int $id): ?Element
306
    {
307
        $model = $this->helper->fetchModelOrData('tl_list_config_element', $id);
1✔
308
        if (null === $model) {
1✔
309
            return null;
1✔
310
        }
311

312
        return new Element(
1✔
313
            $model->id,
1✔
314
            'tl_list_config_element',
1✔
315
            'List config element ' . $model->title . ' (ID: ' . $model->id . ')',
1✔
316
            (function () use ($model): \Iterator {
1✔
317
                yield ['table' => 'tl_list_config', 'id' => $model->pid];
1✔
318
            })()
1✔
319
        );
1✔
320
    }
321

322

323
}
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