• 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

15.22
/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\CoreBundle\Framework\ContaoFramework;
12
use Contao\Database;
13
use Contao\Model;
14
use Contao\Model\Collection;
15
use Contao\ModuleModel;
16
use Contao\Validator;
17
use Doctrine\DBAL\Connection;
18
use HeimrichHannot\UtilsBundle\Util\Utils;
19

20
class EntityFinderHelper
21
{
22
    public function __construct(
23
        private readonly Utils           $utils,
24
        private readonly ContaoFramework $framework,
25
        private readonly Connection      $connection,
26
    )
27
    {
28
    }
1✔
29

30
    /**
31
     * Search within serialized array fields of the model entity.
32
     *
33
     * @param string $type   Module type
34
     * @param string $field  Field with serialized data
35
     * @param array  $values Values to search for in serialized data field
36
     *
37
     * @throws \Exception
38
     */
39
    public function findModulesByTypeAndSerializedValue(string $type, string $field, array $values): ?Collection
40
    {
41
        $blobQuery = $this->utils->database()->createWhereForSerializedBlob(ModuleModel::getTable().'.'.$field, $values);
1✔
42
        $columns = [$blobQuery->createOrWhere()];
1✔
43
        $values = $blobQuery->values;
1✔
44

45
        $columns[] = ModuleModel::getTable().'.type=?';
1✔
46
        $values[] = $type;
1✔
47

48
        return $this->framework->getAdapter(ModuleModel::class)->findBy($columns, $values);
1✔
49
    }
50

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

77
        return $result->fetchEach('id');
×
78
    }
79

80

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

107
        return $result->fetchEach('id');
×
108
    }
109

110
    /**
111
     * @return object|Model|null
112
     * @throws \Doctrine\DBAL\Exception
113
     */
114
    public function fetchModelOrData(string $table, int|string $idOrAlias): ?object
115
    {
116
        /** @var class-string<Model> $modelClass */
NEW
117
        $modelClass = Model::getClassFromTable($table);
×
118

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

NEW
135
                $result = $this->connection->executeQuery("SELECT * FROM $table WHERE id=?", [(int)$idOrAlias]);
×
NEW
136
                if ($result->rowCount() === 0) {
×
NEW
137
                    return null;
×
138
                }
NEW
139
                $element = (object) $result->fetchAssociative();
×
140
                $element->getTable = function() use ($table) {
NEW
141
                    return $table;
×
142
                };
NEW
143
                return $element;
×
144
            }
145
        }
146

NEW
147
        return $modelClass::findByIdOrAlias($idOrAlias);
×
148
    }
149
}
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