Failed Conditions
Push — task/2976_TYPO3.11_compatibili... ( 14c9f4...2d3a36 )
by Rafael
23:17
created

findOneByRootPidAndOptionalIndexingConfigurationName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 2.0004

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 30
ccs 20
cts 21
cp 0.9524
rs 9.6
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.0004
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApacheSolrForTypo3\Solr\Domain\Index\Queue\Statistic;
6
7
/*
8
 * This file is part of the TYPO3 CMS project.
9
 *
10
 * It is free software; you can redistribute it and/or modify it under
11
 * the terms of the GNU General Public License, either version 2
12
 * of the License, or any later version.
13
 *
14
 * For the full copyright and license information, please read the
15
 * LICENSE.txt file that was distributed with this source code.
16
 *
17
 * The TYPO3 project - inspiring people to share!
18
 */
19
20
use ApacheSolrForTypo3\Solr\System\Records\AbstractRepository;
21
use Doctrine\DBAL\Exception as DBALException;
22
use Doctrine\DBAL\Driver\Exception as DBALDriverException;
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 $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 null $indexingConfigurationName
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $indexingConfigurationName is correct as it would always require null to be passed?
Loading history...
43
     *
44
     * @return QueueStatistic
45
     *
46
     * @throws DBALException
47
     * @throws DBALDriverException
48
     */
49 5
    public function findOneByRootPidAndOptionalIndexingConfigurationName(
50
        int $rootPid,
51
        $indexingConfigurationName = null
52
    ): QueueStatistic {
53 5
        $queryBuilder = $this->getQueryBuilder();
54
        $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
            );
76
        }
77
78 5
        return $this->buildQueueStatisticFromResultSet($queryBuilder->execute()->fetchAllAssociative());
79
    }
80
81
    /**
82
     * Instantiates and fills QueueStatistic with values
83
     *
84
     * @param array $indexQueueStatisticResultSet
85
     * @return QueueStatistic
86
     */
87 5
    protected function buildQueueStatisticFromResultSet(array $indexQueueStatisticResultSet): QueueStatistic
88
    {
89
        /* @var $statistic QueueStatistic */
90 5
        $statistic = GeneralUtility::makeInstance(QueueStatistic::class);
91 5
        foreach ($indexQueueStatisticResultSet as $row) {
92 5
            if ($row['failed'] == 1) {
93 2
                $statistic->setFailedCount((int)$row['count']);
94 5
            } elseif ($row['pending'] == 1) {
95 5
                $statistic->setPendingCount((int)$row['count']);
96
            } else {
97 3
                $statistic->setSuccessCount((int)$row['count']);
98
            }
99
        }
100
101 5
        return $statistic;
102
    }
103
104
    /**
105
     * Don't use this method.
106
     *
107
     * @return int
108
     * @throws UnsupportedMethodException
109
     */
110
    public function count(): int
111
    {
112
        throw new UnsupportedMethodException('Can not count the Index Queue Statistics.', 1504694750);
113
    }
114
}
115