Completed
Push — master ( 6a706e...7fd4a2 )
by Konstantin
12:25
created

IndexManager.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
            ->getSingleRow();
78
    }
79
80
    public function truncate($index)
81
    {
82
        $this->conn->executeUpdate(sprintf('TRUNCATE RTINDEX %s', $this->conn->getEscaper()->quoteIdentifier($index)));
83
    }
84
85
    /**
86
     * @param $index
87
     *
88
     * @return IndexerInterface
89
     *
90
     * @throws \InvalidArgumentException When index not defined
91
     */
92
    protected function getIndexer($index)
93
    {
94
        if (!isset($this->indexers[$index])) {
95
            throw new \InvalidArgumentException('Unknown index');
96
        }
97
98
        return $this->container->get($this->indexers[$index]);
99
    }
100
101
    /**
102
     * @param $index
103
     * @param IndexerInterface $indexer
104
     * @param $items
105
     */
106
    protected function processItems($index, IndexerInterface $indexer, $items)
107
    {
108
        $items = $indexer->processItems($items);
109
        if (!count($items)) {
110
            return;
111
        }
112
113
        $insertQb = $this->conn
114
            ->createQueryBuilder()
115
            ->replace($this->conn->getEscaper()->quoteIdentifier($index));
116
117
        foreach ($items as $item) {
118
            $insertQb->addValues(
119
                $this->conn->getEscaper()->quoteSetArr(
120
                    $indexer->serializeItem($item)
121
                )
122
            );
123
        }
124
125
        $insertQb->execute();
126
    }
127
}
128