Completed
Push — master ( ad6b4f...e57368 )
by Terzi
05:01 queued 10s
created

TranslationsController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Terranet\Administrator\Controllers;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Collection;
7
use Terranet\Administrator\Events\TranslationsChanged;
8
use Terranet\Administrator\Services\Translator;
9
10
ini_set('opcache.enable', 0);
11
12
class TranslationsController extends AdminController
13
{
14
    /** @var Translator */
15
    protected $translator;
16
17
    /**
18
     * @return Translator
19
     */
20
    protected function translator(): ?Translator
21
    {
22
        if (null === $this->translator) {
23
            $this->translator = app(Translator::class)->setLocales(
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

23
            $this->translator = /** @scrutinizer ignore-call */ app(Translator::class)->setLocales(
Loading history...
24
                $this->locales()
25
            );
26
        }
27
28
        return $this->translator;
29
    }
30
31
    public function index(Request $request)
32
    {
33
        $page = $request->get('page', 1);
34
        $term = $request->get('term');
35
        $only = $request->get('only');
36
37
        $pagination = $this->translator()->load(
38
            $term,
39
            'all' === $only ? null : $only,
40
            $page,
41
            20
42
        );
43
44
        $pagination->appends(compact('term', 'only'));
45
46
        return view(app('scaffold.template')->translations('index'), [
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
        return view(/** @scrutinizer ignore-call */ app('scaffold.template')->translations('index'), [
Loading history...
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
        return /** @scrutinizer ignore-call */ view(app('scaffold.template')->translations('index'), [
Loading history...
47
            'pagination' => $pagination,
48
            'scopes' => $this->translator()->filters(),
49
        ]);
50
    }
51
52
    public function store(Request $request)
53
    {
54
        $redirectTo = redirect()->back()->with('messages', [trans('administrator::messages.update_success')]);
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
        $redirectTo = /** @scrutinizer ignore-call */ redirect()->back()->with('messages', [trans('administrator::messages.update_success')]);
Loading history...
Bug introduced by
The function trans was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
        $redirectTo = redirect()->back()->with('messages', [/** @scrutinizer ignore-call */ trans('administrator::messages.update_success')]);
Loading history...
55
56
        if (empty($translation = $request->input('translation'))) {
57
            return $redirectTo;
58
        }
59
60
        $changed = [];
61
        foreach ($this->locales() as $locale) {
62
            // protect against saving foreign languages
63
            if (app('scaffold.translations')->readonly($locale)) {
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
            if (/** @scrutinizer ignore-call */ app('scaffold.translations')->readonly($locale)) {
Loading history...
64
                continue;
65
            }
66
67
            $changed[] = $locale->iso6391();
68
            $this->translator()->save($translation, $locale->iso6391());
69
        }
70
71
        event(new TranslationsChanged($changed));
0 ignored issues
show
Bug introduced by
The function event was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
        /** @scrutinizer ignore-call */ 
72
        event(new TranslationsChanged($changed));
Loading history...
72
73
        return $redirectTo;
74
    }
75
76
    /**
77
     * @return Collection
78
     */
79
    protected function locales(): Collection
80
    {
81
        return \localizer\locales();
82
    }
83
}
84