Completed
Push — typo3v9 ( aea555...37a7d2 )
by Tomas Norre
06:20
created

ConfigurationRepository::createQueryBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AOE\Crawler\Domain\Repository;
6
7
/***************************************************************
8
 *  Copyright notice
9
 *
10
 *  (c) 2018 AOE GmbH <[email protected]>
11
 *
12
 *  All rights reserved
13
 *
14
 *  This script is part of the TYPO3 project. The TYPO3 project is
15
 *  free software; you can redistribute it and/or modify
16
 *  it under the terms of the GNU General Public License as published by
17
 *  the Free Software Foundation; either version 3 of the License, or
18
 *  (at your option) any later version.
19
 *
20
 *  The GNU General Public License can be found at
21
 *  http://www.gnu.org/copyleft/gpl.html.
22
 *
23
 *  This script is distributed in the hope that it will be useful,
24
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
 *  GNU General Public License for more details.
27
 *
28
 *  This copyright notice MUST APPEAR in all copies of the script!
29
 ***************************************************************/
30
31
use TYPO3\CMS\Backend\Utility\BackendUtility;
32
use TYPO3\CMS\Core\Database\Connection;
33
use TYPO3\CMS\Core\Database\ConnectionPool;
34
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
35
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
36
use TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction;
37
use TYPO3\CMS\Core\Utility\GeneralUtility;
38
39
/**
40
 * Class ConfigurationRepository
41
 */
42
class ConfigurationRepository extends AbstractRepository
43
{
44
    /**
45
     * @var string
46
     */
47
    protected $tableName = 'tx_crawler_configuration';
48
49
    /**
50
     * @return array
51
     */
52
    public function getCrawlerConfigurationRecords(): array
53
    {
54
        $records = [];
55
        $queryBuilder = $this->createQueryBuilder();
56
        $statement = $queryBuilder
57
            ->select('*')
58
            ->from($this->tableName)
59
            ->execute();
60
61
        while ($row = $statement->fetch()) {
62
            $records[] = $row;
63
        }
64
65
        return $records;
66
    }
67
68
    /**
69
     * Traverses up the rootline of a page and fetches all crawler records.
70
     *
71
     * @param int $pageId
72
     * @return array
73
     */
74
    public function getCrawlerConfigurationRecordsFromRootLine(int $pageId): array
75
    {
76
        $pageIdsInRootLine = [];
77
        $rootLine = BackendUtility::BEgetRootLine($pageId);
78
79
        foreach ($rootLine as $pageInRootLine) {
80
            $pageIdsInRootLine[] = (int)$pageInRootLine['uid'];
81
        }
82
83
        $queryBuilder = $this->createQueryBuilder();
84
        $queryBuilder
85
            ->getRestrictions()->removeAll()
86
            ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
87
            ->add(GeneralUtility::makeInstance(HiddenRestriction::class));
88
        $configurationRecordsForCurrentPage = $queryBuilder
89
            ->select('*')
90
            ->from($this->tableName)
91
            ->where(
92
                $queryBuilder->expr()->in('pid', $queryBuilder->createNamedParameter($pageIdsInRootLine, Connection::PARAM_INT_ARRAY))
93
            )
94
            ->execute()
95
            ->fetchAll();
96
        return is_array($configurationRecordsForCurrentPage) ? $configurationRecordsForCurrentPage : [];
97
    }
98
99
    protected function createQueryBuilder(): QueryBuilder
100
    {
101
        return GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
102
    }
103
}
104