Completed
Pull Request — master (#25)
by Joshua
04:40
created

Manager::createIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
namespace As3\Bundle\ModlrBundle\Schema;
4
5
use As3\Modlr\Store\Store;
6
// use As3\Modlr\Metadata\Schema\IndexDefinition; // StorageMetadata?
7
8
/**
9
 * Handles requests to create or update indices
10
 *
11
 * @author  Josh Worden <[email protected]>
12
 * @todo    This should be updated to read indices from the metadata factory
13
 */
14
class Manager
15
{
16
    /**
17
     * @var     array
18
     */
19
    private $indices = [];
20
21
    /**
22
     * @var     Store
23
     */
24
    private $store;
25
26
    /**
27
     * @param   Store   $store      The As3\Modlr\Store instance
28
     * @param   array   $schema     The bundle's schema configuration
29
     */
30
    public function __construct(Store $store, array $schema = [])
31
    {
32
        $this->store = $store;
33
        $this->indices = isset($schema['indices']) ? $schema['indices'] : [];
34
    }
35
36
    /**
37
     * Returns the loaded indices
38
     *
39
     * @param   string  $type   The model type to retrieve indices for
40
     * @return  array
41
     */
42
    public function getIndices($type = null)
43
    {
44
        if (null === $type) {
45
            return $this->indices;
46
        }
47
        $out = [];
48
        foreach ($this->indices as $index) {
49
            if ($index['model_type'] === $type) {
50
                $out[] = $index;
51
            }
52
        }
53
        return $out;
54
    }
55
56
    /**
57
     * Creates an index from the supplied data.
58
     * @todo    Change to Metadata\Schema\IndexDefinition or something later
59
     *
60
     * @param   array       $index  Associative array containin index data
61
     *
62
     * @return  boolean     If the index was created successfully
63
     */
64
    public function createIndex(array $index)
65
    {
66
        $index['options']['background'] = true;
67
        $type = $index['model_type'];
68
        $metadata = $this->store->getMetadataForType($type);
69
        $collection = $this->store->getPersisterFor($type)->getQuery()->getModelCollection($metadata);
70
        return $collection->ensureIndex($index['keys'], $index['options']);
71
    }
72
}
73