Passed
Push — master ( 5f60a5...391298 )
by Timo
24:49
created

findOneByRootPidAndOptionalIndexingConfigurationName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 2.0116

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 18
cts 21
cp 0.8571
rs 9.0856
c 0
b 0
f 0
cc 2
eloc 18
nc 2
nop 2
crap 2.0116
1
<?php declare(strict_types = 1);
2
namespace ApacheSolrForTypo3\Solr\Domain\Index\Queue\Statistic;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2010-2017 dkd Internet Service GmbH <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 3 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use ApacheSolrForTypo3\Solr\System\Records\AbstractRepository;
28
use TYPO3\CMS\Core\Utility\GeneralUtility;
29
use TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedMethodException;
30
31
/**
32
 * Class QueueStatisticsRepository
33
 */
34
class QueueStatisticsRepository extends AbstractRepository
35
{
36
    /**
37
     * @var string
38
     */
39
    protected $table = 'tx_solr_indexqueue_item';
40
41
    /**
42
     * Extracts the number of pending, indexed and erroneous items from the
43
     * Index Queue.
44
     *
45
     * @param int $rootPid
46
     * @param string $indexingConfigurationName
47
     *
48
     * @return QueueStatistic
49
     */
50 6
    public function findOneByRootPidAndOptionalIndexingConfigurationName(int $rootPid, $indexingConfigurationName = null): QueueStatistic
51
    {
52 6
        $queryBuilder = $this->getQueryBuilder();
53
        $queryBuilder
54 6
            ->add('select', vsprintf('(%s < %s) AS %s', [
55 6
                $queryBuilder->quoteIdentifier('indexed'),
56 6
                $queryBuilder->quoteIdentifier('changed'),
57 6
                $queryBuilder->quoteIdentifier('pending')
58 6
            ]), true)
59 6
            ->add('select', vsprintf('(%s) AS %s', [
60 6
                $queryBuilder->expr()->notLike('errors', '""'),
61 6
                $queryBuilder->quoteIdentifier('failed')
62 6
            ]), true)
63 6
            ->add('select', $queryBuilder->expr()->count('*', 'count'), true)
64 6
            ->from($this->table)
65 6
            ->where($queryBuilder->expr()->eq('root', $queryBuilder->createNamedParameter($rootPid, \PDO::PARAM_INT)))
66 6
            ->groupBy('pending', 'failed');
67
68 6
        if (!empty($indexingConfigurationName)) {
69 1
            $queryBuilder->andWhere($queryBuilder->expr()->eq('indexing_configuration', $queryBuilder->createNamedParameter($indexingConfigurationName)));
70
        }
71
72 6
        return $this->buildQueueStatisticFromResultSet($queryBuilder->execute()->fetchAll());
73
    }
74
75
    /**
76
     * Instantiates and fills QueueStatistic with values
77
     *
78
     * @param array $indexQueueStatisticResultSet
79
     * @return QueueStatistic
80
     */
81 6
    protected function buildQueueStatisticFromResultSet(array $indexQueueStatisticResultSet): QueueStatistic
82
    {
83
        /* @var $statistic QueueStatistic */
84 6
        $statistic = GeneralUtility::makeInstance(QueueStatistic::class);
85 6
        foreach ($indexQueueStatisticResultSet as $row) {
86 6
            if ($row['failed'] == 1) {
87 2
                $statistic->setFailedCount((int)$row['count']);
88 6
            } elseif ($row['pending'] == 1) {
89 6
                $statistic->setPendingCount((int)$row['count']);
90
            } else {
91 6
                $statistic->setSuccessCount((int)$row['count']);
92
            }
93
        }
94
95 6
        return $statistic;
96
    }
97
98
    /**
99
     * Don't use this method.
100
     *
101
     * @return int
102
     * @throws UnsupportedMethodException
103
     */
104
    public function count(): int
105
    {
106
        throw new UnsupportedMethodException('Can not count the Index Queue Statistics.', 1504694750);
107
    }
108
}
109