Passed
Branch master (ffb5df)
by Michele
03:52
created

WebArtisanAuthController::prepareResultToHtml()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 14
rs 9.9666
1
<?php
2
3
namespace Micheledamo\LaravelWebArtisan\Controllers;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\Artisan;
7
use Symfony\Component\Console\Exception\CommandNotFoundException;
8
9
class WebArtisanAuthController extends WebArtisanBaseController
10
{
11
    /**
12
     * The username string
13
     *
14
     * @var
15
     */
16
    protected $username;
17
18
    /**
19
     * The password string
20
     *
21
     * @var
22
     */
23
    protected $password;
24
25
    /**
26
     * Authentication
27
     *
28
     * @param Request $request
29
     * @return string
30
     */
31
    public function auth(Request $request)
32
    {
33
        $this->username = $request->has('username')
34
            ? $request->get('username')
35
            : null;
36
37
        $this->password = $request->has('password')
38
            ? $request->get('password')
39
            : null;
40
41
        if($this->username and $this->password) {
42
            $authenticated = true;
43
            if($this->username != config('webartisan.auth.username')) $authenticated = false;
44
            if($this->password != config('webartisan.auth.password')) $authenticated = false;
45
46
            if($authenticated) {
47
                $request->session()->put('webartisan__authenticated', true);
48
                return $this->prepareResultToHtml("Welcome", 'success');
49
            }
50
            else return $this->prepareResultToHtml("Oops, it looks like we don't know each other.", 'error');
51
        }
52
        return $this->prepareResultToHtml("Username and Password cannot be empty.", 'error');
53
    }
54
55
    /**
56
     * Logout
57
     *
58
     * @param Request $request
59
     * @return bool
60
     */
61
    public function logout(Request $request)
62
    {
63
        $this->webartisan->setAuthenticated(false);
64
        $request->session()->forget('webartisan__authenticated');
65
        return $this->prepareResultToHtml("See you soon", 'success');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->prepareRes...e you soon', 'success') returns the type string which is incompatible with the documented return type boolean.
Loading history...
66
    }
67
68
    /**
69
     * Convert to HTML
70
     * and present with type colors
71
     *
72
     * @param $string
73
     * @param $type
74
     * @return string|string[]
75
     */
76
    public function prepareResultToHtml($string, $type)
77
    {
78
        $string = str_replace("\n", '<br>',
79
            str_replace("\r", '<br>',
80
                str_replace("\t", '&nbsp;&nbsp;&nbsp;&nbsp;',
81
                    str_replace(" ", '&nbsp;',
82
                        $string))));
83
84
        switch ($type) {
85
            case "error": { $string = '<span class="webartisan__window__results__error">' . $string . '</span>'; }break;
86
            case "success": { $string = '<span class="webartisan__window__results__success">' . $string . '</span>'; }break;
87
        }
88
89
        return $string;
90
    }
91
}