Completed
Branch feature/split-orm (60a911)
by Anton
03:15
created

ODM::updateSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * components
4
 *
5
 * @author    Wolfy-J
6
 */
7
namespace Spiral\ODM;
8
9
use Spiral\Core\Component;
10
use Spiral\Core\Container\SingletonInterface;
11
use Spiral\Core\FactoryInterface;
12
use Spiral\Core\MemoryInterface;
13
14
class ODM extends Component implements ODMInterface, SingletonInterface
15
{
16
    /**
17
     * Memory section to store ODM schema.
18
     */
19
    const MEMORY = 'odm.schema';
20
21
    /**
22
     * Already created instantiators.
23
     *
24
     * @invisible
25
     * @var InstantiatorInterface[]
26
     */
27
    private $instantiators = [];
28
29
    /**
30
     * Set of classes responsible for document save operations.
31
     *
32
     * @invisible
33
     *
34
     * @var MapperInterface[]
35
     */
36
    private $mappers = [];
37
38
    /**
39
     * ODM schema.
40
     *
41
     * @invisible
42
     * @var array
43
     */
44
    private $schema = [];
45
46
    /**
47
     * @var MongoManager
48
     */
49
    protected $manager;
50
51
    /**
52
     * @invisible
53
     * @var MemoryInterface
54
     */
55
    protected $memory;
56
57
    /**
58
     * @var FactoryInterface
59
     */
60
    protected $factory;
61
62
    /**
63
     * @param MongoManager     $manager
64
     * @param MemoryInterface  $memory
65
     * @param FactoryInterface $factory
66
     */
67
    public function __construct(
68
        MongoManager $manager,
69
        MemoryInterface $memory,
70
        FactoryInterface $factory
71
    ) {
72
        $this->manager = $manager;
73
        $this->memory = $memory;
74
        $this->factory = $factory;
75
76
        //Loading schema from memory (if any)
77
        $this->schema = (array)$memory->loadData(static::MEMORY);
78
    }
79
80
    public function setSchema($s)
81
    {
82
        $this->schema = $s;
83
    }
84
85
    protected function loadSchema()
86
    {
87
    }
88
89
    protected function updateSchema()
90
    {
91
92
    }
93
94
    /**
95
     * Instantiate document/model instance based on a given class name and fieldset. Some ODM
96
     * documents might return instances of their child if fields point to child model schema.
97
     *
98
     * @param string $class
99
     * @param array  $fields
100
     *
101
     * @return CompositableInterface
102
     */
103
    public function instantiate(string $class, $fields = []): CompositableInterface
104
    {
105
        return $this->instantiator($class)->instantiate($fields);
106
    }
107
108
    public function selector(string $class)
0 ignored issues
show
Unused Code introduced by
The parameter $class is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
109
    {
110
        //Selector!
111
        return new DocumentSelector();
112
    }
113
114
    public function mapper(string $class)
0 ignored issues
show
Unused Code introduced by
The parameter $class is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
115
    {
116
        return new DocumentMapper();
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function instantiator(string $class): InstantiatorInterface
123
    {
124
        if (isset($this->instantiators[$class])) {
125
            return $this->instantiators[$class];
126
        }
127
128
        //Potential optimization
129
        $instantiator = $this->factory->make($this->schema[$class][self::D_INSTANTIATOR], [
130
            'class'  => $class,
131
            'odm'    => $this,
132
            'schema' => $this->schema[$class][self::D_SCHEMA]
133
        ]);
134
135
        //Constructing instantiator and storing it in cache
136
        return $this->instantiators[$class] = $instantiator;
137
    }
138
139
    /**
140
     * Get property from cached schema.
141
     *
142
     * @param string $class
143
     * @param int    $property See ODM constants.
144
     *
145
     * @return mixed
146
     */
147
    protected function schema(string $class, int $property)
0 ignored issues
show
Unused Code introduced by
The parameter $class is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $property is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
148
    {
149
        return [];
150
    }
151
}