Model::isNested()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Spinen\Halo\Support;
4
5
use ArrayAccess;
6
use GuzzleHttp\Exception\GuzzleException;
7
use Illuminate\Contracts\Support\Arrayable;
8
use Illuminate\Contracts\Support\Jsonable;
9
use Illuminate\Database\Eloquent\Concerns\HasAttributes;
10
use Illuminate\Database\Eloquent\Concerns\HasTimestamps;
11
use Illuminate\Database\Eloquent\Concerns\HidesAttributes;
12
use Illuminate\Database\Eloquent\JsonEncodingException;
13
use Illuminate\Support\Carbon;
14
use Illuminate\Support\Facades\Date;
15
use Illuminate\Support\Str;
16
use Illuminate\Support\Traits\Conditionable;
17
use JsonSerializable;
18
use LogicException;
19
use Spinen\Halo\Concerns\HasClient;
20
use Spinen\Halo\Exceptions\InvalidRelationshipException;
21
use Spinen\Halo\Exceptions\ModelNotFoundException;
22
use Spinen\Halo\Exceptions\ModelReadonlyException;
23
use Spinen\Halo\Exceptions\NoClientException;
24
use Spinen\Halo\Exceptions\TokenException;
25
use Spinen\Halo\Exceptions\UnableToSaveException;
26
use Spinen\Halo\Support\Relations\BelongsTo;
27
use Spinen\Halo\Support\Relations\ChildOf;
28
use Spinen\Halo\Support\Relations\HasMany;
29
use Spinen\Halo\Support\Relations\Relation;
30
31
/**
32
 * Class Model
33
 *
34
 * NOTE: Since we are trying to give a Laravel like feel when interacting
35
 * with the API, there are sections of this code that is very heavily
36
 * patterned/inspired directly from Laravel's Model class.
37
 */
