Passed
Push — typo3v9 ( 2404ee...b9b5fa )
by Tomas Norre
05:51
created

ConfigurationRepository   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 71.05%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 60
ccs 27
cts 38
cp 0.7105
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getCrawlerConfigurationRecords() 0 14 2
A createQueryBuilder() 0 3 1
A getCrawlerConfigurationRecordsFromRootLine() 0 23 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AOE\Crawler\Domain\Repository;
6
7
/*
8
 * (c) 2020 AOE GmbH <[email protected]>
9
 *
10
 * This file is part of the TYPO3 Crawler Extension.
11
 *
12
 * It is free software; you can redistribute it and/or modify it under
13
 * the terms of the GNU General Public License, either version 2
14
 * of the License, or any later version.
15
 *
16
 * For the full copyright and license information, please read the
17
 * LICENSE.txt file that was distributed with this source code.
18
 *
19
 * The TYPO3 project - inspiring people to share!
20
 */
21
22
use TYPO3\CMS\Backend\Utility\BackendUtility;
23
use TYPO3\CMS\Core\Database\Connection;
24
use TYPO3\CMS\Core\Database\ConnectionPool;
25
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
26
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
27
use TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction;
28
use TYPO3\CMS\Core\Utility\GeneralUtility;
29
30
/**
31
 * Class ConfigurationRepository
32
 */
33
class ConfigurationRepository extends AbstractRepository
34
{
35
    /**
36
     * @var string
37
     */
38
    protected $tableName = 'tx_crawler_configuration';
39
40
    /**
41
     * @return array
42
     */
43 1
    public function getCrawlerConfigurationRecords(): array
44
    {
45 1
        $records = [];
46 1
        $queryBuilder = $this->createQueryBuilder();
47
        $statement = $queryBuilder
48 1
            ->select('*')
49 1
            ->from($this->tableName)
50 1
            ->execute();
51
52 1
        while ($row = $statement->fetch()) {
53 1
            $records[] = $row;
54
        }
55
56 1
        return $records;
57
    }
58
59
    /**
60
     * Traverses up the rootline of a page and fetches all crawler records.
61
     *
62
     * @param int $pageId
63
     * @return array
64
     */
65 2
    public function getCrawlerConfigurationRecordsFromRootLine(int $pageId): array
66
    {
67 2
        $pageIdsInRootLine = [];
68 2
        $rootLine = BackendUtility::BEgetRootLine($pageId);
69
70 2
        foreach ($rootLine as $pageInRootLine) {
71 2
            $pageIdsInRootLine[] = (int)$pageInRootLine['uid'];
72
        }
73
74 2
        $queryBuilder = $this->createQueryBuilder();
75
        $queryBuilder
76 2
            ->getRestrictions()->removeAll()
77 2
            ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
78 2
            ->add(GeneralUtility::makeInstance(HiddenRestriction::class));
79
        $configurationRecordsForCurrentPage = $queryBuilder
80 2
            ->select('*')
81 2
            ->from($this->tableName)
82 2
            ->where(
83 2
                $queryBuilder->expr()->in('pid', $queryBuilder->createNamedParameter($pageIdsInRootLine, Connection::PARAM_INT_ARRAY))
84
            )
85 2
            ->execute()
86 2
            ->fetchAll();
87 2
        return is_array($configurationRecordsForCurrentPage) ? $configurationRecordsForCurrentPage : [];
0 ignored issues
show
introduced by
The condition is_array($configurationRecordsForCurrentPage) is always true.
Loading history...
88
    }
89
90 3
    protected function createQueryBuilder(): QueryBuilder
91
    {
92 3
        return GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->tableName);
93
    }
94
}
95