Passed
Pull Request — master (#150)
by
unknown
06:18
created

DynamoDbQueryBuilder::saveAsync()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace BaoPham\DynamoDb;
4
5
use BaoPham\DynamoDb\Concerns\HasParsers;
6
use BaoPham\DynamoDb\ConditionAnalyzer\Analyzer;
7
use BaoPham\DynamoDb\Facades\DynamoDb;
8
use Closure;
9
use Illuminate\Contracts\Support\Arrayable;
10
use Illuminate\Database\Eloquent\ModelNotFoundException;
11
use Illuminate\Database\Eloquent\Scope;
12
13
class DynamoDbQueryBuilder
14
{
15
    use HasParsers;
16
17
    const MAX_LIMIT = -1;
18
    const DEFAULT_TO_ITERATOR = true;
19
20
    /**
21
     * The maximum number of records to return.
22
     *
23
     * @var int
24
     */
25
    public $limit;
26
27
    /**
28
     * @var array
29
     */
30
    public $wheres = [];
31
32
    /**
33
     * @var DynamoDbModel
34
     */
35
    protected $model;
36
37
    /**
38
     * @var \Aws\DynamoDb\DynamoDbClient
39
     */
40
    protected $client;
41
42
    /**
43
     * @var Closure
44
     */
45
    protected $decorator;
46
47
    /**
48
     * Applied global scopes.
49
     *
50
     * @var array
51
     */
52
    protected $scopes = [];
53
54
    /**
55
     * Removed global scopes.
56
     *
57
     * @var array
58
     */
59
    protected $removedScopes = [];
60
61
    /**
62
     * When not using the iterator, you can store the lastEvaluatedKey to
63
     * paginate through the results. The getAll method will take this into account
64
     * when used with $use_iterator = false.
65
     *
66
     * @var mixed
67
     */
68
    protected $lastEvaluatedKey;
69
70
    /**
71
     * Specified index name for the query.
72
     *
73
     * @var string
74
     */
75
    protected $index;
76
77 123
    public function __construct(DynamoDbModel $model)
78
    {
79 123
        $this->model = $model;
80 123
        $this->client = $model->getClient();
81 123
        $this->setupExpressions();
82 123
    }
83
84
    /**
85
     * Alias to set the "limit" value of the query.
86
     *
87
     * @param  int  $value
88
     * @return DynamoDbQueryBuilder
89
     */
90 6
    public function take($value)
91
    {
92 6
        return $this->limit($value);
93
    }
94
95
    /**
96
     * Set the "limit" value of the query.
97
     *
98
     * @param  int  $value
99
     * @return $this
100
     */
101 14
    public function limit($value)
102
    {
103 14
        $this->limit = $value;
104
105 14
        return $this;
106
    }
107
108
    /**
109
     * Alias to set the "offset" value of the query.
110
     *
111
     * @param  int $value
112
     * @throws NotSupportedException
113
     */
114
    public function skip($value)
115
    {
116
        return $this->offset($value);
117
    }
118
119
    /**
120
     * Set the "offset" value of the query.
121
     *
122
     * @param  int $value
123
     * @throws NotSupportedException
124
     */
125
    public function offset($value)
126
    {
127
        throw new NotSupportedException('Skip/Offset is not supported. Consider using after() instead');
128
    }
129
130
    /**
131
     * Determine the starting point (exclusively) of the query.
132
     * Unfortunately, offset of how many records to skip does not make sense for DynamoDb.
133
     * Instead, provide the last result of the previous query as the starting point for the next query.
134
     *
135
     * @param  DynamoDbModel|null  $after
136
     *   Examples:
137
     *
138
     *   For query such as
139
     *       $query = $model->where('count', 10)->limit(2);
140
     *       $last = $query->all()->last();
141
     *   Take the last item of this query result as the next "offset":
142
     *       $nextPage = $query->after($last)->limit(2)->all();
143
     *
144
     *   Alternatively, pass in nothing to reset the starting point.
145
     *
146
     * @return $this
147
     */
148 4
    public function after(DynamoDbModel $after = null)
149
    {
150 4
        if (empty($after)) {
151 4
            $this->lastEvaluatedKey = null;
152
153 4
            return $this;
154
        }
155
156 4
        $afterKey = $after->getKeys();
157
158 4
        $analyzer = $this->getConditionAnalyzer();
159
160 4
        if ($index = $analyzer->index()) {
161 1
            foreach ($index->columns() as $column) {
162 1
                $afterKey[$column] = $after->getAttribute($column);
163
            }
164
        }
165
166 4
        $this->lastEvaluatedKey = DynamoDb::marshalItem($afterKey);
167
168 4
        return $this;
169
    }
170
171
    /**
172
     * Similar to after(), but instead of using the model instance, the model's keys are used.
173
     * Use $collection->lastKey() or $model->getKeys() to retrieve the value.
174
     *
175
     * @param  Array  $key
176
     *   Examples:
177
     *
178
     *   For query such as
179
     *       $query = $model->where('count', 10)->limit(2);
180
     *       $items = $query->all();
181
     *   Take the last item of this query result as the next "offset":
182
     *       $nextPage = $query->afterKey($items->lastKey())->limit(2)->all();
183
     *
184
     *   Alternatively, pass in nothing to reset the starting point.
185
     *
186
     * @return $this
187
     */
188 4
    public function afterKey($key = null)
189
    {
190 4
        $this->lastEvaluatedKey = empty($key) ? null : DynamoDb::marshalItem($key);
191 4
        return $this;
192
    }
193
194
    /**
195
     * Set the index name manually
196
     *
197
     * @param string $index The index name
198
     * @return $this
199
     */
200 1
    public function withIndex($index)
201
    {
202 1
        $this->index = $index;
203 1
        return $this;
204
    }
205
206 74
    public function where($column, $operator = null, $value = null, $boolean = 'and')
207
    {
208
        // If the column is an array, we will assume it is an array of key-value pairs
209
        // and can add them each as a where clause. We will maintain the boolean we
210
        // received when the method was called and pass it into the nested where.
211 74
        if (is_array($column)) {
212
            foreach ($column as $key => $value) {
213
                return $this->where($key, '=', $value);
214
            }
215
        }
216
217
        // Here we will make some assumptions about the operator. If only 2 values are
218
        // passed to the method, we will assume that the operator is an equals sign
219
        // and keep going. Otherwise, we'll require the operator to be passed in.
220 74
        if (func_num_args() == 2) {
221 45
            list($value, $operator) = [$operator, '='];
222
        }
223
224
        // If the columns is actually a Closure instance, we will assume the developer
225
        // wants to begin a nested where statement which is wrapped in parenthesis.
226
        // We'll add that Closure to the query then return back out immediately.
227 74
        if ($column instanceof Closure) {
228 2
            return $this->whereNested($column, $boolean);
229
        }
230
231
        // If the given operator is not found in the list of valid operators we will
232
        // assume that the developer is just short-cutting the '=' operators and
233
        // we will set the operators to '=' and set the values appropriately.
234 74
        if (!ComparisonOperator::isValidOperator($operator)) {
235 4
            list($value, $operator) = [$operator, '='];
236
        }
237
238
        // If the value is a Closure, it means the developer is performing an entire
239
        // sub-select within the query and we will need to compile the sub-select
240
        // within the where clause to get the appropriate query record results.
241 74
        if ($value instanceof Closure) {
242
            throw new NotSupportedException('Closure in where clause is not supported');
243
        }
244
245 74
        $this->wheres[] = [
246 74
            'column' => $column,
247 74
            'type' => ComparisonOperator::getDynamoDbOperator($operator),
248 74
            'value' => $value,
249 74
            'boolean' => $boolean,
250
        ];
251
252 74
        return $this;
253
    }
254
255
    /**
256
     * Add a nested where statement to the query.
257
     *
258
     * @param  \Closure $callback
259
     * @param  string   $boolean
260
     * @return $this
261
     */
262 2
    public function whereNested(Closure $callback, $boolean = 'and')
263
    {
264 2
        call_user_func($callback, $query = $this->forNestedWhere());
265
266 2
        return $this->addNestedWhereQuery($query, $boolean);
267
    }
268
269
    /**
270
     * Create a new query instance for nested where condition.
271
     *
272
     * @return $this
273
     */
274 2
    public function forNestedWhere()
275
    {
276 2
        return $this->newQuery();
277
    }
278
279
    /**
280
     * Add another query builder as a nested where to the query builder.
281
     *
282
     * @param  DynamoDbQueryBuilder $query
283
     * @param  string  $boolean
284
     * @return $this
285
     */
286 2
    public function addNestedWhereQuery($query, $boolean = 'and')
287
    {
288 2
        if (count($query->wheres)) {
289 2
            $type = 'Nested';
290 2
            $column = null;
291 2
            $value = $query->wheres;
292 2
            $this->wheres[] = compact('column', 'type', 'value', 'boolean');
293
        }
294
295 2
        return $this;
296
    }
297
298
    /**
299
     * Add an "or where" clause to the query.
300
     *
301
     * @param  string  $column
302
     * @param  string  $operator
303
     * @param  mixed   $value
304
     * @return $this
305
     */
306 22
    public function orWhere($column, $operator = null, $value = null)
307
    {
308 22
        return $this->where($column, $operator, $value, 'or');
309
    }
310
311
    /**
312
     * Add a "where in" clause to the query.
313
     *
314
     * @param  string  $column
315
     * @param  mixed   $values
316
     * @param  string  $boolean
317
     * @param  bool    $not
318
     * @return $this
319
     * @throws NotSupportedException
320
     */
321 2
    public function whereIn($column, $values, $boolean = 'and', $not = false)
322
    {
323 2
        if ($not) {
324
            throw new NotSupportedException('"not in" is not a valid DynamoDB comparison operator');
325
        }
326
327
        // If the value is a query builder instance, not supported
328 2
        if ($values instanceof static) {
329
            throw new NotSupportedException('Value is a query builder instance');
330
        }
331
332
        // If the value of the where in clause is actually a Closure, not supported
333 2
        if ($values instanceof Closure) {
334
            throw new NotSupportedException('Value is a Closure');
335
        }
336
337
        // Next, if the value is Arrayable we need to cast it to its raw array form
338 2
        if ($values instanceof Arrayable) {
339
            $values = $values->toArray();
340
        }
341
342 2
        return $this->where($column, ComparisonOperator::IN, $values, $boolean);
343
    }
344
345
    /**
346
     * Add an "or where in" clause to the query.
347
     *
348
     * @param  string  $column
349
     * @param  mixed   $values
350
     * @return $this
351
     */
352 2
    public function orWhereIn($column, $values)
353
    {
354 2
        return $this->whereIn($column, $values, 'or');
355
    }
356
357
    /**
358
     * Add a "where null" clause to the query.
359
     *
360
     * @param  string  $column
361
     * @param  string  $boolean
362
     * @param  bool    $not
363
     * @return $this
364
     */
365 4
    public function whereNull($column, $boolean = 'and', $not = false)
366
    {
367 4
        $type = $not ? ComparisonOperator::NOT_NULL : ComparisonOperator::NULL;
368
369 4
        $this->wheres[] = compact('column', 'type', 'boolean');
370
371 4
        return $this;
372
    }
373
374
    /**
375
     * Add an "or where null" clause to the query.
376
     *
377
     * @param  string  $column
378
     * @return $this
379
     */
380 2
    public function orWhereNull($column)
381
    {
382 2
        return $this->whereNull($column, 'or');
383
    }
384
385
    /**
386
     * Add an "or where not null" clause to the query.
387
     *
388
     * @param  string  $column
389
     * @return $this
390
     */
391 2
    public function orWhereNotNull($column)
392
    {
393 2
        return $this->whereNotNull($column, 'or');
394
    }
395
396
    /**
397
     * Add a "where not null" clause to the query.
398
     *
399
     * @param  string  $column
400
     * @param  string  $boolean
401
     * @return $this
402
     */
403 2
    public function whereNotNull($column, $boolean = 'and')
404
    {
405 2
        return $this->whereNull($column, $boolean, true);
406
    }
407
408
    /**
409
     * Get a new instance of the query builder.
410
     *
411
     * @return DynamoDbQueryBuilder
412
     */
413 2
    public function newQuery()
414
    {
415 2
        return new static($this->getModel());
416
    }
417
418
    /**
419
     * Implements the Query Chunk method
420
     *
421
     * @param int $chunkSize
422
     * @param callable $callback
423
     */
424 7
    public function chunk($chunkSize, callable $callback)
425
    {
426 7
        while (true) {
427 7
            $results = $this->getAll([], $chunkSize, false);
428
429 7
            if ($results->isNotEmpty()) {
430 7
                call_user_func($callback, $results);
431
            }
432
433 7
            if (empty($this->lastEvaluatedKey)) {
434 7
                break;
435
            }
436
        }
437 7
    }
438
439
    /**
440
     * @param $id
441
     * @param array $columns
442
     * @return DynamoDbModel|\Illuminate\Database\Eloquent\Collection|null
443
     */
444 36
    public function find($id, array $columns = [])
445
    {
446 36
        if ($this->isMultipleIds($id)) {
447 4
            return $this->findMany($id, $columns);
448
        }
449
450 32
        $this->resetExpressions();
451
452 32
        $this->model->setId($id);
453
454 32
        $query = DynamoDb::table($this->model->getTable())
455 32
            ->setKey(DynamoDb::marshalItem($this->model->getKeys()))
456 32
            ->setConsistentRead(true);
457
458 32
        if (!empty($columns)) {
459
            $query
460 3
                ->setProjectionExpression($this->projectionExpression->parse($columns))
461 3
                ->setExpressionAttributeNames($this->expressionAttributeNames->all());
462
        }
463
464 32
        $item = $query->prepare($this->client)->getItem();
465
466 32
        $item = array_get($item->toArray(), 'Item');
467
468 32
        if (empty($item)) {
469 4
            return null;
470
        }
471
472 28
        $item = DynamoDb::unmarshalItem($item);
473
474 28
        $model = $this->model->newInstance([], true);
475
476 28
        $model->setRawAttributes($item, true);
477
478 28
        return $model;
479
    }
480
481
    /**
482
     * @param $ids
483
     * @param array $columns
484
     * @return \Illuminate\Database\Eloquent\Collection
485
     */
486 4
    public function findMany($ids, array $columns = [])
487
    {
488 4
        $collection = $this->model->newCollection();
489
490 4
        if (empty($ids)) {
491
            return $collection;
492
        }
493
494 4
        $this->resetExpressions();
495
496 4
        $table = $this->model->getTable();
497
498
        $keys = collect($ids)->map(function ($id) {
499 4
            if (! is_array($id)) {
500 2
                $id = [$this->model->getKeyName() => $id];
501
            }
502
503 4
            return DynamoDb::marshalItem($id);
504 4
        });
505
506 4
        $subQuery = DynamoDb::newQuery()
507 4
            ->setKeys($keys->toArray())
508 4
            ->setProjectionExpression($this->projectionExpression->parse($columns))
509 4
            ->setExpressionAttributeNames($this->expressionAttributeNames->all())
510 4
            ->prepare($this->client)
511 4
            ->query;
512
513 4
        $results = DynamoDb::newQuery()
514 4
            ->setRequestItems([$table => $subQuery])
515 4
            ->prepare($this->client)
516 4
            ->batchGetItem();
517
518 4
        foreach ($results['Responses'][$table] as $item) {
519 4
            $item = DynamoDb::unmarshalItem($item);
520 4
            $model = $this->model->newInstance([], true);
521 4
            $model->setRawAttributes($item, true);
522 4
            $collection->add($model);
523
        }
524
525 4
        return $collection;
526
    }
527
528 5
    public function findOrFail($id, $columns = [])
529
    {
530 5
        $result = $this->find($id, $columns);
531
532 5
        if ($this->isMultipleIds($id)) {
533 1
            if (count($result) == count(array_unique($id))) {
534 1
                return $result;
535
            }
536 4
        } elseif (! is_null($result)) {
537 2
            return $result;
538
        }
539
540 2
        throw (new ModelNotFoundException)->setModel(
541 2
            get_class($this->model),
542 2
            $id
543
        );
544
    }
545
546 16
    public function first($columns = [])
547
    {
548 16
        $items = $this->getAll($columns, 1);
549
550 16
        return $items->first();
551
    }
552
553 6
    public function firstOrFail($columns = [])
554
    {
555 6
        if (! is_null($model = $this->first($columns))) {
556 4
            return $model;
557
        }
558
559 2
        throw (new ModelNotFoundException)->setModel(get_class($this->model));
560
    }
561
562
    /**
563
     * Remove attributes from an existing item
564
     *
565
     * @param array ...$attributes
566
     * @return bool
567
     * @throws InvalidQuery
568
     */
569 6
    public function removeAttribute(...$attributes)
570
    {
571 6
        $keySet = !empty(array_filter($this->model->getKeys()));
572
573 6
        if (!$keySet) {
574 4
            $analyzer = $this->getConditionAnalyzer();
575
576 4
            if (!$analyzer->isExactSearch()) {
577
                throw new InvalidQuery('Need to provide the key in your query');
578
            }
579
580 4
            $id = $analyzer->identifierConditionValues();
581 4
            $this->model->setId($id);
582
        }
583
584 6
        $key = DynamoDb::marshalItem($this->model->getKeys());
585
586 6
        $this->resetExpressions();
587
588 6
        $result = DynamoDb::table($this->model->getTable())
589 6
            ->setKey($key)
590 6
            ->setUpdateExpression($this->updateExpression->remove($attributes))
591 6
            ->setExpressionAttributeNames($this->expressionAttributeNames->all())
592 6
            ->prepare($this->client)
593 6
            ->updateItem();
594
595 6
        return array_get($result, '@metadata.statusCode') === 200;
596
    }
597
598 2
    public function delete()
599
    {
600 2
        $result = DynamoDb::table($this->model->getTable())
601 2
            ->setKey(DynamoDb::marshalItem($this->model->getKeys()))
602 2
            ->prepare($this->client)
603 2
            ->deleteItem();
604
605 2
        return array_get($result->toArray(), '@metadata.statusCode') === 200;
606
    }
607
608 8
    public function save()
609
    {
610 8
        $result = DynamoDb::table($this->model->getTable())
611 8
            ->setItem(DynamoDb::marshalItem($this->model->getAttributes()))
612 8
            ->prepare($this->client)
613 8
            ->putItem();
614
615 8
        return array_get($result, '@metadata.statusCode') === 200;
616
    }
617
618 4
    public function saveAsync()
619
    {
620 4
        $promise = DynamoDb::table($this->model->getTable())
621 4
            ->setItem(DynamoDb::marshalItem($this->model->getAttributes()))
622 4
            ->prepare($this->client)
623 4
            ->putItemAsync();
624
625 4
        return $promise;
626
    }
627
628 55
    public function get($columns = [])
629
    {
630 55
        return $this->all($columns);
631
    }
632
633 67
    public function all($columns = [])
634
    {
635 67
        $limit = isset($this->limit) ? $this->limit : static::MAX_LIMIT;
636 67
        return $this->getAll($columns, $limit, !isset($this->limit));
637
    }
638
639 4
    public function count()
640
    {
641 4
        $limit = isset($this->limit) ? $this->limit : static::MAX_LIMIT;
642 4
        $raw = $this->toDynamoDbQuery(['count(*)'], $limit);
643
644 4
        if ($raw->op === 'Scan') {
645 4
            $res = $this->client->scan($raw->query);
646
        } else {
647
            $res = $this->client->query($raw->query);
648
        }
649
650 4
        return $res['Count'];
651
    }
652
653 4
    public function decorate(Closure $closure)
654
    {
655 4
        $this->decorator = $closure;
656 4
        return $this;
657
    }
658
659 86
    protected function getAll(
660
        $columns = [],
661
        $limit = DynamoDbQueryBuilder::MAX_LIMIT,
662
        $useIterator = DynamoDbQueryBuilder::DEFAULT_TO_ITERATOR
663
    ) {
664 86
        $analyzer = $this->getConditionAnalyzer();
665
666 86
        if ($analyzer->isExactSearch()) {
667 7
            $item = $this->find($analyzer->identifierConditionValues(), $columns);
668
669 7
            return $this->getModel()->newCollection([$item]);
670
        }
671
672 80
        $raw = $this->toDynamoDbQuery($columns, $limit);
673
674 80
        if ($useIterator) {
675 65
            $iterator = $this->client->getIterator($raw->op, $raw->query);
676
677 65
            if (isset($raw->query['Limit'])) {
678 12
                $iterator = new \LimitIterator($iterator, 0, $raw->query['Limit']);
679
            }
680
        } else {
681 19
            if ($raw->op === 'Scan') {
682 16
                $res = $this->client->scan($raw->query);
683
            } else {
684 3
                $res = $this->client->query($raw->query);
685
            }
686
687 19
            $this->lastEvaluatedKey = array_get($res, 'LastEvaluatedKey');
688 19
            $iterator = $res['Items'];
689
        }
690
691 80
        $results = [];
692
693 80
        foreach ($iterator as $item) {
694 80
            $item = DynamoDb::unmarshalItem($item);
695 80
            $model = $this->model->newInstance([], true);
696 80
            $model->setRawAttributes($item, true);
697 80
            $results[] = $model;
698
        }
699
700 80
        return $this->getModel()->newCollection($results, $analyzer->index());
701
    }
702
703
    /**
704
     * Return the raw DynamoDb query
705
     *
706
     * @param array $columns
707
     * @param int $limit
708
     * @return RawDynamoDbQuery
709
     */
710 90
    public function toDynamoDbQuery(
711
        $columns = [],
712
        $limit = DynamoDbQueryBuilder::MAX_LIMIT
713
    ) {
714 90
        $this->applyScopes();
715
716 90
        $this->resetExpressions();
717
718 90
        $op = 'Scan';
719 90
        $queryBuilder = DynamoDb::table($this->model->getTable());
720
721 90
        if (! empty($this->wheres)) {
722 70
            $analyzer = $this->getConditionAnalyzer();
723
724 70
            if ($keyConditions = $analyzer->keyConditions()) {
725 16
                $op = 'Query';
726 16
                $queryBuilder->setKeyConditionExpression($this->keyConditionExpression->parse($keyConditions));
727
            }
728
729 70
            if ($filterConditions = $analyzer->filterConditions()) {
730 61
                $queryBuilder->setFilterExpression($this->filterExpression->parse($filterConditions));
731
            }
732
733 70
            if ($index = $analyzer->index()) {
734 8
                $queryBuilder->setIndexName($index->name);
735
            }
736
        }
737
738 90
        if ($this->index) {
739
            // If user specifies the index manually, respect that
740 1
            $queryBuilder->setIndexName($this->index);
741
        }
742
743 90
        if ($limit !== static::MAX_LIMIT) {
744 31
            $queryBuilder->setLimit($limit);
745
        }
746
747 90
        if (!empty($columns)) {
748
            // Either we try to get the count or specific columns
749 8
            if ($columns == ['count(*)']) {
750 6
                $queryBuilder->setSelect('COUNT');
751
            } else {
752 2
                $queryBuilder->setProjectionExpression($this->projectionExpression->parse($columns));
753
            }
754
        }
755
756 90
        if (!empty($this->lastEvaluatedKey)) {
757 15
            $queryBuilder->setExclusiveStartKey($this->lastEvaluatedKey);
758
        }
759
760
        $queryBuilder
761 90
            ->setExpressionAttributeNames($this->expressionAttributeNames->all())
762 90
            ->setExpressionAttributeValues($this->expressionAttributeValues->all());
763
764 90
        $raw = new RawDynamoDbQuery($op, $queryBuilder->prepare($this->client)->query);
765
766 90
        if ($this->decorator) {
767 4
            call_user_func($this->decorator, $raw);
768
        }
769
770 90
        return $raw;
771
    }
772
773
    /**
774
     * @return Analyzer
775
     */
776 95
    protected function getConditionAnalyzer()
777
    {
778 95
        return with(new Analyzer)
779 95
            ->on($this->model)
780 95
            ->withIndex($this->index)
781 95
            ->analyze($this->wheres);
782
    }
783
784 36
    protected function isMultipleIds($id)
785
    {
786 36
        $keys = collect($this->model->getKeyNames());
787
788
        // could be ['id' => 'foo'], ['id1' => 'foo', 'id2' => 'bar']
789
        $single = $keys->first(function ($name) use ($id) {
790 36
            return !isset($id[$name]);
791 36
        }) === null;
792
793 36
        if ($single) {
794 20
            return false;
795
        }
796
797
        // could be ['foo', 'bar'], [['id1' => 'foo', 'id2' => 'bar'], ...]
798 16
        return $this->model->hasCompositeKey() ? is_array(array_first($id)) : is_array($id);
799
    }
800
801
    /**
802
     * @return DynamoDbModel
803
     */
804 86
    public function getModel()
805
    {
806 86
        return $this->model;
807
    }
808
809
    /**
810
     * @return \Aws\DynamoDb\DynamoDbClient
811
     */
812
    public function getClient()
813
    {
814
        return $this->client;
815
    }
816
817
    /**
818
     * Register a new global scope.
819
     *
820
     * @param  string  $identifier
821
     * @param  \Illuminate\Database\Eloquent\Scope|\Closure  $scope
822
     * @return $this
823
     */
824 7
    public function withGlobalScope($identifier, $scope)
825
    {
826 7
        $this->scopes[$identifier] = $scope;
827
828 7
        if (method_exists($scope, 'extend')) {
829
            $scope->extend($this);
830
        }
831
832 7
        return $this;
833
    }
834
835
    /**
836
     * Remove a registered global scope.
837
     *
838
     * @param  \Illuminate\Database\Eloquent\Scope|string  $scope
839
     * @return $this
840
     */
841 3
    public function withoutGlobalScope($scope)
842
    {
843 3
        if (! is_string($scope)) {
844
            $scope = get_class($scope);
845
        }
846
847 3
        unset($this->scopes[$scope]);
848
849 3
        $this->removedScopes[] = $scope;
850
851 3
        return $this;
852
    }
853
854
    /**
855
     * Remove all or passed registered global scopes.
856
     *
857
     * @param  array|null  $scopes
858
     * @return $this
859
     */
860 5
    public function withoutGlobalScopes(array $scopes = null)
861
    {
862 5
        if (is_array($scopes)) {
863
            foreach ($scopes as $scope) {
864
                $this->withoutGlobalScope($scope);
865
            }
866
        } else {
867 5
            $this->scopes = [];
868
        }
869
870 5
        return $this;
871
    }
872
873
    /**
874
     * Get an array of global scopes that were removed from the query.
875
     *
876
     * @return array
877
     */
878
    public function removedScopes()
879
    {
880
        return $this->removedScopes;
881
    }
882
883
    /**
884
     * Apply the scopes to the Eloquent builder instance and return it.
885
     *
886
     * @return DynamoDbQueryBuilder
887
     */
888 90
    public function applyScopes()
889
    {
890 90
        if (! $this->scopes) {
891 89
            return $this;
892
        }
893
894 3
        $builder = $this;
895
896 3
        foreach ($builder->scopes as $identifier => $scope) {
897 3
            if (! isset($builder->scopes[$identifier])) {
898
                continue;
899
            }
900
901
            $builder->callScope(function (DynamoDbQueryBuilder $builder) use ($scope) {
902
                // If the scope is a Closure we will just go ahead and call the scope with the
903
                // builder instance. The "callScope" method will properly group the clauses
904
                // that are added to this query so "where" clauses maintain proper logic.
905 3
                if ($scope instanceof Closure) {
906 3
                    $scope($builder);
907
                }
908
909
                // If the scope is a scope object, we will call the apply method on this scope
910
                // passing in the builder and the model instance. After we run all of these
911
                // scopes we will return back the builder instance to the outside caller.
912 3
                if ($scope instanceof Scope) {
913
                    throw new NotSupportedException('Scope object is not yet supported');
914
                }
915 3
            });
916
917 3
            $builder->withoutGlobalScope($identifier);
918
        }
919
920 3
        return $builder;
921
    }
922
923
    /**
924
     * Apply the given scope on the current builder instance.
925
     *
926
     * @param  callable  $scope
927
     * @param  array  $parameters
928
     * @return mixed
929
     */
930 7
    protected function callScope(callable $scope, $parameters = [])
931
    {
932 7
        array_unshift($parameters, $this);
933
934
        // $query = $this->getQuery();
935
936
        // // We will keep track of how many wheres are on the query before running the
937
        // // scope so that we can properly group the added scope constraints in the
938
        // // query as their own isolated nested where statement and avoid issues.
939
        // $originalWhereCount = is_null($query->wheres)
940
        //             ? 0 : count($query->wheres);
941
942 7
        $result = $scope(...array_values($parameters)) ?: $this;
943
944
        // if (count((array) $query->wheres) > $originalWhereCount) {
945
        //     $this->addNewWheresWithinGroup($query, $originalWhereCount);
946
        // }
947
948 7
        return $result;
949
    }
950
951
    /**
952
     * Dynamically handle calls into the query instance.
953
     *
954
     * @param  string  $method
955
     * @param  array  $parameters
956
     * @return mixed
957
     */
958 7
    public function __call($method, $parameters)
959
    {
960 7
        if (method_exists($this->model, $scope = 'scope'.ucfirst($method))) {
961 5
            return $this->callScope([$this->model, $scope], $parameters);
962
        }
963
964 2
        return $this;
965
    }
966
}
967