|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nimble\ElasticBundle\Populator; |
|
4
|
|
|
|
|
5
|
|
|
use Nimble\ElasticBundle\Populator\Exception\PopulationFetcherAlreadyRegisteredException; |
|
6
|
|
|
use Nimble\ElasticBundle\Populator\Exception\PopulationFetcherNotFoundException; |
|
7
|
|
|
use Nimble\ElasticBundle\Transformer\TransformerManager; |
|
8
|
|
|
use Nimble\ElasticBundle\Type\Type; |
|
9
|
|
|
|
|
10
|
|
|
class PopulatorManager |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var PopulationFetcherInterface[][] |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $fetchers = []; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var TransformerManager |
|
19
|
|
|
*/ |
|
20
|
|
|
private $transformer; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param TransformerManager $transformer |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(TransformerManager $transformer) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->transformer = $transformer; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param $indexId |
|
32
|
|
|
* @param $typeName |
|
33
|
|
|
* @param PopulationFetcherInterface $fetcher |
|
34
|
|
|
*/ |
|
35
|
|
|
public function registerFetcher(PopulationFetcherInterface $fetcher, $indexId, $typeName) |
|
36
|
|
|
{ |
|
37
|
|
|
if (isset($this->fetchers[$indexId][$typeName])) { |
|
38
|
|
|
throw new PopulationFetcherAlreadyRegisteredException($indexId, $typeName); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$this->fetchers[$indexId][$typeName] = $fetcher; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param Type $type |
|
46
|
|
|
* @return PopulationFetcherInterface |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function getFetcherForType(Type $type) |
|
49
|
|
|
{ |
|
50
|
|
|
$indexId = $type->getIndex()->getId(); |
|
51
|
|
|
$typeName = $type->getName(); |
|
52
|
|
|
|
|
53
|
|
|
if (!isset($this->fetchers[$indexId][$typeName])) { |
|
54
|
|
|
throw new PopulationFetcherNotFoundException($indexId, $typeName); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $this->fetchers[$indexId][$typeName]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param Type $type |
|
62
|
|
|
* @return Populator |
|
63
|
|
|
*/ |
|
64
|
|
|
public function createPopulator(Type $type) |
|
65
|
|
|
{ |
|
66
|
|
|
return new Populator( |
|
67
|
|
|
$type, |
|
68
|
|
|
$this->getFetcherForType($type), |
|
69
|
|
|
$this->transformer |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
} |