Completed
Push — master ( c4e3cd...c5cae6 )
by Anton
05:38
created

RecordSource::find()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
namespace Spiral\ORM\Entities;
9
10
use Spiral\Core\Component;
11
use Spiral\Core\Traits\SaturateTrait;
12
use Spiral\Models\SourceInterface;
13
use Spiral\ORM\Exceptions\SourceException;
14
use Spiral\ORM\ORM;
15
use Spiral\ORM\RecordEntity;
16
17
/**
18
 * Source class associated to one or multiple (default implementation) ORM models. Source can be
19
 * used to write your own custom find method or change default selection.
20
 */
21
class RecordSource extends Component implements SourceInterface, \Countable
22
{
23
    /**
24
     * Sugary!
25
     */
26
    use SaturateTrait;
27
28
    /**
29
     * Linked document model. ORM can automatically index and link user sources to models based on
30
     * value of this constant.
31
     */
32
    const RECORD = null;
33
34
    /**
35
     * Associated document class.
36
     *
37
     * @var string
38
     */
39
    private $class = null;
40
41
    /**
42
     * @var RecordSelector
43
     */
44
    private $selector = null;
45
46
    /**
47
     * @invisible
48
     * @var ORM
49
     */
50
    protected $orm = null;
51
52
    /**
53
     * @param string $class
54
     * @param ORM    $orm
55
     * @throws SourceException
56
     */
57 View Code Duplication
    public function __construct($class = null, ORM $orm = 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...
58
    {
59
        if (empty($class)) {
60
            if (empty(static::RECORD)) {
61
                throw new SourceException("Unable to create source without associate class.");
62
            }
63
64
            $class = static::RECORD;
65
        }
66
67
        $this->class = $class;
68
        $this->orm = $this->saturate($orm, ORM::class);
69
        $this->setSelector($this->orm->selector($this->class));
70
    }
71
72
    /**
73
     * Create new Record based on set of provided fields.
74
     *
75
     * @final Change static method of entity, not this one.
76
     * @param array $fields
77
     * @return RecordEntity
78
     */
79
    final public function create($fields = [])
80
    {
81
        //Letting entity to create itself (needed
82
        return call_user_func([$this->class, 'create'], $fields, $this->orm);
83
    }
84
85
    /**
86
     * Find record by it's primary key.
87
     *
88
     * @see findOne()
89
     * @param string|int $id Primary key value.
90
     * @return RecordEntity|null
91
     */
92
    public function findByPK($id)
93
    {
94
        return $this->find()->findByPK($id);
95
    }
96
97
    /**
98
     * Select one record from mongo collection.
99
     *
100
     * @param array $where   Where conditions in array form.
101
     * @param array $orderBy In a form of [key => direction].
102
     * @return RecordEntity|null
103
     */
104
    public function findOne(array $where = [], array $orderBy = [])
105
    {
106
        return $this->find()->orderBy($orderBy)->findOne($where);
107
    }
108
109
    /**
110
     * Get associated record selection with pre-configured query (if any).
111
     *
112
     * @param array $where Where conditions in array form.
113
     * @return RecordSelector
114
     */
115
    public function find(array $where = [])
116
    {
117
        return $this->selector()->where($where);
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function count()
124
    {
125
        return $this->find()->count();
126
    }
127
128
    /**
129
     * @return RecordSelector
130
     */
131
    final protected function selector()
132
    {
133
        //Has to be cloned every time to prevent query collisions
134
        return clone $this->selector;
135
    }
136
137
    /**
138
     * @param RecordSelector $selector
139
     */
140
    protected function setSelector(RecordSelector $selector)
141
    {
142
        $this->selector = $selector;
143
    }
144
}
145