Completed
Branch feature/split-orm (fa2f8e)
by Anton
03:35
created

DocumentSource::iocContainer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/**
3
 * components
4
 *
5
 * @author    Wolfy-J
6
 */
7
namespace Spiral\ODM\Entities;
8
9
use Spiral\Core\Component;
10
use Spiral\Core\Traits\SaturateTrait;
11
use Spiral\ODM\CompositableInterface;
12
use Spiral\ODM\DocumentEntity;
13
use Spiral\ODM\Exceptions\SourceException;
14
use Spiral\ODM\MongoManager;
15
use Spiral\ODM\ODM;
16
17
/**
18
 * Source class associated to one or multiple (default implementation) ODM models. Source can be
19
 * used to write your own custom find method or change default selection.
20
 */
21
class DocumentSource extends Component implements \Countable, \IteratorAggregate
22
{
23
    use SaturateTrait;
24
25
    /**
26
     * Linked document model. ODM can automatically index and link user sources to models based on
27
     * value of this constant.
28
     */
29
    const DOCUMENT = null;
30
31
    /**
32
     * @var DocumentSelector
33
     */
34
    private $selector;
35
36
    /**
37
     * Associated document class.
38
     *
39
     * @var string
40
     */
41
    private $class = null;
42
43
    /**
44
     * @invisible
45
     *
46
     * @var ODM
47
     */
48
    protected $odm = null;
49
50
    /**
51
     * @param string $class
52
     * @param ODM    $odm
53
     *
54
     * @throws SourceException
55
     */
56
    public function __construct(string $class = null, ODM $odm = null)
57
    {
58
        if (empty($class)) {
59
            if (empty(static::DOCUMENT)) {
60
                throw new SourceException('Unable to create source without associated class');
61
            }
62
63
            $class = static::DOCUMENT;
64
        }
65
66
        $this->class = $class;
67
        $this->odm = $this->saturate($odm, ODM::class);
68
        $this->selector = $this->odm->selector($this->class);
69
    }
70
71
    /**
72
     * Create new DocumentEntity based on set of provided fields.
73
     *
74
     * @final Change static method of entity, not this one.
75
     *
76
     * @param array  $fields
77
     * @param string $class  Due ODM models can be inherited you can use this argument to specify
78
     *                       custom model class.
79
     *
80
     * @return CompositableInterface|DocumentEntity
81
     */
82
    public function create($fields = [], string $class = null)
83
    {
84
        return $this->odm->instantiate($class ?? $this->class, $fields);
85
    }
86
87
    /**
88
     * Find document by it's primary key.
89
     *
90
     * @see findOne()
91
     *
92
     * @param string|\MongoId $id Primary key value.
93
     *
94
     * @return CompositableInterface|DocumentEntity|null
95
     */
96
    public function findByPK($id)
97
    {
98
        return $this->findOne(['_id' => MongoManager::mongoID($id)]);
99
    }
100
101
    /**
102
     * Select one document from mongo collection.
103
     *
104
     * @param array $query  Fields and conditions to query by.
105
     * @param array $sortBy Always specify sort by to ensure that results are stable.
106
     *
107
     * @return CompositableInterface|DocumentEntity|null
108
     */
109
    public function findOne(array $query = [], array $sortBy = [])
110
    {
111
        return $this->getIterator()->sortBy($sortBy)->findOne($query);
112
    }
113
114
    /**
115
     * Get associated document selection with pre-configured query (if any).
116
     *
117
     * @param array $query
118
     *
119
     * @return DocumentSelector
120
     */
121
    public function find(array $query = []): DocumentSelector
122
    {
123
        return $this->getIterator()->where($query);
124
    }
125
126
    /**
127
     * @return int
128
     */
129
    public function count(): int
130
    {
131
        return $this->getIterator()->count();
132
    }
133
134
    /**
135
     * @return DocumentSelector
136
     */
137
    public function getIterator(): DocumentSelector
138
    {
139
        return clone $this->selector;
140
    }
141
142
    /**
143
     * Create source with new associated selector.
144
     *
145
     * @param DocumentSelector $selector
146
     *
147
     * @return DocumentSource
148
     */
149
    public function withSelector(DocumentSelector $selector): DocumentSource
150
    {
151
        $source = clone $this;
152
        $source->selector = $selector;
153
154
        return $source;
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160
    protected function iocContainer()
161
    {
162
        if ($this->odm instanceof Component) {
163
            //Always work in ODM scope
164
            return $this->odm->iocContainer();
1 ignored issue
show
Bug introduced by
The method iocContainer() cannot be called from this context as it is declared protected in class Spiral\Core\Component.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
165
        }
166
167
        return parent::iocContainer();
168
    }
169
}