Completed
Push — master ( ab4a72...99f0f9 )
by Sergey
05:20
created

ElasticsearchModel::search()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Isswp101\Persimmon;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Collection;
7
use Isswp101\Persimmon\Collection\ElasticsearchCollection;
8
use Isswp101\Persimmon\DAL\ElasticsearchDAL;
9
use Isswp101\Persimmon\DAL\IDAL;
10
use Isswp101\Persimmon\Exceptions\ModelNotFoundException;
11
use Isswp101\Persimmon\QueryBuilder\QueryBuilder;
12
use Isswp101\Persimmon\Relationship\BelongsToRelationship;
13
use Isswp101\Persimmon\Relationship\HasManyRelationship;
14
use Isswp101\Persimmon\Traits\Elasticsearchable;
15
use Isswp101\Persimmon\Traits\Paginationable;
16
use Isswp101\Persimmon\Traits\Relationshipable;
17
18
class ElasticsearchModel extends Model
19
{
20
    use Elasticsearchable, Paginationable, Relationshipable;
21
22
    /**
23
     * @var ElasticsearchDAL
24
     */
25
    public $_dal;
26
27 54
    public function __construct(IDAL $dal, array $attributes = [])
28
    {
29 54
        $this->validateModelEndpoint();
30
31 52
        parent::__construct($dal, $attributes);
32 52
    }
33
34 4
    public static function findWithParentId($id, $parent, array $columns = ['*'])
35
    {
36 4
        $model = static::find($id, $columns, ['parent' => $parent]);
37
38 4
        if ($model) {
39 4
            $model->setParentId($parent);
40 4
        }
41
42 4
        return $model;
43
    }
44
45
    /**
46
     * Execute the query and get the result.
47
     *
48
     * @param QueryBuilder|array $query
49
     * @return ElasticsearchCollection|static[]
50
     */
51 22
    public static function search($query = [])
52
    {
53 22
        if ($query instanceof QueryBuilder) {
54 14
            $query = $query->build();
55 14
        }
56 22
        $model = static::createInstance();
57 22
        return $model->_dal->search($query);
58
    }
59
60
    /**
61
     * Execute the query and get the first result.
62
     *
63
     * @param QueryBuilder|array $query
64
     * @return static
65
     */
66 4
    public static function first($query = [])
67
    {
68 2
        if ($query instanceof QueryBuilder) {
69
            $query = $query->build();
70
        }
71 2
        $query['from'] = 0;
72 4
        $query['size'] = 1;
73 2
        return static::search($query)->first();
74
    }
75
76
    /**
77
     * Execute the query and get the first result or throw an exception.
78
     *
79
     * @param QueryBuilder|array $query
80
     * @throws ModelNotFoundException
81
     * @return static
82
     */
83
    public static function firstOrFail($query = [])
84
    {
85
        $model = static::first($query);
86
        if (is_null($model)) {
87
            throw new ModelNotFoundException(get_called_class());
88
        }
89
        return $model;
90
    }
91
92
    /**
93
     * Apply the callback to the documents of the given query.
94
     *
95
     * @param QueryBuilder|array $query
96
     * @param callable $callback
97
     * @param int $limit
98
     * @return int hits.total
99
     */
100 6
    public static function map($query = [], callable $callback = null, $limit = -1)
101
    {
102 6
        if ($query instanceof QueryBuilder) {
103
            $query = $query->build();
104
        }
105
106 4
        $query['from'] = Arr::get($query, 'from', 0);
107 4
        $query['size'] = Arr::get($query, 'size', 50);
108
109 4
        $i = 0;
110 4
        $models = static::search($query);
111 4
        $total = $models->getTotal();
112 4
        while ($models) {
113 4
            foreach ($models as $model) {
114 4
                if ($callback) {
115 4
                    $callback($model);
116 4
                }
117 4
                $i++;
118 4
            }
119
120 4
            $query['from'] += $query['size'];
121
122 4
            if ($i >= $total || ($limit > 0 && $i >= $limit)) {
123 4
                break;
124
            }
125
126
            $models = static::search($query);
127
        }
128
129 4
        return $total;
130
    }
131
132
    /**
133
     * Execute the query and get all items.
134
     *
135
     * @param QueryBuilder|array $query
136
     * @return Collection
137
     */
138 2
    public static function all($query = [])
139
    {
140 2
        if ($query instanceof QueryBuilder) {
141
            $query = $query->build();
142
        }
143 2
        $collection = collect();
144 2
        static::map($query, function (ElasticsearchModel $document) use ($collection) {
145 2
            $collection->put($document->getId(), $document);
146 2
        });
147 2
        return $collection;
148
    }
149
150 6
    protected function belongsTo($class)
151
    {
152 6
        return new BelongsToRelationship($this, $class);
153
    }
154
155 4
    protected function hasMany($class)
156
    {
157 4
        return new HasManyRelationship($this, $class);
158
    }
159
}
160