• 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

14.58
/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
     * @throws \Doctrine\DBAL\Exception
112
     */
113
    public function fetchModelOrData(string $table, int|string $idOrAlias): ?Model
114
    {
115
        /** @var class-string<Model> $modelClass */
NEW
116
        $modelClass = Model::getClassFromTable($table);
×
117

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

NEW
134
                $result = $this->connection->executeQuery("SELECT * FROM $table WHERE id=?", [(int)$idOrAlias]);
×
NEW
135
                if ($result->rowCount() === 0) {
×
NEW
136
                    return null;
×
137
                }
NEW
138
                return $this->anonymousModel($table, $result->fetchAssociative());
×
139
            }
140
        }
141

NEW
142
        return $modelClass::findByIdOrAlias($idOrAlias);
×
143
    }
144

145
    private function anonymousModel(string $table, array $data): Model
146
    {
NEW
147
        return new class($table, $data) extends Model {
×
148

149
            protected $blnPreventSaving = true;
150

151
            public function __construct(string $table, array $data = [])
152
            {
NEW
153
                $this->strTable = $table;
×
NEW
154
                $this->setRow($data);
×
155
            }
NEW
156
        };
×
157
    }
158
}
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