SourceTrait::findOne()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Spiral\ODM\Traits;
10
11
use Interop\Container\ContainerInterface;
12
use Spiral\Core\Component;
13
use Spiral\Core\Exceptions\ScopeException;
14
use Spiral\ODM\CompositableInterface;
15
use Spiral\ODM\Document;
16
use Spiral\ODM\Entities\DocumentSelector;
17
use Spiral\ODM\Entities\DocumentSource;
18
use Spiral\ODM\ODMInterface;
19
20
/**
21
 * Static record functionality including create and find methods.
22
 */
23
trait SourceTrait
24
{
25
    /**
26
     * Find multiple documents based on provided query.
27
     *
28
     * Example:
29
     * User::find(['status' => 'active'];
30
     *
31
     * @param array $query Selection WHERE statement.
32
     *
33
     * @return DocumentSelector
34
     *
35
     * @throws ScopeException
36
     */
37
    public static function find(array $query = []): DocumentSelector
38
    {
39
        return static::source()->find($query);
40
    }
41
42
    /**
43
     * Fetch one record based on provided query or return null. Make sure to specify sort by in
44
     * order to stabilize selection
45
     *
46
     * Example:
47
     * User::findOne(['name' => 'Wolfy-J'], ['id' => -1]);
48
     *
49
     * @param array $where  Selection WHERE statement.
50
     * @param array $sortBy Sort by.
51
     *
52
     * @return CompositableInterface|Document|null
53
     *
54
     * @throws ScopeException
55
     */
56
    public static function findOne($where = [], array $sortBy = [])
57
    {
58
        return static::source()->findOne($where, $sortBy);
59
    }
60
61
    /**
62
     * Find record using it's primary key.
63
     *
64
     * Example:
65
     * User::findByOK(1);
66
     *
67
     * @param mixed $primaryKey Primary key.
68
     *
69
     * @return CompositableInterface|Document|null
70
     *
71
     * @throws ScopeException
72
     */
73
    public static function findByPK($primaryKey)
74
    {
75
        return static::source()->findByPK($primaryKey);
76
    }
77
78
    /**
79
     * Instance of ODM Selector associated with specific document.
80
     *
81
     * @see   Component::staticContainer()
82
     **
83
     * @return DocumentSource
84
     *
85
     * @throws ScopeException
86
     */
87
    public static function source(): DocumentSource
88
    {
89
        /**
90
         * Container to be received via global scope.
91
         *
92
         * @var ContainerInterface $container
93
         */
94
        //Via global scope
95
        $container = self::staticContainer();
96
97
        if (empty($container)) {
98
            //Via global scope
99
            throw new ScopeException(sprintf(
100
                "Unable to get '%s' source, no container scope is available",
101
                static::class
102
            ));
103
        }
104
105
        return $container->get(ODMInterface::class)->source(static::class);
106
    }
107
108
    /**
109
     * Trait can ONLY be added to components.
110
     *
111
     * @see Component
112
     *
113
     * @param ContainerInterface|null $container
114
     *
115
     * @return ContainerInterface|null
116
     */
117
    abstract protected static function staticContainer(ContainerInterface $container = null);
118
}