HasMeta::getSimpleMeta()   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\Libraries\MetaablePrice;
4
5
use App\Libraries\MetaablePrice\Repository as MetaRepository;
6
7
trait HasMeta
8
{
9
    /**
10
     * @return \Illuminate\Database\Eloquent\Relations\morphMany
11
     */
12
    public function meta()
13
    {
14
        return $this->morphMany(Meta::class, 'metaable');
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...
15
    }
16
17
    /**
18
     * Get meta by key.
19
     *
20
     * @param $key
21
     * @param $group null
22
     * @return mixed
23
     */
24
    public function getMeta($key, $group = null)
25
    {
26
        if (is_null($group))
27
            return $this->meta()->whereKey($key)->first();
28
29
        return $this->meta()->whereKey($key)->group($group)->first();
30
    }
31
32
    /**
33
     * Set meta.
34
     *
35
     * @param $key
36
     * @param $value
37
     * @param null $group
38
     * @return mixed
39
     */
40 View Code Duplication
    public function setMeta($key, $value, $group = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
    {
42
        if (!$meta = $this->getMeta($key, $group)) {
43
            return $this->getRepository()->create($this, $key, $value, $group);
44
        } else {
45
            return $this->updateMeta($meta, $key, $value);
46
        }
47
    }
48
49
    /**
50
     * Update Meta.
51
     * 
52
     * @param $meta
53
     * @param $key
54
     * @param $value
55
     * @param null $group
56
     * @return mixed
57
     */
58 View Code Duplication
    public function updateMeta($meta, $key, $value, $group = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60
        if(is_numeric($meta))
61
            $meta = $this->getRepository()->getModel()->find((int) $meta)->first();
62
63
        $meta->key = $key;
64
        $meta->value = $value;
65
        if(isset($group))
66
            $meta->group = $group;
67
68
        $meta->save();
69
        return $meta;
70
    }
71
72
    /**
73
     * Delete meta.
74
     *
75
     * @param $key
76
     * @param null $group
77
     * @return mixed
78
     */
79
    public function unsetMeta($key, $group = null)
80
    {
81
        return $this->getMeta($key, $group)->delete();
82
    }
83
84
    /**
85
     * Alias for unsetMeta method.
86
     *
87
     * @param $key
88
     * @param null $group
89
     * @return mixed
90
     */
91
    public function removeMeta($key, $group = null)
92
    {
93
        if(isset($group))
94
            return $this->unsetMeta($key, $group);
95
96
        return $this->unsetMeta($key);
97
    }
98
99
    /**
100
     * Remove by id.
101
     * 
102
     * @param $id
103
     */
104
    public function removeMetaById($id)
105
    {
106
        $this->getRepository()->removeById((int) $id);
107
    }
108
109
    /**
110
     * Remove group of meta.
111
     *
112
     * @param $groups
113
     * @return $this
114
     */
115 View Code Duplication
    public function removeMetaGroups($groups)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
    {
117
        if(is_array($groups)) {
118
            array_walk($groups, function ($group) {
119
                $this->getRepository()->removeGroup($group);
120
            });
121
        } else {
122
            $this->getRepository()->removeGroup($groups);
123
        }
124
        
125
        return $this;
126
    }
127
128
    /**
129
     * @param $group
130
     * @return $this
131
     */
132
    public function removeGroup($group)
133
    {
134
        return $this->removeMetaGroups($group);
135
    }
136
137
    /**
138
     * Get other meta.
139
     *
140
     * @return mixed
141
     */
142
    public function getOtherMeta()
143
    {
144
        return $this->getMetaFromGroup('other');
145
    }
146
147
    /**
148
     * Get other meta.
149
     *
150
     * @alias: self::getOtherMeta()
151
     * @return mixed
152
     */
153
    public function getSimpleMeta()
154
    {
155
        return $this->getOtherMeta();
156
    }
157
158
    /**
159
     * Get other meta.
160
     *
161
     * @return mixed
162
     */
163
    public function metaWithoutGroup()
164
    {
165
        return $this->getOtherMeta();
166
    }
167
168
    /**
169
     * Get all meta from specific group.
170
     *
171
     * @param $group
172
     * @return mixed
173
     */
174
    public function getMetaFromGroup($group)
175
    {
176
        return $this->meta()->group($group)->get();
177
    }
178
179
    /**
180
     * @param $group
181
     * @return mixed
182
     */
183
    public function getMetaGroup($group)
184
    {
185
        return $this->getMetaFromGroup($group);
186
    }
187
188
    /**
189
     * Get meta repository
190
     *
191
     * @return Repository
192
     */
193
    private function getRepository()
194
    {
195
        return new MetaRepository();
196
    }
197
}