Passed
Push — master ( c306d6...ceddc6 )
by Timo
51:27 queued 28:43
created

QueueStatisticsRepository::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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 2 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
    public function findOneByRootPidAndOptionalIndexingConfigurationName(int $rootPid, $indexingConfigurationName = null): QueueStatistic
51
    {
52
        $queryBuilder = $this->getQueryBuilder();
53
        $queryBuilder
54
            ->add('select', vsprintf('(%s < %s) AS %s', [
55
                $queryBuilder->quoteIdentifier('indexed'),
56
                $queryBuilder->quoteIdentifier('changed'),
57
                $queryBuilder->quoteIdentifier('pending')
58
            ]), true)
59
            ->add('select', vsprintf('%s AS %s', [
60
                $queryBuilder->expr()->notLike('errors', '""'),
61
                $queryBuilder->quoteIdentifier('failed')
62
            ]), true)
63
            ->add('select', $queryBuilder->expr()->count('*', 'count'), true)
64
            ->from($this->table)
65
            ->where($queryBuilder->expr()->eq('root', $queryBuilder->createNamedParameter($rootPid, \PDO::PARAM_INT)))
66
            ->groupBy('pending', 'failed');
67
68
        if (!empty($indexingConfigurationName)) {
69
            $queryBuilder->add('where',
70
                $queryBuilder->expr()->eq('indexing_configuration', $queryBuilder->createNamedParameter($indexingConfigurationName)), true);
71
        }
72
73
        return $this->buildQueueStatisticFromResultSet($queryBuilder->execute()->fetchAll());
74
    }
75
76
    /**
77
     * Instantiates and fills QueueStatistic with values
78
     *
79
     * @param array $indexQueueStatisticResultSet
80
     * @return QueueStatistic
81
     */
82
    protected function buildQueueStatisticFromResultSet(array $indexQueueStatisticResultSet): QueueStatistic
83
    {
84
        /* @var $statistic QueueStatistic */
85
        $statistic = GeneralUtility::makeInstance(QueueStatistic::class);
86
        foreach ($indexQueueStatisticResultSet as $row) {
87
            if ($row['failed'] == 1) {
88
                $statistic->setFailedCount((int)$row['count']);
89
            } elseif ($row['pending'] == 1) {
90
                $statistic->setPendingCount((int)$row['count']);
91
            } else {
92
                $statistic->setSuccessCount((int)$row['count']);
93
            }
94
        }
95
96
        return $statistic;
97
    }
98
99
    /**
100
     * Don't use this method.
101
     *
102
     * @return int
103
     * @throws UnsupportedMethodException
104
     */
105
    public function count(): int
106
    {
107
        throw new UnsupportedMethodException('Can not count the Index Queue Statistics.', 1504694750);
108
    }
109
}
110