mdiyakov /
DoctrineSolrBundle
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Mdiyakov\DoctrineSolrBundle\Config; |
||
| 4 | |||
| 5 | use Mdiyakov\DoctrineSolrBundle\Schema\Schema; |
||
| 6 | |||
| 7 | class Config |
||
| 8 | { |
||
| 9 | |||
| 10 | /** |
||
| 11 | * @var \string[][] |
||
| 12 | */ |
||
| 13 | private $indexedEntities; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var Schema[] |
||
| 17 | */ |
||
| 18 | private $entitySchemaMap = []; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var string[] |
||
| 22 | */ |
||
| 23 | private $entitiesClasses = []; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var Schema[] |
||
| 27 | */ |
||
| 28 | private $schemes = []; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var array|\string[][] |
||
| 32 | */ |
||
| 33 | private $filters = []; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | private $entitiesConfigMap = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var \string[] |
||
| 42 | */ |
||
| 43 | private $solariumClients; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param string[][] $indexedEntities |
||
| 47 | * @param string[][] $schemes |
||
| 48 | * @param string[][] $filters |
||
| 49 | * @param string[] $solariumClients |
||
| 50 | */ |
||
| 51 | public function __construct($indexedEntities, $schemes, $filters, $solariumClients) |
||
| 52 | { |
||
| 53 | $configValidator = new ConfigValidator(); |
||
| 54 | foreach ($indexedEntities as $entityConfig) { |
||
| 55 | $schemaName = $entityConfig['schema']; |
||
| 56 | $configValidator->validate($entityConfig, $schemes, $filters, $solariumClients); |
||
| 57 | |||
| 58 | /** todo should be moved to some SchemaProvider class */ |
||
| 59 | if (!isset($this->schemes[$schemaName])) { |
||
| 60 | $schema = new Schema( |
||
| 61 | $schemaName, |
||
| 62 | $schemes[$schemaName]['client'], |
||
| 63 | $schemes[$schemaName]['document_unique_field'], |
||
| 64 | $schemes[$schemaName]['fields'], |
||
| 65 | $schemes[$schemaName]['config_entity_fields'] |
||
| 66 | ); |
||
| 67 | $this->schemes[$schemaName] = $schema; |
||
| 68 | } |
||
| 69 | |||
| 70 | $this->entitySchemaMap[$entityConfig['class']] = $schema; |
||
| 71 | $this->entitiesClasses[$entityConfig['class']] = true; |
||
| 72 | $this->entitiesConfigMap[$entityConfig['class']] = $entityConfig; |
||
| 73 | } |
||
| 74 | |||
| 75 | $this->filters = $filters; |
||
| 76 | $this->indexedEntities = $indexedEntities; |
||
| 77 | $this->solariumClients = $solariumClients; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param string $class |
||
| 82 | * @return Schema|null |
||
| 83 | */ |
||
| 84 | public function getSchemaByEntityClass($class) |
||
| 85 | { |
||
| 86 | if (array_key_exists($class, $this->entitySchemaMap)) { |
||
| 87 | return $this->entitySchemaMap[$class]; |
||
| 88 | } |
||
| 89 | |||
| 90 | return null; |
||
| 91 | } |
||
| 92 | |||
| 93 | |||
| 94 | /** |
||
| 95 | * @param string $schemaName |
||
| 96 | * @return Schema|null |
||
| 97 | */ |
||
| 98 | public function getSchemaByName($schemaName) |
||
| 99 | { |
||
| 100 | if (array_key_exists($schemaName, $this->schemes)) { |
||
| 101 | return $this->schemes[$schemaName]; |
||
| 102 | } |
||
| 103 | |||
| 104 | return null; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param string $class |
||
| 109 | * @return string|null |
||
| 110 | * |
||
| 111 | */ |
||
| 112 | public function getClientByEntityClass($class) |
||
| 113 | { |
||
| 114 | return array_key_exists($class, $this->entityClientMap) ? $this->entityClientMap[$class] : null; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param $class |
||
| 119 | * @return null|\string[][] |
||
| 120 | */ |
||
| 121 | public function getEntityConfig($class) |
||
| 122 | { |
||
| 123 | return array_key_exists($class, $this->entitiesConfigMap) ? $this->entitiesConfigMap[$class] : null; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @return string[] |
||
| 128 | */ |
||
| 129 | public function getIndexedClasses() |
||
| 130 | { |
||
| 131 | return array_keys($this->entitiesClasses); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @return array |
||
| 136 | */ |
||
| 137 | public function getIndexedEntities() |
||
| 138 | { |
||
| 139 | return $this->indexedEntities; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @return array|\string[][] |
||
| 144 | */ |
||
| 145 | public function getFilters() |
||
| 146 | { |
||
| 147 | return $this->filters; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param string $clientName |
||
| 152 | * @return null|string |
||
| 153 | */ |
||
| 154 | public function getSolariumClient($clientName) |
||
| 155 | { |
||
| 156 | return (array_key_exists($clientName, $this->solariumClients)) ? $this->solariumClients[$clientName] : null; |
||
|
0 ignored issues
–
show
|
|||
| 157 | } |
||
| 158 | } |