Passed
Pull Request — main (#3451)
by Rafael
50:21 queued 10:58
created

findOneByRootPidAndOptionalIndexingConfigurationName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 33
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 2.0002

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 33
ccs 25
cts 26
cp 0.9615
rs 9.552
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.0002
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace ApacheSolrForTypo3\Solr\Domain\Index\Queue\Statistic;
19
20
use ApacheSolrForTypo3\Solr\System\Records\AbstractRepository;
21
use Doctrine\DBAL\Driver\Exception as DBALDriverException;
22
use Doctrine\DBAL\Exception as DBALException;
23
use PDO;
24
use TYPO3\CMS\Core\Utility\GeneralUtility;
25
use TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedMethodException;
26
27
/**
28
 * Class QueueStatisticsRepository
29
 */
30
class QueueStatisticsRepository extends AbstractRepository
31
{
32
    /**
33
     * @var string
34
     */
35
    protected string $table = 'tx_solr_indexqueue_item';
36
37
    /**
38
     * Extracts the number of pending, indexed and erroneous items from the
39
     * Index Queue.
40
     *
41
     * @param int $rootPid
42
     * @param string|null $indexingConfigurationName
43
     *
44
     * @return QueueStatistic
45
     *
46
     * @throws DBALDriverException
47
     * @throws DBALException
48
     */
49 5
    public function findOneByRootPidAndOptionalIndexingConfigurationName(
50
        int $rootPid,
51
        ?string $indexingConfigurationName = null
52
    ): QueueStatistic {
53 5
        $queryBuilder = $this->getQueryBuilder();
54 5
        $queryBuilder
55 5
            ->add('select', vsprintf('(%s < %s) AS %s', [
56 5
                $queryBuilder->quoteIdentifier('indexed'),
57 5
                $queryBuilder->quoteIdentifier('changed'),
58 5
                $queryBuilder->quoteIdentifier('pending'),
59 5
            ]), true)
60 5
            ->add('select', vsprintf('(%s) AS %s', [
61 5
                $queryBuilder->expr()->notLike('errors', $queryBuilder->createNamedParameter('')),
62 5
                $queryBuilder->quoteIdentifier('failed'),
63 5
            ]), true)
64 5
            ->add('select', $queryBuilder->expr()->count('*', 'count'), true)
65 5
            ->from($this->table)
66 5
            ->where(
67
                /** @scrutinizer ignore-type */
68 5
                $queryBuilder->expr()->eq('root', $queryBuilder->createNamedParameter($rootPid, PDO::PARAM_INT))
69 5
            )->groupBy('pending', 'failed');
70
71 5
        if (!empty($indexingConfigurationName)) {
72 1
            $queryBuilder->andWhere(
73
                /** @scrutinizer ignore-type */
74 1
                $queryBuilder->expr()->eq('indexing_configuration', $queryBuilder->createNamedParameter($indexingConfigurationName))
75 1
            );
76
        }
77
78 5
        return $this->buildQueueStatisticFromResultSet(
79 5
            $queryBuilder
80 5
                ->execute()
81 5
                ->fetchAllAssociative()
82 5
        );
83
    }
84
85
    /**
86
     * Instantiates and fills QueueStatistic with values
87
     *
88
     * @param array $indexQueueStatisticResultSet
89
     * @return QueueStatistic
90
     */
91 5
    protected function buildQueueStatisticFromResultSet(array $indexQueueStatisticResultSet): QueueStatistic
92
    {
93
        /* @var $statistic QueueStatistic */
94 5
        $statistic = GeneralUtility::makeInstance(QueueStatistic::class);
95 5
        foreach ($indexQueueStatisticResultSet as $row) {
96 5
            if ($row['failed'] == 1) {
97 2
                $statistic->setFailedCount((int)$row['count']);
98 5
            } elseif ($row['pending'] == 1) {
99 5
                $statistic->setPendingCount((int)$row['count']);
100
            } else {
101 3
                $statistic->setSuccessCount((int)$row['count']);
102
            }
103
        }
104
105 5
        return $statistic;
106
    }
107
108
    /**
109
     * Don't use this method.
110
     *
111
     * @return int
112
     * @throws UnsupportedMethodException
113
     */
114
    public function count(): int
115
    {
116
        throw new UnsupportedMethodException('Can not count the Index Queue Statistics.', 1504694750);
117
    }
118
}
119