Product::involved()   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 0
1
<?php
2
3
namespace App;
4
5
use App\Libraries\Metaable\HasMeta;
6
//use App\Libraries\MetaablePrice\HasMeta;
7
use App\Libraries\Presenterable\Presenterable;
8
use App\Libraries\Presenterable\Presenters\ProductPresenter;
9
use App\Traits\ActivateableTrait;
10
use App\Traits\HasImages;
11
use Cviebrock\EloquentTaggable\Taggable;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, App\Taggable.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
12
use Keyhunter\Administrator\Repository;
13
use App\Libraries\Taggable\TagService;
14
use Illuminate\Database\Eloquent\Builder;
15
16
class Product extends Repository
17
{
18
    use ActivateableTrait, Presenterable, HasMeta, HasImages, Taggable;
19
20
    /**
21
     * @var string
22
     */
23
    protected $table = 'products';
24
25
    /**
26
     * @var ProductPresenter
27
     */
28
    protected $presenter = ProductPresenter::class;
29
30
    /**
31
     * @var array
32
     */
33
    protected $fillable = [
34
        'sub_category_id',
35
        'lot_id',
36
        'name',
37
        'featured',
38
        'price',
39
        'old_price',
40
        'sale',
41
        'count',
42
        'active',
43
        'description',
44
        'uniqid'
45
    ];
46
47
    /**
48
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
49
     */
50
    public function colors()
51
    {
52
        return $this->hasMany(ModelColors::class, 'product_id', 'id');
53
    }
54
55
    /**
56
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
57
     */
58
    public function lot()
59
    {
60
        return $this->belongsTo(Lot::class, 'lot_id', 'id');
61
    }
62
63
    /**
64
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
65
     */
66
    public function involved()
67
    {
68
        return $this->hasMany(Involved::class)->active();
69
    }
70
71
    /**
72
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
73
     */
74
    public function improvedSpecs()
75
    {
76
        return $this->hasMany(ImprovedSpec::class, 'product_id', 'id');
77
    }
78
    /**
79
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
80
     */
81
    public function specPrice()
82
    {
83
        return $this->hasMany(SpecPrice::class, 'product_id', 'id');
84
    }
85
    /**
86
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
87
     */
88
    public function subcategory()
89
    {
90
        return $this->hasOne(SubCategory::class, 'id', 'sub_category_id');
91
    }
92
93
    /**
94
     * Drafted products scope.
95
     *
96
     * @param $query
97
     * @return \Illuminate\Database\Query\Builder
98
     */
99
    public function scopeDrafted($query)
100
    {
101
        return $query->whereStatus('drafted');
102
    }
103
104
    /**
105
     * Published products scope.
106
     *
107
     * @param $query
108
     * @return \Illuminate\Database\Query\Builder
109
     */
110
    public function scopePublished($query)
111
    {
112
        return $query->whereStatus('published');
113
    }
114
115
    /**
116
     * Completed products scope.
117
     *
118
     * @param $query
119
     * @return \Illuminate\Database\Query\Builder
120
     */
121
    public function scopeCompleted($query)
122
    {
123
        return $query->whereStatus('completed');
124
    }
125
126
    /**
127
     * Featured products scope.
128
     *
129
     * @param $query
130
     * @return mixed
131
     */
132
    public function scopeFeatured($query)
133
    {
134
        return $query->whereFeatured(1);
135
    }
136
137
    /**
138
     * Get a collection of all Tags a Model has.
139
     *
140
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
141
     */
142
    public function tags()
143
    {
144
        return $this->morphToMany(Tag::class, 'taggable', 'taggable_taggables')
145
            ->withTimestamps();
146
    }
147
148
    /**
149
     * Scope for a Model that has all of the given tags.
150
     *
151
     * @param \Illuminate\Database\Eloquent\Builder $query
152
     * @param array|string $tags
153
     *
154
     * @return \Illuminate\Database\Eloquent\Builder
155
     */
156
    public function scopeWithAllTags(Builder $query, $tags)
157
    {
158
        $normalized = app(TagService::class)->buildTagArrayNormalized($tags);
159
160
        return $query->has('tags', '=', count($normalized), 'and', function (Builder $q) use ($normalized) {
161
            $q->whereIn('normalized', $normalized);
162
        });
163
    }
164
}