38
abstract class Model implements Arrayable, ArrayAccess, Jsonable, JsonSerializable
39
{
40
    use Conditionable;
41
    use HasAttributes {
0 ignored issues
show
introduced by
The trait Illuminate\Database\Eloq...\Concerns\HasAttributes requires some properties which are not provided by Spinen\Halo\Support\Model: $preventsLazyLoading, $name, $map, $set, $withoutObjectCaching, $withCaching, $withObjectCaching, $get, $value
Loading history...
42
        asDateTime as originalAsDateTime;
43
    }
44
    use HasClient;
45
    use HasTimestamps;
46
    use HidesAttributes;
47
48
    /**
49
     * Default wheres to send.  They are overwrote by any matching where calls.
50
     */
51
    public array $defaultWheres = [];
52
53
    /**
54
     * Indicates if the model exists.
55
     */
56
    public bool $exists = false;
57
58
    /**
59
     * Indicates if the IDs are auto-incrementing.
60
     */
61
    public bool $incrementing = false;
62
63
    /**
64
     * The "type" of the primary key ID.
65
     */
66
    protected string $keyType = 'int';
67
68
    /**
69
     * Is resource nested behind parentModel
70
     *
71
     * Several of the endpoints are nested behind another model for relationship, but then to
72
     * interact with the specific model, then are not nested.  This property will know when to
73
     * keep the specific model nested.
74
     */
75
    protected bool $nested = false;
76
77
    /**
78
     * Parameter for order by direction
79
     *
80
     * Default is "$orderByParameter . 'desc'"
81
     */
82
    protected ?string $orderByDirectionParameter = null;
83
84
    /**
85
     * Parameter for order by column
86
     */
87
    protected string $orderByParameter = 'order';
88
89
    /**
90
     * Optional parentModel instance
91
     */
92
    public ?Model $parentModel;
93
94
    /**
95
     * Path to API endpoint.
96
     */
97
    protected string $path;
98
99
    /**
100
     * The primary key for the model.
101
     */
102
    protected string $primaryKey = 'id';
103
104
    /**
105
     * Is the model readonly?
106
     */
107
    protected bool $readonlyModel = false;
108
109
    /**
110
     * The loaded relationships for the model.
111
     */
112
    protected array $relations = [];
113
114
    /**
115
     * Some of the responses have the collections under a property
116
     */
117
    protected ?string $responseCollectionKey = null;
118
119
    /**
120
     * Some of the responses have the data under a property
121
     */
122
    protected ?string $responseKey = null;
123
124
    /**
125
     * Are timestamps in milliseconds?
126
     */
127
    protected bool $timestampsInMilliseconds = true;
128
129
    /**
130
     * Indicates if the model was inserted during the current request lifecycle.
131
     *
132
     * @var bool
133
     */
134
    public $wasRecentlyCreated = false;
135
136
    /**
137
     * The name of the "created at" column.
138
     *
139
     * @var string
140
     */
141
    const CREATED_AT = null;
142
143
    /**
144
     * The name of the "updated at" column.
145
     *
146
     * @var string
147
     */
148
    const UPDATED_AT = null;
149
150
    /**
151
     * Model constructor.
152
     */
153 108
    public function __construct(?array $attributes = [], Model $parentModel = null)
154
    {
155
        // All dates from API comes as epoch with milliseconds
156 108
        $this->dateFormat = 'Uv';
157
        // None of these models will use timestamps, but need the date casting
158 108
        $this->timestamps = false;
159
160 108
        $this->syncOriginal();
161
162 108
        $this->fill($attributes);
163 108
        $this->parentModel = $parentModel;
164
    }
165
166
    /**
167
     * Dynamically retrieve attributes on the model.
168
     */
169 25
    public function __get(string $key)
170
    {
171 25
        return $this->getAttribute($this->keyMap($key));
172
    }
173
174
    /**
175
     * Determine if an attribute or relation exists on the model.
176
     */
177 2
    public function __isset(string $key): bool
178
    {
179 2
        return $this->offsetExists($this->keyMap($key));
180
    }
181
182
    /**
183
     * Dynamically set attributes on the model.
184
     *
185
     * @param  string  $key
186
     * @return void
187
     *
188
     * @throws ModelReadonlyException
189
     */
190 13
    public function __set($key, $value)
191
    {
192 13
        if ($this->readonlyModel) {
193 1
            throw new ModelReadonlyException();
194
        }
195
196 12
        $this->setAttribute($this->keyMap($key), $value);
197
    }
198
199
    /**
200
     * Convert the model to its string representation.
201
     *
202
     * @return string
203
     */
204 1
    public function __toString()
205
    {
206 1
        return $this->toJson();
207
    }
208
209
    /**
210
     * Unset an attribute on the model.
211
     *
212
     * @param  string  $key
213
     * @return void
214
     */
215 2
    public function __unset($key)
216
    {
217 2
        $this->offsetUnset($this->keyMap($key));
218
    }
219
220
    /**
221
     * Return a timestamp as DateTime object.
222
     *
223
     * @return Carbon
224
     */
225 8
    protected function asDateTime($value)
226
    {
227 8
        if (is_numeric($value) && $this->timestampsInMilliseconds) {
228 1
            return Date::createFromTimestampMs($value);
229
        }
230
231 7
        return $this->originalAsDateTime($value);
232
    }
233
234
    /**
235
     * Assume foreign key
236
     *
237
     * @param  string  $related
238
     */
239 2
    protected function assumeForeignKey($related): string
240
    {
241 2
        return Str::snake((new $related())->getResponseKey()).'_id';
242
    }
243
244
    /**
245
     * Relationship that makes the model belongs to another model
246
     *
247
     * @param  string  $related
248
     * @param  string|null  $foreignKey
249
     *
250
     * @throws InvalidRelationshipException
251
     * @throws ModelNotFoundException
252
     * @throws NoClientException
253
     */
254 2
    public function belongsTo($related, $foreignKey = null): BelongsTo
255
    {
256 2
        $foreignKey = $foreignKey ?? $this->assumeForeignKey($related);
257
258 2
        $builder = (new Builder())->setClass($related)
259 2
                                  ->setClient($this->getClient());
260
261 2
        return new BelongsTo($builder, $this, $foreignKey);
262
    }
263
264
    /**
265
     * Relationship that makes the model child to another model
266
     *
267
     * @param  string  $related
268
     * @param  string|null  $foreignKey
269
     *
270
     * @throws InvalidRelationshipException
271
     * @throws ModelNotFoundException
272
     * @throws NoClientException
273
     */
274 2
    public function childOf($related, $foreignKey = null): ChildOf
275
    {
276 2
        $foreignKey = $foreignKey ?? $this->assumeForeignKey($related);
277
278 2
        $builder = (new Builder())->setClass($related)
279 2
                                  ->setClient($this->getClient())
280 2
                                  ->setParent($this);
281
282 2
        return new ChildOf($builder, $this, $foreignKey);
283
    }
284
285
    /**
286
     * Convert boolean to a string as their API expects "true"/"false
287
     */
288 18
    protected function convertBoolToString(mixed $value): mixed
289
    {
290 18
        return match (true) {
291 18
            is_array($value) => array_map([$this, 'convertBoolToString'], $value),
292 18
            is_bool($value) => $value ? 'true' : 'false',
293 18
            default => $value,
294 18
        };
295
    }
296
297
    /**
298
     * Delete the model from Halo
299
     *
300
     * @throws NoClientException
301
     * @throws TokenException
302
     */
303 3
    public function delete(): bool
304
    {
305
        // TODO: Make sure that the model supports being deleted
306 3
        if ($this->readonlyModel) {
307 1
            return false;
308
        }
309
310
        try {
311 2
            $this->getClient()
312 2
                 ->delete($this->getPath($this->getKey()));
313
314 1
            return true;
315 1
        } catch (GuzzleException $e) {
316
            // TODO: Do something with the error
317
318 1
            return false;
319
        }
320
    }
321
322
    /**
323
     * Fill the model with the supplied properties
324
     */
325 108
    public function fill(?array $attributes = []): self
326
    {
327 108
        foreach ((array) $attributes as $attribute => $value) {
328 59
            $this->setAttribute($attribute, $value);
329
        }
330
331 108
        return $this;
332
    }
333
334
    /**
335
     * Merge any where in the defaultWheres property with any passed in.
336
     */
337 57
    public function getDefaultWheres(array $query = []): array
338
    {
339 57
        return [
340 57
            ...$this->defaultWheres,
341 57
            ...$query,
342 57
        ];
343
    }
344
345
    /**
346
     * Get the value indicating whether the IDs are incrementing.
347
     */
348 105
    public function getIncrementing(): bool
349
    {
350 105
        return $this->incrementing;
351
    }
352
353
    /**
354
     * Get the value of the model's primary key.
355
     */
356 58
    public function getKey()
357
    {
358 58
        return $this->getAttribute($this->getKeyName());
359
    }
360
361
    /**
362
     * Get the primary key for the model.
363
     */
364 63
    public function getKeyName(): string
365
    {
366 63
        return $this->primaryKey;
367
    }
368
369
    /**
370
     * Get the auto-incrementing key type.
371
     */
372 1
    public function getKeyType(): string
373
    {
374 1
        return $this->keyType;
375
    }
376
377
    /**
378
     * Get the parameter the endpoint uses to sort.
379
     */
380 2
    public function getOrderByDirectionParameter(): string
381
    {
382 2
        return $this->orderByDirectionParameter ?? $this->getOrderByParameter().'desc';
383
    }
384
385
    /**
386
     * Get the parameter the endpoint uses to sort.
387
     */
388 2
    public function getOrderByParameter(): string
389
    {
390 2
        return $this->orderByParameter;
391
    }
392
393
    /**
394
     * Build API path
395
     *
396
     * Put anything on the end of the URI that is passed in
397
     *
398
     * @param  string|null  $extra
399
     * @param  array|null  $query
400
     * @return string
401
     */
402 57
    public function getPath($extra = null, array $query = []): ?string
403
    {
404
        // Start with path to resource without "/" on end
405 57
        $path = rtrim($this->path, '/');
406
407
        // If have an id, then put it on the end
408 57
        // NOTE: Halo treats creates & updates the same, so only on existing
409 5
        if ($this->exist && $this->getKey()) {
0 ignored issues
show
Bug Best Practice introduced by
The property exist does not exist on Spinen\Halo\Support\Model. Since you implemented __get, consider adding a @property annotation.
Loading history...
410
            $path .= '/'.$this->getKey();
411
        }
412
413 57
        // Stick any extra things on the end
414 6
        if (! is_null($extra)) {
415
            $path .= '/'.ltrim($extra, '/');
416
        }
417 57
418 18
        if (! empty($query = $this->getDefaultWheres($query))) {
419
            // NOTE: Halo does not accept the index in array of properties, so removing them
420
            //       ?priority%5B0%5D=4&priority%5B1%5D=3 becomes priority=4&priority=3
421
            $path .= '?'.preg_replace('/%5B\d+%5D=/', '=', http_build_query($this->convertBoolToString($query)));
422 57
        }
423 4
424
        // If there is a parentModel & not have an id (unless for nested), then prepend parentModel
425
        if (! is_null($this->parentModel) && (! $this->getKey() || $this->isNested())) {
426 57
            return $this->parentModel->getPath($path);
427
        }
428
429
        return $path;
430
    }
431
432
    /**
433
     * Get a relationship value from a method.
434
     *
435
     * @param  string  $method
436 5
     *
437
     * @throws LogicException
438 5
     */
439
    public function getRelationshipFromMethod($method)
440 5
    {
441 2
        $relation = $this->{$method}();
442 1
443 1
        if (! $relation instanceof Relation) {
444
            $exception_message = is_null($relation)
445 2
                ? '%s::%s must return a relationship instance, but "null" was returned. Was the "return" keyword used?'
446 2
                : '%s::%s must return a relationship instance.';
447 2
448
            throw new LogicException(
449
                sprintf($exception_message, static::class, $method)
450 3
            );
451 3
        }
452 3
453 3
        return tap(
454 3
            $relation->getResults(),
455 3
            function ($results) use ($method) {
456
                $this->setRelation($method, $results);
457
            }
458
        );
459
    }
460
461
    /**
462
     * Name of the wrapping key when response is a collection
463 32
     *
464
     * If none provided, assume plural version responseKey
465 32
     */
466
    public function getResponseCollectionKey(): ?string
467
    {
468
        return $this->responseCollectionKey ?? Str::plural($this->getResponseKey());
469
    }
470
471
    /**
472
     * Name of the wrapping key of response
473 36
     *
474
     * If none provided, assume camelCase of class name
475 36
     */
476
    public function getResponseKey(): ?string
477
    {
478
        return $this->responseKey ?? Str::camel(class_basename(static::class));
479
    }
480
481
    /**
482
     * Many of the results include collection of related data, so cast it
483
     *
484
     * @param  string  $related
485
     * @param  array  $given
486
     * @param  bool  $reset Some of the values are nested under a property, so peel it off
487 1
     *
488
     * @throws NoClientException
489
     */
490 1
    public function givenMany($related, $given, $reset = false): Collection
491
    {
492 1
        /** @var Model $model */
493 1
        $model = (new $related([], $this->parentModel))->setClient($this->getClient());
494 1
495 1
        return (new Collection($given))->map(
0 ignored issues
show
Bug introduced by
$given of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Spinen\Halo\Support\Collection::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

495
        return (new Collection(/** @scrutinizer ignore-type */ $given))->map(
Loading history...
496 1
            function ($attributes) use ($model, $reset) {
497
                return $model->newFromBuilder($reset ? reset($attributes) : $attributes);
498
            }
499
        );
500
    }
501
502
    /**
503
     * Many of the results include related data, so cast it to object
504
     *
505
     * @param  string  $related
506
     * @param  array  $attributes
507
     * @param  bool  $reset Some of the values are nested under a property, so peel it off
508 1
     *
509
     * @throws NoClientException
510 1
     */
511 1
    public function givenOne($related, $attributes, $reset = false): Model
512
    {
513
        return (new $related([], $this->parentModel))->setClient($this->getClient())
514
                                                     ->newFromBuilder($reset ? reset($attributes) : $attributes);
515
    }
516
517
    /**
518
     * Relationship that makes the model have a collection of another model
519
     *
520
     * @param  string  $related
521
     *
522
     * @throws InvalidRelationshipException
523 4
     * @throws ModelNotFoundException
524
     * @throws NoClientException
525 4
     */
526 4
    public function hasMany($related): HasMany
527 4
    {
528
        $builder = (new Builder())->setClass($related)
529 4
                                  ->setClient($this->getClient())
530
                                  ->setParent($this);
531
532
        return new HasMany($builder, $this);
533
    }
534
535 2
    /**
536
     * Is endpoint nested behind another endpoint
537 2
     */
538
    public function isNested(): bool
539
    {
540
        return $this->nested ?? false;
541
    }
542
543 1
    /**
544
     * Convert the object into something JSON serializable.
545 1
     */
546
    public function jsonSerialize(): array
547
    {
548
        return $this->toArray();
549
    }
550
551 32
    /**
552
     * Map keys to names that are more standard to our use
553
     */
554 32
    protected function keyMap(string $key): string
555 32
    {
556 32
        // TODO: Is this a good idea?
557 32
        return match ($key) {
558
            'color' => 'colour',
559
            default => $key,
560
        };
561
    }
562
563
    /**
564
     * Create a new model instance that is existing.
565
     *
566 8
     * @param  array  $attributes
567
     * @return static
568 8
     */
569
    public function newFromBuilder($attributes = []): self
570 8
    {
571
        $model = $this->newInstance([], true);
572 8
573
        // TODO: Should we add a way to transform values?
574
        //       (i.e. SoftwareLicence gives 1899-12-30 00:00:00 for null date)
575
576
        $model->setRawAttributes((array) $attributes, true);
577
578
        return $model;
579
    }
580
581
    /**
582
     * Create a new instance of the given model.
583
     *
584 13
     * Provides a convenient way for us to generate fresh model instances of this current model.
585
     * It is particularly useful during the hydration of new objects via the builder.
586 13
     *
587
     * @param  bool  $exists
588 13
     * @return static
589
     */
590 13
    public function newInstance(array $attributes = [], $exists = false): self
591
    {
592
        $model = (new static($attributes, $this->parentModel))->setClient($this->client);
593
594
        $model->exists = $exists;
595
596 5
        return $model;
597
    }
598 5
599
    /**
600
     * Determine if the given attribute exists.
601
     */
602
    public function offsetExists($offset): bool
603
    {
604 2
        return ! is_null($this->getAttribute($offset));
605
    }
606 2
607
    /**
608
     * Get the value for a given offset.
609
     */
610
    public function offsetGet($offset): mixed
611
    {
612
        return $this->getAttribute($offset);
613
    }
614
615 2
    /**
616
     * Set the value for a given offset.
617 2
     *
618 1
     *
619
     * @throws ModelReadonlyException
620
     */
621 1
    public function offsetSet($offset, $value): void
622
    {
623
        if ($this->readonlyModel) {
624
            throw new ModelReadonlyException();
625
        }
626
627 3
        $this->setAttribute($offset, $value);
628
    }
629 3
630
    /**
631
     * Unset the value for a given offset.
632
     */
633
    public function offsetUnset($offset): void
634
    {
635
        unset($this->attributes[$offset], $this->relations[$offset]);
636
    }
637
638
    /**
639
     * Laravel allows control of accessing missing attributes, so we just return false
640
     *
641
     * @return bool
642
     */
643
    public static function preventsAccessingMissingAttributes()
644
    {
645
        return false;
646
    }
647 66
648
    /**
649 66
     * Determine if the given relation is loaded.
650
     *
651
     * @param  string  $key
652
     */
653
    public function relationLoaded($key): bool
654
    {
655
        return array_key_exists($key, $this->relations);
656
    }
657
658
    /**
659 66
     * Laravel allows the resolver to be set at runtime, so we just return null
660
     *
661 66
     * @param  string  $class
662
     * @param  string  $key
663
     * @return null
664
     */
665
    public function relationResolver($class, $key)
666
    {
667
        return null;
668
    }
669
670 7
    /**
671
     * Save the model in Halo
672
     *
673 7
     * @throws NoClientException
674 1
     * @throws TokenException
675
     */
676
    public function save(): bool
677
    {
678 6
        // TODO: Make sure that the model supports being saved
679 1
        if ($this->readonlyModel) {
680
            return false;
681
        }
682 5
683
        try {
684 1
            if (! $this->isDirty()) {
685 1
                return true;
686
            }
687
688 1
            $response = $this->getClient()
689
                             ->post($this->getPath(), [$this->toArray()]);
690
691 1
            $this->exists = true;
692
693 1
            $this->wasRecentlyCreated = true;
694
695
            // Reset the model with the results as we get back the full model
696 5
            $this->setRawAttributes($response, true);
0 ignored issues
show
Bug introduced by
It seems like $response can also be of type null; however, parameter $attributes of Spinen\Halo\Support\Model::setRawAttributes() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

696
            $this->setRawAttributes(/** @scrutinizer ignore-type */ $response, true);
Loading history...
697 5
698
            return true;
699 3
        } catch (GuzzleException $e) {
700
            // TODO: Do something with the error
701 3
702
            return false;
703
        }
704 3
    }
705
706 3
    /**
707 2
     * Save the model in Halo, but raise error if fail
708
     *
709
     * @throws NoClientException
710 2
     * @throws TokenException
711
     * @throws UnableToSaveException
712
     */
713
    public function saveOrFail(): bool
714
    {
715
        if (! $this->save()) {
716
            throw new UnableToSaveException();
717
        }
718
719
        return true;
720
    }
721 2
722
    /**
723 2
     * Set the readonly
724 1
     *
725
     * @param  bool  $readonly
726
     * @return $this
727 1
     */
728
    public function setReadonly($readonly = true): self
729
    {
730
        $this->readonlyModel = $readonly;
731
732
        return $this;
733
    }
734
735
    /**
736 4
     * Set the given relationship on the model.
737
     *
738 4
     * @param  string  $relation
739
     * @return $this
740 4
     */
741
    public function setRelation($relation, $value): self
742
    {
743
        $this->relations[$relation] = $value;
744
745
        return $this;
746
    }
747
748
    /**
749 3
     * Convert the model instance to an array.
750
     */
751 3
    public function toArray(): array
752
    {
753 3
        return array_merge($this->attributesToArray(), $this->relationsToArray());
754
    }
755
756
    /**
757
     * Convert the model instance to JSON.
758
     *
759 12
     * @param  int  $options
760
     *
761 12
     * @throws JsonEncodingException
762
     */
763
    public function toJson($options = 0): string
764
    {
765
        $json = json_encode($this->jsonSerialize(), $options);
766
767
        // @codeCoverageIgnoreStart
768
        if (JSON_ERROR_NONE !== json_last_error()) {
769
            throw JsonEncodingException::forModel($this, json_last_error_msg());
770
        }
771 1
        // @codeCoverageIgnoreEnd
772
773 1
        return $json;
774
    }
775
}
776