EloquentTagRepository   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 50
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setLocaleAsKey() 0 16 4
A createForLanguage() 0 10 1
A findByName() 0 8 1
1
<?php namespace Modules\Blog\Repositories\Eloquent;
2
3
use Illuminate\Database\Eloquent\Builder;
4
use Illuminate\Support\Facades\App;
5
use Modules\Blog\Repositories\TagRepository;
6
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository;
7
8
class EloquentTagRepository extends EloquentBaseRepository implements TagRepository
0 ignored issues
show
Bug introduced by
There is one abstract method findBySlug in this class; you could implement it, or declare this class as abstract.
Loading history...
9
{
10
    /**
11
     * Find a tag by its name
12
     * @param $name
13
     * @return mixed
14
     */
15
    public function findByName($name)
16
    {
17
        $tags = $this->model->with('translations')->whereHas('translations', function (Builder $q) use ($name) {
18
            $q->where('name', 'like', "%$name%");
19
        })->get();
20
21
        return $this->setLocaleAsKey($tags);
22
    }
23
24
    private function setLocaleAsKey($tags)
25
    {
26
        $cleanedTags = [];
27
        foreach ($tags as $tag) {
28
            foreach ($tag->translations as $tagTranslation) {
29
                if (App::getLocale() == $tagTranslation->locale) {
30
                    $cleanedTags[] = [
31
                        'id' => $tag->id,
32
                        'name' => $tagTranslation->name,
33
                    ];
34
                }
35
            }
36
        }
37
38
        return $cleanedTags;
39
    }
40
41
    /**
42
     * Create the tag for the specified language
43
     * @param  string $lang
44
     * @param  array  $name
45
     * @return mixed
46
     */
47
    public function createForLanguage($lang = 'en', $name)
48
    {
49
        $data = [
50
            $lang => [
51
                'name' => $name,
52
            ],
53
        ];
54
55
        return $this->create($data);
56
    }
57
}
58