1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Brouzie\Sphinxy\Indexer; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\AbstractQuery; |
6
|
|
|
use Doctrine\ORM\EntityManager; |
7
|
|
|
|
8
|
|
|
abstract class DoctrineQbIndexer implements IndexerInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var EntityManager |
12
|
|
|
*/ |
13
|
|
|
protected $em; |
14
|
|
|
|
15
|
|
|
public function __construct(EntityManager $em) |
16
|
|
|
{ |
17
|
|
|
$this->em = $em; |
18
|
|
|
|
19
|
|
|
$this->em->getConfiguration()->setSQLLogger(null); |
20
|
|
|
$this->em->getConnection()->getConfiguration()->setSQLLogger(null); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function getRangeCriterias() |
24
|
|
|
{ |
25
|
|
|
$this->checkConnection(); |
26
|
|
|
|
27
|
|
|
$rootId = $this->getRootIdentifier(); |
28
|
|
|
$qb = $this->getQueryBuilder(); |
29
|
|
|
|
30
|
|
|
$range = $qb |
31
|
|
|
->select($qb->expr()->min($rootId), $qb->expr()->max($rootId)) |
32
|
|
|
->getQuery() |
33
|
|
|
->getSingleResult(); |
34
|
|
|
|
35
|
|
|
return array('min' => array_shift($range), 'max' => array_shift($range)); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
View Code Duplication |
public function getItemsByIds(array $ids) |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
$this->checkConnection(); |
41
|
|
|
$rootId = $this->getRootIdentifier(); |
42
|
|
|
|
43
|
|
|
$items = $this->getQueryBuilder() |
44
|
|
|
->andWhere(sprintf('%s IN (:ids)', $rootId)) |
45
|
|
|
->setParameter('ids', $ids) |
46
|
|
|
->getQuery() |
47
|
|
|
->getResult($this->getHydrationMode()) |
48
|
|
|
; |
49
|
|
|
|
50
|
|
|
return $items; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
View Code Duplication |
public function getItemsByInterval($idFrom, $idTo) |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$this->checkConnection(); |
56
|
|
|
$rootId = $this->getRootIdentifier(); |
57
|
|
|
|
58
|
|
|
$items = $this->getQueryBuilder() |
59
|
|
|
->andWhere(sprintf('%s >= :min AND %s < :max', $rootId, $rootId)) |
60
|
|
|
->setParameter('min', $idFrom) |
61
|
|
|
->setParameter('max', $idTo) |
62
|
|
|
->getQuery() |
63
|
|
|
->getResult($this->getHydrationMode()) |
64
|
|
|
; |
65
|
|
|
|
66
|
|
|
return $items; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function processItems(array $items) |
70
|
|
|
{ |
71
|
|
|
return $items; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function serializeItem($item) |
75
|
|
|
{ |
76
|
|
|
return $item; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return \Doctrine\ORM\QueryBuilder |
81
|
|
|
*/ |
82
|
|
|
protected function getQueryBuilder() |
83
|
|
|
{ |
84
|
|
|
return $this->em->getRepository($this->getEntityName())->createQueryBuilder('e'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
protected function getEntityName() |
88
|
|
|
{ |
89
|
|
|
throw new \BadMethodCallException('You should override this method.'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
protected function getRootIdentifier() |
93
|
|
|
{ |
94
|
|
|
return 'e.id'; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
protected function getHydrationMode() |
98
|
|
|
{ |
99
|
|
|
return AbstractQuery::HYDRATE_ARRAY; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
private function checkConnection() |
103
|
|
|
{ |
104
|
|
|
$connection = $this->em->getConnection(); |
105
|
|
|
if ($connection->ping() === false) { |
106
|
|
|
$connection->close(); |
107
|
|
|
$connection->connect(); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.