Storage::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 8
Ratio 100 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 8
loc 8
rs 9.4285
cc 1
eloc 6
nc 1
nop 3
1
<?php
2
3
/*
4
 * This file is part of the Storage package
5
 *
6
 * (c) Michal Wachowski <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Moss\Storage\Query;
13
14
use Doctrine\DBAL\Connection;
15
use Moss\Storage\Model\ModelBag;
16
use Moss\Storage\Query\Accessor\Accessor;
17
use Moss\Storage\Query\Accessor\AccessorInterface;
18
use Moss\Storage\Query\EventDispatcher\EventDispatcherInterface;
19
use Moss\Storage\Query\Relation\RelationFactory;
20
use Moss\Storage\Query\Relation\RelationFactoryInterface;
21
use Moss\Storage\NormalizeNamespaceTrait;
22
use Moss\Storage\StorageException;
23
24
/**
25
 * Storage - query source, used to create and execute CRUD operations on entities
26
 *
27
 * @author  Michal Wachowski <[email protected]>
28
 * @package Moss\Storage
29
 */
30
class Storage implements StorageInterface
31
{
32
    use NormalizeNamespaceTrait;
33
34
    /**
35
     * @var Connection
36
     */
37
    protected $connection;
38
39
    /**
40
     * @var ModelBag
41
     */
42
    protected $models;
43
44
    /**
45
     * @var RelationFactoryInterface
46
     */
47
    protected $factory;
48
49
    /**
50
     * @var AccessorInterface
51
     */
52
    protected $accessor;
53
54
    /**
55
     * @var EventDispatcherInterface
56
     */
57
    protected $dispatcher;
58
59
    /**
60
     * Constructor
61
     *
62
     * @param Connection               $connection
63
     * @param ModelBag                 $models
64
     * @param EventDispatcherInterface $dispatcher
65
     */
66 View Code Duplication
    public function __construct(Connection $connection, ModelBag $models, EventDispatcherInterface $dispatcher)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    {
68
        $this->connection = $connection;
69
        $this->models = $models;
70
        $this->factory = new RelationFactory($this, $models);
71
        $this->accessor = new Accessor();
72
        $this->dispatcher = $dispatcher;
73
    }
74
75
    /**
76
     * Returns connection
77
     *
78
     * @return Connection
79
     */
80
    public function connection()
81
    {
82
        return $this->connection;
83
    }
84
85
    /**
86
     * Registers event listener
87
     *
88
     * @param string   $event
89
     * @param callable $listener
90
     *
91
     * @return $this
92
     */
93
    public function registerEventListener($event, callable $listener)
94
    {
95
        $this->dispatcher->register($event, $listener);
96
97
        return $this;
98
    }
99
100
    /**
101
     * Sets read operation
102
     *
103
     * @param string $entityName
104
     *
105
     * @return ReadQueryInterface
106
     */
107
    public function read($entityName)
108
    {
109
        return new ReadQuery(
110
            $this->connection,
111
            $this->models->get($entityName),
112
            $this->factory,
113
            $this->accessor,
114
            $this->dispatcher
115
        );
116
    }
117
118
    /**
119
     * Sets read one operation
120
     *
121
     * @param string $entityName
122
     *
123
     * @return ReadQueryInterface
124
     */
125
    public function readOne($entityName)
126
    {
127
        return new ReadOneQuery(
128
            $this->connection,
129
            $this->models->get($entityName),
130
            $this->factory,
131
            $this->accessor,
132
            $this->dispatcher
133
        );
134
    }
135
136
    /**
137
     * Sets write operation
138
     *
139
     * @param array|object       $instance
140
     * @param null|string|object $entity
141
     *
142
     * @return WriteQueryInterface
143
     */
144 View Code Duplication
    public function write($instance, $entity = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
145
    {
146
        list($instance, $entity) = $this->reassignEntity($instance, $entity);
147
148
        return new WriteQuery(
149
            $this->connection,
150
            $instance,
151
            $this->models->get($entity),
152
            $this->factory,
153
            $this->accessor,
154
            $this->dispatcher
155
        );
156
    }
157
158
    /**
159
     * Sets update operation
160
     *
161
     * @param array|object       $instance
162
     * @param null|string|object $entity
163
     *
164
     * @return UpdateQueryInterface
165
     */
166 View Code Duplication
    public function update($instance, $entity = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
167
    {
168
        list($instance, $entity) = $this->reassignEntity($instance, $entity);
169
170
        return new UpdateQuery(
171
            $this->connection,
172
            $instance,
173
            $this->models->get($entity),
174
            $this->factory,
175
            $this->accessor,
176
            $this->dispatcher
177
        );
178
    }
179
180
    /**
181
     * Sets insert operation
182
     *
183
     * @param array|object       $instance
184
     * @param null|string|object $entity
185
     *
186
     * @return InsertQueryInterface
187
     */
188 View Code Duplication
    public function insert($instance, $entity = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
189
    {
190
        list($instance, $entity) = $this->reassignEntity($instance, $entity);
191
192
        return new InsertQuery(
193
            $this->connection,
194
            $instance,
195
            $this->models->get($entity),
196
            $this->factory,
197
            $this->accessor,
198
            $this->dispatcher
199
        );
200
    }
201
202
    /**
203
     * Sets delete operation
204
     *
205
     * @param array|object       $instance
206
     * @param null|string|object $entity
207
     *
208
     * @return DeleteQueryInterface
209
     */
210 View Code Duplication
    public function delete($instance, $entity = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
211
    {
212
        list($instance, $entity) = $this->reassignEntity($instance, $entity);
213
214
        return new DeleteQuery(
215
            $this->connection,
216
            $instance,
217
            $this->models->get($entity),
218
            $this->factory,
219
            $this->accessor,
220
            $this->dispatcher
221
        );
222
    }
223
224
    /**
225
     * Reassigns entity/instance variables if entity is object
226
     *
227
     * @param array|object       $instance
228
     * @param null|string|object $entity
229
     *
230
     * @return array
231
     * @throws StorageException
232
     */
233
    protected function reassignEntity($instance, $entity = null)
234
    {
235
        if ($entity === null && !is_object($instance)) {
236
            throw new StorageException('When entity class is omitted, instance must be an object');
237
        }
238
239
        if (is_object($instance)) {
240
            return [$instance, $this->normalizeNamespace($instance)];
241
        }
242
243
        return [$instance, $entity];
244
    }
245
}
246