Passed
Push — main ( 98505c...b09cff )
by
unknown
14:24 queued 11:28
created

FeatureFlagData   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 60
dl 0
loc 100
ccs 58
cts 58
cp 1
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getContentElements() 0 47 1
A updateContentElements() 0 15 2
A getContentElementsPIDs() 0 22 1
1
<?php
2
3
namespace Aoe\FeatureFlag\System\Db;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2021 AOE GmbH <[email protected]>
9
 *
10
 *  All rights reserved
11
 *
12
 *  This script is part of the TYPO3 project. The TYPO3 project is
13
 *  free software; you can redistribute it and/or modify
14
 *  it under the terms of the GNU General Public License as published by
15
 *  the Free Software Foundation; either version 3 of the License, or
16
 *  (at your option) any later version.
17
 *
18
 *  The GNU General Public License can be found at
19
 *  http://www.gnu.org/copyleft/gpl.html.
20
 *
21
 *  This script is distributed in the hope that it will be useful,
22
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 *  GNU General Public License for more details.
25
 *
26
 *  This copyright notice MUST APPEAR in all copies of the script!
27
 ***************************************************************/
28
29
use TYPO3\CMS\Core\Database\Connection;
30
use TYPO3\CMS\Core\Database\ConnectionPool;
31
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
32
use TYPO3\CMS\Core\Utility\GeneralUtility;
33
34
class FeatureFlagData
35
{
36
    /**
37
     * @var string
38
     */
39
    public const TABLE_MAPPING = 'tx_featureflag_domain_model_mapping';
40
41
    /**
42
     * @var string
43
     */
44
    public const TABLE_FLAGS = 'tx_featureflag_domain_model_featureflag';
45
46 1
    public function getContentElements(string $table, int $behavior, int $enabled): array
47
    {
48
        /** @var QueryBuilder $queryBuilder */
49 1
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
50 1
            ->getQueryBuilderForTable($table);
51
52 1
        $queryBuilder->getRestrictions()
53 1
            ->removeAll();
54
55
        $queryBuilder
56 1
            ->select($table . '.uid')
57 1
            ->from($table)
58 1
            ->from(self::TABLE_MAPPING)
59 1
            ->from(self::TABLE_FLAGS)
60 1
            ->where(
61 1
                $queryBuilder->expr()
62 1
                    ->andX(
63 1
                        $queryBuilder->expr()
64 1
                            ->eq(
65 1
                                self::TABLE_MAPPING . '.feature_flag',
66 1
                                self::TABLE_FLAGS . '.uid'
67
                            ),
68 1
                        $queryBuilder->expr()
69 1
                            ->eq(
70 1
                                $table . '.uid',
71 1
                                self::TABLE_MAPPING . '.foreign_table_uid'
72
                            ),
73 1
                        $queryBuilder->expr()
74 1
                            ->eq(
75 1
                                self::TABLE_FLAGS . '.enabled',
76 1
                                $queryBuilder->createNamedParameter($enabled, Connection::PARAM_INT)
77
                            ),
78 1
                        $queryBuilder->expr()
79 1
                            ->eq(
80 1
                                self::TABLE_MAPPING . '.foreign_table_name',
81 1
                                $queryBuilder->createNamedParameter($table, Connection::PARAM_STR)
82
                            ),
83 1
                        $queryBuilder->expr()
84 1
                            ->eq(
85 1
                                self::TABLE_MAPPING . '.behavior',
86 1
                                $queryBuilder->createNamedParameter($behavior, Connection::PARAM_INT)
87
                            )
88
                    )
89
            );
90
91 1
        return $queryBuilder->execute()
92 1
            ->fetchAllAssociative();
93
    }
94
95 1
    public function updateContentElements(string $table, array $uids, bool $isVisible): void
96
    {
97
        /** @var QueryBuilder $queryBuilder */
98 1
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
99 1
            ->getQueryBuilderForTable($table);
100
101 1
        $queryBuilder->getRestrictions()
102 1
            ->removeAll();
103
104
        $query = $queryBuilder
105 1
            ->update($table)
106 1
            ->set('hidden', $isVisible ? 0 : 1)
107 1
            ->add('where', $queryBuilder->expr()->in('uid', $uids));
108
109 1
        $query->execute();
110 1
    }
111
112 1
    public function getContentElementsPIDs(string $table, int $uid): string
113
    {
114
        /** @var QueryBuilder $queryBuilder */
115 1
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
116 1
            ->getQueryBuilderForTable($table);
117
118 1
        $queryBuilder->getRestrictions()
119 1
            ->removeAll();
120
121
        $query = $queryBuilder
122 1
            ->select($table . '.pid')
123 1
            ->from($table)
124 1
            ->where(
125 1
                $queryBuilder->expr()
0 ignored issues
show
Bug introduced by
$queryBuilder->expr()->e...Connection::PARAM_INT)) of type string is incompatible with the type Doctrine\DBAL\Query\Expr...on|array<integer,mixed> expected by parameter $predicates of TYPO3\CMS\Core\Database\...y\QueryBuilder::where(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

125
                /** @scrutinizer ignore-type */ $queryBuilder->expr()
Loading history...
126 1
                    ->eq(
127 1
                        $table . '.uid',
128 1
                        $queryBuilder->createNamedParameter($uid, Connection::PARAM_INT)
129
                    )
130
            );
131
132 1
        return $query->execute()
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->execute()->fetchOne() could return the type false which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
133 1
            ->fetchOne();
134
    }
135
}
136