Completed
Push — master ( 79accf...5cd3ef )
by
unknown
09:16
created

TranslationMatrixController::labels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace BBSLab\NovaTranslation\Http\Controllers;
4
5
use BBSLab\NovaTranslation\Models\Label;
6
use BBSLab\NovaTranslation\Models\Locale;
7
use BBSLab\NovaTranslation\Models\Translation;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\DB;
10
11
class TranslationMatrixController
12
{
13
    /**
14
     * Setup labels matrix endpoint.
15
     *
16
     * @return \Illuminate\Http\JsonResponse
17
     */
18
    public function index()
19
    {
20
        $labels = $this->labels();
21
22
        $locales = Locale::query()->select('id', 'label')->get();
0 ignored issues
show
Bug introduced by
The method select() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean createSelectWithConstraint()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
23
24
        return response()->json(compact('labels', 'locales'), 200);
25
    }
26
27
    /**
28
     * Save all labels provided in payload.
29
     *
30
     * @param  \Illuminate\Http\Request  $request
31
     * @return \Illuminate\Http\JsonResponse
32
     */
33
    public function save(Request $request)
34
    {
35
        // @TODO...
36
        DB::beginTransaction();
37
38
        Label::query()->truncate();
39
        Translation::query()->where('translatable_type', '=', Label::class)->delete();
40
41
        $labels = $request->input('labels', []);
42
        foreach ($labels as $label) {
43
            $this->createLabel($label);
44
        }
45
46
        DB::commit();
47
48
        return response()->json([
49
            'labels' => $labels,
50
        ], 200);
51
    }
52
53
    /**
54
     * Return all non-scoped labels.
55
     *
56
     * @return \Illuminate\Database\Eloquent\Collection
57
     */
58
    protected function labels()
59
    {
60
        return Label::query()
0 ignored issues
show
Bug introduced by
The method select() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean createSelectWithConstraint()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
61
            ->select('translations.locale_id', 'labels.type', 'labels.key', 'labels.value')
62
            ->join('translations', 'labels.id', '=', 'translations.translatable_id')
63
            ->where('translations.translatable_type', '=', Label::class)
64
            ->get();
65
    }
66
67
    /**
68
     * Create label and associated translation.
69
     *
70
     * @param  array  $data
71
     * @return void
72
     */
73
    protected function createLabel(array $data)
74
    {
75
        /** @var \BBSLab\NovaTranslation\Models\Translation $keyTranslation */
76
        $keyTranslation = Translation::query()
0 ignored issues
show
Bug introduced by
The method select() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean createSelectWithConstraint()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
77
            ->select('translations.translation_id')
78
            ->join('labels', 'translations.translatable_id', '=', 'labels.id')
79
            ->where('translations.translatable_type', '=', Label::class)
80
            ->where('labels.key', '=', $data['key'])
81
            ->first();
82
83
        $translationId = ! empty($keyTranslation) ? $keyTranslation->translation_id : (new Label)->freshTranslationId();
84
85
        /** @var \BBSLab\NovaTranslation\Models\Label $label */
86
        $label = Label::query()->create([
87
            'type' => Label::TYPE_TEXT,
88
            'key' => $data['key'],
89
            'value' => ! empty($data['value']) ? $data['value'] : '',
90
        ]);
91
92
        $label->upsertTranslationEntry($data['locale_id'], $translationId);
93
    }
94
}
95