• 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

0.0
/src/EntityFinder/EntityFinderHelper.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\EntityFinder;
10

11
use Contao\ContentModel;
12
use Contao\Database;
13
use Contao\Model;
14
use Contao\Model\Collection;
15
use Contao\ModuleModel;
16
use Contao\StringUtil;
17
use Contao\Validator;
18
use Doctrine\DBAL\Connection;
19
use HeimrichHannot\UtilsBundle\Database\DatabaseUtil;
20
use HeimrichHannot\UtilsBundle\Util\Utils;
21

22
class EntityFinderHelper
23
{
24
    private Utils $utils;
25
    private Connection $connection;
26

27
    public function __construct(
28
        Utils $utils,
29
        Connection $connection
30
    )
31
    {
UNCOV
32
        $this->utils = $utils;
×
UNCOV
33
        $this->connection = $connection;
×
34
    }
35

36
    /**
37
     * Search within serialized array fields of the model entity.
38
     *
39
     * @param string $type   Module type
40
     * @param string $field  Field with serialized data
41
     * @param array  $values Values to search for in serialized data field
42
     *
43
     * @throws \Exception
44
     */
45
    public function findModulesByTypeAndSerializedValue(string $type, string $field, array $values): ?Collection
46
    {
UNCOV
47
        $result = $this->utils->database()->createWhereForSerializedBlob(ModuleModel::getTable().'.'.$field, $values);
×
UNCOV
48
        $columns = [$result->createAndWhere(), ModuleModel::getTable().'.type=?'];
×
UNCOV
49
        $values = [$result->values, $type];
×
50

UNCOV
51
        return ModuleModel::findBy($columns, $values);
×
52
    }
53

54
    /**
55
     * Find frontend modules by insert inserttags like insert_module oder insert_article.
56
     *
57
     * @param string $type The module type
58
     * @param string $field The tl_module field
59
     * @param string $inserttag The inserttag to search for, for example insert_module
60
     * @param int $id The element id to search for, for example the module id (as used in {{insert_module::1}}, would be 1 in this case)
61
     * @return array The found module ids
62
     * @throws \Exception
63
     */
64
    public function findModulesByInserttag(string $type, string $field, string $inserttag, int $id): array
65
    {
66
        if (!Validator::isAlias($field)) {
×
UNCOV
67
            throw new \Exception('Invalid field name '.$field.'given.');
×
68
        }
69
        if (!Validator::isAlias($inserttag)) {
×
UNCOV
70
            throw new \Exception('Invalid inserttag '.$inserttag.'given.');
×
71
        }
72
        $result = Database::getInstance()
×
UNCOV
73
            ->prepare("SELECT id FROM tl_module
×
74
                        WHERE type=?
75
                        AND (
76
                            $field LIKE '%{{".$inserttag."::".$id."}}%'
×
77
                            OR $field LIKE '%{{".$inserttag."::".$id."::%')")
×
UNCOV
78
            ->execute($type);
×
79

UNCOV
80
        return $result->fetchEach('id');
×
81
    }
82

83

84
    /**
85
     * Find content elements by insert inserttags like insert_module oder insert_article.
86
     *
87
     * @param string $type The element type
88
     * @param string $field The tl_content field
89
     * @param string $inserttag The inserttag to search for, for example insert_module
90
     * @param int $id The element id to search for, for example the module id (as used in {{insert_module::1}}, would be 1 in this case)
91
     * @return array The found content element ids
92
     * @throws \Exception
93
     */
94
    public function findContentElementByInserttag(string $type, string $field, string $inserttag, int $id): array
95
    {
96
        if (!Validator::isAlias($field)) {
×
UNCOV
97
            throw new \Exception('Invalid field name '.$field.'given.');
×
98
        }
99
        if (!Validator::isAlias($inserttag)) {
×
UNCOV
100
            throw new \Exception('Invalid inserttag '.$inserttag.'given.');
×
101
        }
102
        $result = Database::getInstance()
×
UNCOV
103
            ->prepare("SELECT id FROM tl_content
×
104
                        WHERE type=?
105
                        AND (
106
                            $field LIKE '%{{".$inserttag."::".$id."}}%'
×
107
                            OR $field LIKE '%{{".$inserttag."::".$id."::%')")
×
UNCOV
108
            ->execute($type);
×
109

UNCOV
110
        return $result->fetchEach('id');
×
111
    }
112

113
    /**
114
     * @param string $table
115
     * @param int|string $idOrAlias
116
     * @return object|Model|null
117
     * @throws \Doctrine\DBAL\Exception
118
     */
119
    public function fetchModelOrData(string $table, $idOrAlias): ?object
120
    {
121
        /** @var class-string<Model> $modelClass */
UNCOV
122
        $modelClass = Model::getClassFromTable($table);
×
123

UNCOV
124
        if (!$modelClass || !class_exists($modelClass)) {
×
UNCOV
125
            if (!$this->connection->createSchemaManager()->tablesExist([$table])) {
×
UNCOV
126
                return null;
×
127
            }
UNCOV
128
            if (is_string($idOrAlias)) {
×
UNCOV
129
                $result = $this->connection->executeQuery("SELECT * FROM $table WHERE alias=?", [$idOrAlias]);
×
UNCOV
130
                if ($result->rowCount() === 0) {
×
UNCOV
131
                    return null;
×
132
                }
133
                return (object) $result->fetchAssociative();
×
134
            }
UNCOV
135
            if (is_numeric($idOrAlias)) {
×
UNCOV
136
                if ($idOrAlias != (int) $idOrAlias) {
×
UNCOV
137
                    return null;
×
138
                }
139

UNCOV
140
                $result = $this->connection->executeQuery("SELECT * FROM $table WHERE id=?", [(int)$idOrAlias]);
×
UNCOV
141
                if ($result->rowCount() === 0) {
×
UNCOV
142
                    return null;
×
143
                }
UNCOV
144
                $element = (object) $result->fetchAssociative();
×
145
                $element->getTable = function() use ($table) {
UNCOV
146
                    return $table;
×
147
                };
UNCOV
148
                return $element;
×
149
            }
150
        }
151

UNCOV
152
        return $modelClass::findByIdOrAlias($idOrAlias);
×
153
    }
154
}
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