Completed
Push — master ( dcc265...b7fd28 )
by Aitor Riba
02:21
created

LaralangController   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 120
Duplicated Lines 3.33 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 6
Bugs 3 Features 0
Metric Value
wmc 23
lcom 0
cbo 3
dl 4
loc 120
rs 10
c 6
b 3
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A showLogin() 0 8 3
A login() 4 10 2
A logout() 0 6 1
A index() 0 4 1
A showTranslations() 0 4 1
A showTranslationsFilter() 0 4 1
A showTranslate() 0 4 1
A translate() 0 5 1
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

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 Aitor24\Linker\Facades\Linker;
8
use App\Http\Controllers\Controller;
9
use Crypt;
10
use Illuminate\Http\Request;
11
12
class LaralangController extends Controller
13
{
14
    public function showLogin()
15
    {
16
        if (session('laralang.password') && Crypt::decrypt(session('laralang.password')) == config('laralang.default.password')) {
17
            return redirect(Route('laralang::translations'));
18
        }
19
20
        return view('laralang::login');
21
    }
22
23
    public function login(Request $request)
24
    {
25
        session(['laralang.password' => Crypt::encrypt($request->input('password'))]);
26 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...
27
            return redirect(Route('laralang::login'))
28
            ->with('status', 'Invalid password');
29
        }
30
31
        return redirect(Linker::route('laralang::index'));
32
    }
33
34
    public function logout(Request $request)
35
    {
36
        $request->session()->forget('laralang.password');
0 ignored issues
show
Bug introduced by
The method forget() does not seem to exist on object<Symfony\Component...ssion\SessionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
37
38
        return redirect(Linker::route('laralang::login'));
39
    }
40
41
    public function index()
42
    {
43
        return view('laralang::index');
44
    }
45
46
    public function showTranslations()
47
    {
48
        return view('laralang::translations');
49
    }
50
51
    public function showTranslationsFilter()
52
    {
53
        return view('laralang::filter', ['languagesFrom' => Laralang::fromLanguages(), 'languagesTo' => Laralang::toLanguages()]);
54
    }
55
56
    public function showTranslate()
57
    {
58
        return view('laralang::translate');
59
    }
60
61
    public function translate(Request $request)
62
    {
63
        Laralang::generateTranslations($request->input('is_package'), $request->input('package'), $request->input('path'), $request->input('to_langs'));
64
        return redirect(Linker::route('laralang::translate'));
65
    }
66
67
    public function translationsFilter(Request $request)
68
    {
69
        return redirect(Linker::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