Issues (73)

src/Traits/HasAttributes.php (2 issues)

1
<?php
2
3
namespace Ronmrcdo\Inventory\Traits;
4
5
use Illuminate\Support\Facades\DB;
6
use Illuminate\Database\Eloquent\Relations\HasMany;
7
use Ronmrcdo\Inventory\Exceptions\InvalidAttributeException;
8
9
trait HasAttributes
10
{
11
	/**
12
	 * Create a product attribute
13
	 * 
14
	 * @param array $attributeData
15
	 * @throw  \Ronmrcdo\Inventory\Exceptions\InvalidAttributeException
16
	 * @return $this
17
	 */
18
	public function addAttribute(string $attribute)
19
	{
20
		DB::beginTransaction();
21
22
		try {
23
			$this->attributes()->create(['name' => $attribute]);
24
25
			DB::commit();
26
		} catch (\Throwable $err) { // No matter what error will occur we should throw invalidAttribute
27
			DB::rollBack();
28
29
			throw new InvalidAttributeException($err->getMessage(), 422);
30
		}
31
32
		return $this;
33
	}
34
35
	/**
36
	 * Create multiple attributes
37
	 * 
38
	 * @param mixed $attributes
39
	 * @throw  \Ronmrcdo\Inventory\Exceptions\InvalidAttributeException
40
	 * @return $this
41
	 */
42
	public function addAttributes($attributes)
43
	{
44
		DB::beginTransaction();
45
46
		try {
47
			$this->attributes()->createMany($attributes);
48
49
			DB::commit();
50
		} catch (\Throwable $err) { // No matter what error will occur we should throw invalidAttribute
51
			DB::rollBack();
52
53
			throw new InvalidAttributeException($err->getMessage(), 422);
54
		}
55
56
		return $this;
57
	}
58
59
	/**
60
	 * It should remove attribute from product
61
	 * 
62
	 * @param string $key
63
	 * @return self
64
	 */
65
	public function removeAttribute($attr)
66
	{
67
		DB::beginTransaction();
68
69
		try {
70
			$attribute = $this->attributes()->where('name', $attr)->firstOrFail();
71
72
			$attribute->delete();
73
74
			DB::commit();
75
		} catch (\Throwable $err) { // No matter what error will occur we should throw invalidAttribute
76
			DB::rollBack();
77
78
			throw new InvalidAttributeException($err->getMessage(), 422);
79
		}
80
81
		return $this;
82
	}
83
84
	/**
85
	 * It should remove attribute from product
86
	 * 
87
	 * @param string $key
88
	 * @return self
89
	 */
90
	public function removeAttributeTerm(string $attribute, string $term)
91
	{
92
		DB::beginTransaction();
93
94
		try {
95
			$attribute = $this->attributes()->where('name', $attribute)->firstOrFail();
96
97
			$attribute->removeValue($term);
98
99
			DB::commit();
100
		} catch (\Throwable $err) { // No matter what error will occur we should throw invalidAttribute
101
			DB::rollBack();
102
103
			throw new InvalidAttributeException($err->getMessage(), 422);
104
		}
105
106
		return $this;
107
	}
108
109
	/**
110
	 * Assert if the Product has attributes
111
	 * 
112
	 * @return bool
113
	 */
114
	public function hasAttributes(): bool
115
	{
116
		return !! $this->attributes()->count();
117
	}
118
119
	/**
120
	 * Assert if the product has this attributes
121
	 * 
122
	 * @param string|int $key
123
	 * 
124
	 * @return bool
125
	 */
126
	public function hasAttribute($key): bool
127
	{
128
		// If the arg is a numeric use the id else use the name
129
		if (is_numeric($key)) {
130
			return $this->attributes()->where('id', $key)->exists();
131
		} elseif (is_string($key)) {
0 ignored issues
show
The condition is_string($key) is always true.
Loading history...
132
			return $this->attributes()->where('name', $key)->exists();
133
		}
134
135
		return false;
136
	}
137
138
	/**
139
	 * Add Option Value on the attribute
140
	 * 
141
	 * @param string $option
142
	 * @param mixed $value
143
	 * 
144
	 * @throw \Ronmrcdo\Inventory\Exceptions\InvalidAttributeException
145
	 * 
146
	 * @return \Ronmrcdo\Inventory\Models\AttributeValue
147
	 */
148
	public function addAttributeTerm(string $option, $value)
149
	{
150
		$attribute = $this->attributes()->where('name', $option)->first();
151
152
		if (! $attribute) {
153
			throw new InvalidAttributeException("Invalid attribute", 422);
154
		}
155
156
		return $attribute->addValue($value);
157
	}
158
159
	/**
160
	 * Get Product Attributes
161
	 * 
162
	 * 
163
	 */
164
	public function loadAttributes()
165
	{
166
		return $this->attributes()->get()->load('values');
167
	}
168
169
	/**
170
	 * Relation on Attribute Model
171
	 * 
172
	 * @return \Illuminate\Database\Eloquent\Relations\HasMany $this
173
	 */
174
	public function attributes(): HasMany
175
	{
176
		return $this->hasMany('Ronmrcdo\Inventory\Models\Attribute');
0 ignored issues
show
It seems like hasMany() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

176
		return $this->/** @scrutinizer ignore-call */ hasMany('Ronmrcdo\Inventory\Models\Attribute');
Loading history...
177
	}
178
}