1 | <?php |
||
2 | |||
3 | namespace Mdiyakov\DoctrineSolrBundle\Query; |
||
4 | |||
5 | use Mdiyakov\DoctrineSolrBundle\Exception\SchemaNotFoundException; |
||
6 | use Mdiyakov\DoctrineSolrBundle\Query\Suggest\ClassSuggestQuery; |
||
7 | use Mdiyakov\DoctrineSolrBundle\Query\Suggest\SchemaSuggestQuery; |
||
8 | use Mdiyakov\DoctrineSolrBundle\Schema\Schema; |
||
9 | use Nelmio\SolariumBundle\ClientRegistry; |
||
10 | use Mdiyakov\DoctrineSolrBundle\Config\Config; |
||
11 | use Mdiyakov\DoctrineSolrBundle\Exception\EntityNotIndexedException; |
||
12 | |||
13 | class SuggestQueryBuilder |
||
14 | { |
||
15 | /** |
||
16 | * @var |
||
17 | */ |
||
18 | private $config; |
||
19 | |||
20 | /** |
||
21 | * @var ClientRegistry |
||
22 | */ |
||
23 | private $clientRegistry; |
||
24 | |||
25 | public function __construct( |
||
26 | Config $config, |
||
27 | ClientRegistry $clientRegistry |
||
28 | ) |
||
29 | { |
||
30 | $this->config = $config; |
||
31 | $this->clientRegistry = $clientRegistry; |
||
32 | |||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @param $class |
||
37 | * @return ClassSuggestQuery |
||
38 | */ |
||
39 | public function buildClassSuggestQuery($class) |
||
40 | { |
||
41 | $entityConfig = $this->config->getEntityConfig($class); |
||
42 | if (!$entityConfig) { |
||
43 | throw new EntityNotIndexedException( |
||
44 | sprintf('""%s" is not indexed. Check bundle config in config.yml', $class) |
||
45 | ); |
||
46 | } |
||
47 | $schema = $this->config->getSchemaByEntityClass($class); |
||
48 | |||
49 | return new ClassSuggestQuery( |
||
50 | $this->clientRegistry->getClient( |
||
51 | $this->config->getSolariumClient($schema->getClient()) |
||
52 | ), |
||
53 | $schema, |
||
54 | $entityConfig |
||
55 | ); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * @param string $schemaName |
||
60 | * @return SchemaSuggestQuery |
||
61 | * @throws \InvalidArgumentException |
||
62 | * @throws SchemaNotFoundException |
||
63 | */ |
||
64 | public function buildSchemaSuggestQueryBySchemaName($schemaName) |
||
65 | { |
||
66 | if (!is_string($schemaName)) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
67 | throw new \InvalidArgumentException('Argument $schemaName must be a string'); |
||
68 | } |
||
69 | |||
70 | $schema = $this->config->getSchemaByName($schemaName); |
||
71 | if (!$schema) { |
||
72 | throw new SchemaNotFoundException( |
||
73 | sprintf('Schema "%s" is not found', $schemaName) |
||
74 | ); |
||
75 | } |
||
76 | |||
77 | return $this->buildSchemaSuggestQuery($schema); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @param Schema $schema |
||
82 | * @return SchemaSuggestQuery |
||
83 | */ |
||
84 | public function buildSchemaSuggestQuery(Schema $schema) |
||
85 | { |
||
86 | return new SchemaSuggestQuery( |
||
87 | $this->clientRegistry->getClient( |
||
88 | $this->config->getSolariumClient($schema->getClient()) |
||
89 | ), |
||
90 | $schema |
||
91 | ); |
||
92 | } |
||
93 | } |