GlossaryVariantRepository::store()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 3
b 0
f 0
nc 2
nop 1
dl 0
loc 11
rs 10
1
<?php
2
3
namespace App\Repositories;
4
5
use App\Models\GlossaryVariant;
6
use Illuminate\Support\Collection;
7
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
8
9
10
class GlossaryVariantRepository implements GlossaryVariantRepositoryInterface
11
{
12
13
    /**
14
     * Get Glossary variants bt Glossary term id
15
     *
16
     * @return Collection
17
     */
18
    public function getAll(): Collection
19
    {
20
        return GlossaryVariant::with('glossary')
21
            ->whereHas('glossary', function ($q) {
22
                $q->where('is_published', 1);
23
            })->get();
24
25
26
        //return GlossaryVariant::where($id);
27
    }
28
29
    /**
30
     * Get Glossary variants bt Glossary term id
31
     *
32
     * @param  int  $id
33
     * @return GlossaryVariant
34
     */
35
    public function getById(int $id): GlossaryVariant
36
    {
37
        return GlossaryVariant::findOrFail($id);
38
    }
39
40
    /**
41
     * Store Glossary Variant (all the languages)
42
     *
43
     * @param array $data
44
     * @return GlossaryVariant
45
     */
46
    public function store(array $data): GlossaryVariant
47
    {
48
        $glossaryVariant = new GlossaryVariant();
49
50
        $glossaryVariant->glossary_id = $data['glossary_id'];
0 ignored issues
show
Bug introduced by
The property glossary_id does not exist on App\Models\GlossaryVariant. Did you mean glossary?
Loading history...
51
        foreach (LaravelLocalization::getSupportedLocales() as $key => $locale) {
52
            $glossaryVariant->setTranslation('term', $key, $data['lang'][$key]);
53
        }
54
55
        $glossaryVariant->save();
56
        return $glossaryVariant->fresh();
57
    }
58
59
    /**
60
     * Update Glossary Variant (all the languages)
61
     *
62
     * @param array $data
63
     * @param int $id
64
     *
65
     * @return \App\Models\GlossaryVariant
66
     */
67
    public function update(array $data, int $id): GlossaryVariant
68
    {
69
        $glossaryVariant = $this->getById($id);
70
71
        $glossaryVariant->glossary_id = $data['glossary_id'];
0 ignored issues
show
Bug introduced by
The property glossary_id does not exist on App\Models\GlossaryVariant. Did you mean glossary?
Loading history...
72
        foreach (LaravelLocalization::getSupportedLocales() as $key => $locale) {
73
            $glossaryVariant->setTranslation('term', $key, $data['lang'][$key]);
74
        }
75
76
        $glossaryVariant->update();
77
78
        return $glossaryVariant;
79
    }
80
81
    /**
82
     * Delete Glossary Variant term
83
     *
84
     * @param int $id
85
     * @return void
86
     */
87
    public function delete(int $id): void
88
    {
89
        GlossaryVariant::destroy($id);
90
    }
91
92
93
}
94