UpdateQueryBuilder   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 55
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A buildUpdateQuery() 0 7 1
A __construct() 0 4 1
A buildUpdateQueryBySchemaName() 0 14 3
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
The condition is_string($schemaName) is always true.
Loading history...
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
}