LaralangController   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 121
Duplicated Lines 3.31 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 7
Bugs 4 Features 0
Metric Value
wmc 23
lcom 0
cbo 0
dl 4
loc 121
rs 10
c 7
b 4
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A logout() 0 6 1
A showLogin() 0 8 3
A login() 4 10 2
A translationsFilter() 0 4 1
A showTranslationsFiltered() 0 4 1
A api() 0 4 1
A apiTranslate() 0 6 1
B apiFilterFromTo() 0 19 5
A deleteTranslation() 0 5 1
A deleteAllTranslations() 0 5 1
A editTranslation() 0 10 1
A index() 0 4 1
A showTranslations() 0 4 1
A showTranslationsFilter() 0 4 1
A showTranslate() 0 4 1
A translate() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Aitor24\Laralang\Controllers;
4
5
use Aitor24\Laralang\Facades\Laralang;
6
use Aitor24\Laralang\Models\DB_Translation;
7
use App\Http\Controllers\Controller;
8
use Crypt;
9
use Illuminate\Http\Request;
10
11
class LaralangController extends Controller
12
{
13
    public function showLogin()
14
    {
15
        if (session('laralang.password') && Crypt::decrypt(session('laralang.password')) == config('laralang.default.password')) {
16
            return redirect(Route('laralang::translations'));
17
        }
18
19
        return view('laralang::login');
20
    }
21
22
    public function login(Request $request)
23
    {
24
        session(['laralang.password' => Crypt::encrypt($request->input('password'))]);
25 View Code Duplication
        if (Crypt::decrypt(session('laralang.password')) != config('laralang.default.password')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
26
            return redirect(Route('laralang::login'))
27
            ->with('status', 'Invalid password');
28
        }
29
30
        return redirect()->route('laralang::index');
31
    }
32
33
    public function logout(Request $request)
34
    {
35
        $request->session()->forget('laralang.password');
36
37
        return redirect()->route('laralang::login');
38
    }
39
40
    public function index()
41
    {
42
        return view('laralang::index');
43
    }
44
45
    public function showTranslations()
46
    {
47
        return view('laralang::translations');
48
    }
49
50
    public function showTranslationsFilter()
51
    {
52
        return view('laralang::filter', ['languagesFrom' => Laralang::fromLanguages(), 'languagesTo' => Laralang::toLanguages()]);
53
    }
54
55
    public function showTranslate()
56
    {
57
        return view('laralang::translate');
58
    }
59
60
    public function translate(Request $request)
61
    {
62
        Laralang::generateTranslations($request->input('is_package'), $request->input('package'), $request->input('path'), $request->input('to_langs'));
63
64
        return redirect()->route('laralang::translate');
65
    }
66
67
    public function translationsFilter(Request $request)
68
    {
69
        return redirect()->route('laralang::filterFromTo', [$request->from_lang, $request->to_lang]);
70
    }
71
72
    public function showTranslationsFiltered($from_lang, $to_lang)
73
    {
74
        return view('laralang::translations', ['from_lang' => $from_lang, 'to_lang' => $to_lang]);
75
    }
76
77
    public function api()
78
    {
79
        return DB_Translation::all();
80
    }
81
82
    public function apiTranslate(Request $request)
83
    {
84
        $translatedText = Laralang::trans($request->string)->to($request->to);
85
86
        return ['translatedText' => strval($translatedText)];
87
    }
88
89
    public function apiFilterFromTo($from_lang, $to_lang)
90
    {
91
        // filter wich translation showld send
92
        if ($to_lang == 'all' && $from_lang == 'all') {
93
            return DB_Translation::all();
94
        } elseif ($to_lang == 'all') {
95
96
            //return translation where from_lang == $from
97
            return DB_Translation::where([['from_lang', $from_lang]])->get();
98
        } elseif ($from_lang == 'all') {
99
100
            //return translation where to_lang == $to
101
            return DB_Translation::where([['to_lang', $to_lang]])->get();
102
        } else {
103
104
            //return translation where from_lang == $from and to_lang == $to
105
            return DB_Translation::where([['from_lang', $from_lang], ['to_lang', $to_lang]])->get();
106
        }
107
    }
108
109
    public function deleteTranslation(Request $request)
110
    {
111
        $trans = DB_Translation::findOrFail($request->id);
112
        $trans->delete();
113
    }
114
115
    public function deleteAllTranslations()
116
    {
117
        $trans = DB_Translation::all();
118
        $trans->delete();
119
    }
120
121
    public function editTranslation(Request $request)
122
    {
123
        $trans = DB_Translation::findOrFail($request->id);
124
        $trans->string = $request->string;
125
        $trans->to_lang = $request->to;
126
        $trans->from_lang = $request->from;
127
        $trans->translation = $request->translation;
128
        $trans->touch();
129
        $trans->save();
130
    }
131
}
132