|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mdiyakov\DoctrineSolrBundle\Manager; |
|
4
|
|
|
|
|
5
|
|
|
use Mdiyakov\DoctrineSolrBundle\Exception\FilterException; |
|
6
|
|
|
use Mdiyakov\DoctrineSolrBundle\Filter\FilterValidator; |
|
7
|
|
|
use Mdiyakov\DoctrineSolrBundle\Indexer\IndexerBuilder; |
|
8
|
|
|
|
|
9
|
|
|
class IndexProcessManager |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var IndexerBuilder |
|
13
|
|
|
*/ |
|
14
|
|
|
private $indexerBuilder; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var FilterValidator |
|
18
|
|
|
*/ |
|
19
|
|
|
private $filterValidator; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param IndexerBuilder $indexerBuilder |
|
23
|
|
|
* @param FilterValidator $filterValidator |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(IndexerBuilder $indexerBuilder, FilterValidator $filterValidator) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->indexerBuilder = $indexerBuilder; |
|
28
|
|
|
$this->filterValidator = $filterValidator; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param object $entity |
|
33
|
|
|
* @return IndexProcessResult |
|
34
|
|
|
* @throws \InvalidArgumentException |
|
35
|
|
|
*/ |
|
36
|
|
|
public function reindex($entity) |
|
37
|
|
|
{ |
|
38
|
|
|
if (!is_object($entity)) { |
|
39
|
|
|
throw new \InvalidArgumentException('Argument must be an entity'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$result = new IndexProcessResult(); |
|
43
|
|
|
$entityClass = get_class($entity); |
|
44
|
|
|
$indexer = $this->indexerBuilder->createByEntityClass($entityClass); |
|
45
|
|
|
try { |
|
46
|
|
|
$this->filterValidator->validate($entity); |
|
47
|
|
|
$indexer->indexAllFields($entity); |
|
48
|
|
|
$result->setSuccess(true); |
|
49
|
|
|
} catch (FilterException $e) { |
|
50
|
|
|
$indexer->removeByPrimaryKey($entity); |
|
51
|
|
|
$result->setError($e->getMessage()); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $result; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param object $entity |
|
59
|
|
|
*/ |
|
60
|
|
|
public function remove($entity) |
|
61
|
|
|
{ |
|
62
|
|
|
if (!is_object($entity)) { |
|
63
|
|
|
throw new \InvalidArgumentException('Argument must be an entity'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$entityClass = get_class($entity); |
|
67
|
|
|
$indexer = $this->indexerBuilder->createByEntityClass($entityClass); |
|
68
|
|
|
$indexer->removeByPrimaryKey($entity); |
|
69
|
|
|
} |
|
70
|
|
|
} |