Completed
Push — master ( 398d0f...310d56 )
by Pascal
11:17 queued 06:22
created

bootHasSpecificationsTrait()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 14
nc 1
nop 0
1
<?php
2
3
namespace Pbmedia\Specifications\Laravel\Models;
4
5
use Illuminate\Database\Eloquent\Relations\MorphMany;
6
use Pbmedia\Specifications\Specifications;
7
8
trait HasSpecificationsTrait
9
{
10
    /**
11
     * ScoreModel class name.
12
     *
13
     * @var string
14
     */
15
    protected $scoreModelClass = ScoreModel::class;
16
17
    /**
18
     * Instance of Specifications.
19
     *
20
     * @var \Pbmedia\Specifications\Specifications
21
     */
22
    protected $specifications;
23
24
    /**
25
     * Returns a Specifications object with the Scores and Attributes
26
     * loaded from the database.
27
     *
28
     * @return \Pbmedia\Specifications\Specifications
29
     */
30
    public function specifications(): Specifications
31
    {
32
        if (!$this->specifications) {
33
            $this->specifications = new Specifications;
34
35
            $this->getRelationValue('Scores')->load('Attribute')->each(function ($scoreModel) {
0 ignored issues
show
Bug introduced by
It seems like getRelationValue() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
36
                $this->specifications->add($scoreModel->getAttributeScore());
37
            });
38
        }
39
40
        return $this->specifications;
41
    }
42
43
    /**
44
     * Defines the relationship with the Scores attached to this model.
45
     *
46
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
47
     */
48
    public function Scores(): MorphMany
49
    {
50
        return $this->morphMany($this->scoreModelClass, 'specifiable');
0 ignored issues
show
Bug introduced by
It seems like morphMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
51
    }
52
53
    /**
54
     * Binds a callback to the 'saved' event of the model which syncs the
55
     * AttributeScore objects in the Specifications instance with
56
     * ones in the database.
57
     */
58
    public static function bootHasSpecificationsTrait()
59
    {
60
        static::saved(function ($canBeSpecified) {
61
            $scoreIds = $canBeSpecified->Scores->pluck('id');
62
63
            if ($scoreIds->count() > 0) {
64
                app($this->scoreModelClass)->whereIn('id', $scoreIds)->delete();
65
            }
66
67
            $attributScoreCollection = $canBeSpecified->specifications()->all();
68
69
            if ($attributScoreCollection->isEmpty()) {
70
                return;
71
            }
72
73
            $attributScoreCollection->map(function ($attributeScore) {
74
                return [
75
                    'attribute_id' => $attributeScore->getAttribute()->id,
76
                    'value'        => $attributeScore->getScoreValue(),
77
                ];
78
            })->each(function ($scoreModelAttributes) use ($canBeSpecified) {
79
                $canBeSpecified->Scores()->create($scoreModelAttributes);
80
            });
81
        });
82
    }
83
}
84