1 | <?php |
||
2 | |||
3 | namespace Mdiyakov\DoctrineSolrBundle\Query; |
||
4 | |||
5 | use Mdiyakov\DoctrineSolrBundle\Config\Config; |
||
6 | use Mdiyakov\DoctrineSolrBundle\Exception\SchemaNotFoundException; |
||
7 | use Mdiyakov\DoctrineSolrBundle\Query\Update\UpdateQuery; |
||
8 | use Mdiyakov\DoctrineSolrBundle\Schema\Schema; |
||
9 | use Nelmio\SolariumBundle\ClientRegistry; |
||
10 | |||
11 | class UpdateQueryBuilder |
||
12 | { |
||
13 | /** |
||
14 | * @var ClientRegistry |
||
15 | */ |
||
16 | private $clientRegistry; |
||
17 | |||
18 | /** |
||
19 | * @var Config |
||
20 | */ |
||
21 | private $config; |
||
22 | |||
23 | /** |
||
24 | * @param Config $config |
||
25 | * @param ClientRegistry $clientRegistry |
||
26 | */ |
||
27 | public function __construct(Config $config, ClientRegistry $clientRegistry) |
||
28 | { |
||
29 | $this->config = $config; |
||
30 | $this->clientRegistry = $clientRegistry; |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * @param string $schemaName |
||
35 | * @return UpdateQuery |
||
36 | * @throws \InvalidArgumentException |
||
37 | * @throws SchemaNotFoundException |
||
38 | */ |
||
39 | public function buildUpdateQueryBySchemaName($schemaName) |
||
40 | { |
||
41 | if (!is_string($schemaName)) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
42 | throw new \InvalidArgumentException('Argument $schemaName must be a string'); |
||
43 | } |
||
44 | |||
45 | $schema = $this->config->getSchemaByName($schemaName); |
||
46 | if (!$schema) { |
||
47 | throw new SchemaNotFoundException( |
||
48 | sprintf('Schema "%s" is not found', $schemaName) |
||
49 | ); |
||
50 | } |
||
51 | |||
52 | return $this->buildUpdateQuery($schema); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @param Schema $schema |
||
57 | * @return UpdateQuery |
||
58 | */ |
||
59 | public function buildUpdateQuery(Schema $schema) |
||
60 | { |
||
61 | return new UpdateQuery( |
||
62 | $this->clientRegistry->getClient( |
||
63 | $this->config->getSolariumClient($schema->getClient()) |
||
64 | ), |
||
65 | $schema |
||
66 | ); |
||
67 | } |
||
68 | } |