Completed
Pull Request — master (#6)
by Konstantin
05:51 queued 01:59
created

IndexManager   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 1 Features 2
Metric Value
wmc 14
c 4
b 1
f 2
lcom 1
cbo 6
dl 0
loc 121
ccs 0
cts 80
cp 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A reindex() 0 21 3
A reindexItems() 0 10 2
A removeItems() 0 8 1
A getIndexRange() 0 10 1
A truncate() 0 4 1
A getIndexer() 0 8 2
A processItems() 0 21 3
1
<?php
2
3
namespace Brouzie\Sphinxy;
4
5
use Brouzie\Sphinxy\Indexer\IndexerInterface;
6
use Symfony\Component\DependencyInjection\ContainerInterface;
7
8
class IndexManager
9
{
10
    protected $conn;
11
12
    protected $container;
13
14
    protected $indexers = array();
15
16
    /**
17
     * @param Registry $registry
18
     * @param ContainerInterface $container
19
     * @param array $indexers
20
     */
21
    public function __construct(Registry $registry, ContainerInterface $container, array $indexers)
22
    {
23
        $this->conn = $registry->getConnection();
24
        $this->container = $container;
25
        $this->indexers = $indexers;
26
    }
27
28
    public function reindex($index, $batchSize = 1000, $batchCallback = null, array $rangeCriterias = array())
29
    {
30
        $logger = $this->conn->getLogger();
31
        $this->conn->setLogger(null);
32
33
        $indexer = $this->getIndexer($index);
34
        $range = array_replace($indexer->getRangeCriterias(), $rangeCriterias);
35
36
        $idFrom = $range['min'];
37
        do {
38
            $idTo = $idFrom + $batchSize;
39
            if (is_callable($batchCallback)) {
40
                call_user_func($batchCallback, array('id_from' => $idFrom, 'id_to' => $idTo, 'min_id' => $range['min'], 'max_id' => $range['max']));
41
            }
42
43
            $items = $indexer->getItemsByInterval($idFrom, $idTo);
44
            $this->processItems($index, $indexer, $items);
45
            $idFrom = $idTo;
46
        } while ($idFrom <= $range['max']);
47
        $this->conn->setLogger($logger);
48
    }
49
50
    public function reindexItems($index, $itemsIds, $batchSize = 100)
51
    {
52
        $indexer = $this->getIndexer($index);
53
54
        do {
55
            $itemsIdsToProcess = array_splice($itemsIds, 0, $batchSize);
56
            $items = $indexer->getItemsByIds($itemsIdsToProcess);
57
            $this->processItems($index, $indexer, $items);
58
        } while ($itemsIdsToProcess);
0 ignored issues
show
Bug Best Practice introduced by
The expression $itemsIdsToProcess of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
59
    }
60
61
    public function removeItems($index, $itemsIds)
62
    {
63
        return $this->conn->createQueryBuilder()
64
            ->delete($this->conn->getEscaper()->quoteIdentifier($index))
65
            ->where('id IN :ids')
66
            ->setParameter('ids', $itemsIds)
67
            ->execute();
68
    }
69
70
    public function getIndexRange($index)
71
    {
72
        return $this->conn
73
                ->createQueryBuilder()
74
                ->select('MIN(id) AS `min`, MAX(id) AS `max`')
75
                ->from($this->conn->getEscaper()->quoteIdentifier($index))
76
                ->getResult()
77
                ->getIterator()
78
                ->current();
79
    }
80
81
    public function truncate($index)
82
    {
83
        $this->conn->executeUpdate(sprintf('TRUNCATE RTINDEX %s', $this->conn->getEscaper()->quoteIdentifier($index)));
84
    }
85
86
    /**
87
     * @param $index
88
     *
89
     * @return IndexerInterface
90
     *
91
     * @throws \InvalidArgumentException When index not defined
92
     */
93
    protected function getIndexer($index)
94
    {
95
        if (!isset($this->indexers[$index])) {
96
            throw new \InvalidArgumentException('Unknown index');
97
        }
98
99
        return $this->container->get($this->indexers[$index]);
100
    }
101
102
    /**
103
     * @param $index
104
     * @param IndexerInterface $indexer
105
     * @param $items
106
     */
107
    protected function processItems($index, IndexerInterface $indexer, $items)
108
    {
109
        $items = $indexer->processItems($items);
110
        if (!count($items)) {
111
            return;
112
        }
113
114
        $insertQb = $this->conn
115
            ->createQueryBuilder()
116
            ->replace($this->conn->getEscaper()->quoteIdentifier($index));
117
118
        foreach ($items as $item) {
119
            $insertQb->addValues(
120
                $this->conn->getEscaper()->quoteSetArr(
121
                    $indexer->serializeItem($item)
122
                )
123
            );
124
        }
125
126
        $insertQb->execute();
127
    }
128
}
129