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

heimrichhannot / contao-utils-bundle / 15017483614

14 May 2025 09:41AM UTC coverage: 25.302% (-54.2%) from 79.482%
15017483614

Pull #96

github

koertho
backport anonymize utils
Pull Request #96: Backport anonymize utils

6 of 7 new or added lines in 2 files covered. (85.71%)

248 existing lines in 22 files now uncovered.

1488 of 5881 relevant lines covered (25.3%)

1.56 hits per line

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

90.22
/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
    private EntityFinderHelper $helper;
22
    private EventDispatcherInterface $eventDispatcher;
23
    private ContaoFramework $framework;
24

25
    public function __construct(
26
        EntityFinderHelper $helper,
27
        EventDispatcherInterface $eventDispatcher,
28
        ContaoFramework $framework
29

30
    )
31
    {
32
        $this->helper = $helper;
6✔
33
        $this->eventDispatcher = $eventDispatcher;
6✔
34
        $this->framework = $framework;
6✔
35
    }
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✔
UNCOV
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, $dca): \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 form(int $id): ?Element
97
    {
98
        $model = $this->helper->fetchModelOrData('tl_form', $id);
1✔
99

100
        if (null === $model) {
1✔
101
            return null;
1✔
102
        }
103

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

125
    private function formField(int $id): ?Element
126
    {
127
        $model = $this->helper->fetchModelOrData('tl_form_field', $id);
1✔
128

129
        if (null === $model) {
1✔
130
            return null;
1✔
131
        }
132
        return new Element(
1✔
133
            $model->id,
1✔
134
            'tl_form_field',
1✔
135
            'Form field ' . $model->name . ' (ID: ' . $model->id . ')',
1✔
136
            (function () use ($model): \Generator {
1✔
137
                yield ['table' => FormModel::getTable(), 'id' => $model->pid];
1✔
138
            })()
1✔
139
        );
1✔
140
    }
141

142
    private function listConfig(int $id): ?Element
143
    {
144
        $model = $this->helper->fetchModelOrData('tl_list_config', $id);
1✔
145
        if ($model === null) {
1✔
146
            return null;
1✔
147
        }
148

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

163
    private function listConfigElement(int $id): ?Element
164
    {
165
        $model = $this->helper->fetchModelOrData('tl_list_config_element', $id);
1✔
166
        if (null === $model) {
1✔
167
            return null;
1✔
168
        }
169

170
        return new Element(
1✔
171
            $model->id,
1✔
172
            'tl_list_config_element',
1✔
173
            'List config element ' . $model->title. ' (ID: ' . $model->id . ')',
1✔
174
            (function() use ($model): \Iterator {
1✔
175
                yield ['table' => 'tl_list_config', 'id' => $model->pid];
1✔
176
            })()
1✔
177
        );
1✔
178
    }
179
}
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