Test Setup Failed
Push — development ( 057645...ac5058 )
by Ashutosh
15:58
created

BaseAuthController::sendActivation()   B

Complexity

Conditions 5
Paths 36

Size

Total Lines 53

Duplication

Lines 5
Ratio 9.43 %

Importance

Changes 0
Metric Value
cc 5
nc 36
nop 3
dl 5
loc 53
rs 8.7143
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Http\Controllers\Auth;
4
5
use App\ApiKey;
6
use App\Http\Controllers\Controller;
7
use App\Model\User\AccountActivate;
8
use App\User;
9
use Illuminate\Http\Request;
10
11
class BaseAuthController extends Controller
12
{
13
    //Required Fields for Zoho
14
    public function reqFields($user, $email)
15
    {
16
        $user = $user->where('email', $email)->first();
17
        if ($user) {
18
            $xml = '      <Leads>
19
                        <row no="1">
20
                        <FL val="Lead Source">Faveo Billing</FL>
21
                        <FL val="Company">'.$user->company.'</FL>
22
                        <FL val="First Name">'.$user->first_name.'</FL>
23
                        <FL val="Last Name">'.$user->last_name.'</FL>
24
                        <FL val="Email">'.$user->email.'</FL>
25
                        <FL val="Manager">'.$user->manager.'</FL>
26
27
                        <FL val="Phone">'.$user->mobile_code.''.$user->mobile.'</FL>
28
                        <FL val="Mobile">'.$user->mobile_code.''.$user->mobile.'</FL>
29
                        <FL val="Industry">'.$user->bussiness.'</FL>
30
                        <FL val="City">'.$user->town.'</FL>
31
                        <FL val="Street">'.$user->address.'</FL>
32
                        <FL val="State">'.$user->state.'</FL>
33
                        <FL val="Country">'.$user->country.'</FL>
34
35
                        <FL val="Zip Code">'.$user->zip.'</FL>
36
                         <FL val="No. of Employees">'.$user->company_size.'</FL>
37
                        </row>
38
                        </Leads>';
39
40
            return $xml;
41
        }
42
    }
43
44
    /**
45
     * Sends Otp.
46
     */
47 View Code Duplication
    public static function sendOtp($mobile, $code)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
48
    {
49
        $client = new \GuzzleHttp\Client();
50
        $number = $code.$mobile;
51
        $key = ApiKey::where('id', 1)->value('msg91_auth_key');
52
        $response = $client->request('GET', 'https://control.msg91.com/api/sendotp.php', [
53
            'query' => ['authkey' => $key, 'mobile' => $number],
54
        ]);
55
        $send = $response->getBody()->getContents();
56
        $array = json_decode($send, true);
57
        if ($array['type'] == 'error') {
58
            throw new \Exception($array['message']);
59
        }
60
61
        return $array['type'];
62
    }
63
64
    /**
65
     * ReSends Otp.
66
     */
67 View Code Duplication
    public function sendForReOtp($mobile, $code)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
68
    {
69
        $client = new \GuzzleHttp\Client();
70
        $number = $code.$mobile;
71
        $key = ApiKey::where('id', 1)->value('msg91_auth_key');
72
        $response = $client->request('GET', 'https://control.msg91.com/api/retryotp.php', [
73
            'query' => ['authkey' => $key, 'mobile' => $number],
74
        ]);
75
        $send = $response->getBody()->getContents();
76
        $array = json_decode($send, true);
77
        if ($array['type'] == 'error') {
78
            throw new \Exception($array['message']);
79
        }
80
81
        return $array['type'];
82
    }
83
84
    /**
85
     * Sends otp and email for confirmatiob.
86
     */
87
    public function requestOtpFromAjax(Request $request)
88
    {
89
        // dd($request->allow());
90
        $this->validate($request, [
91
            'email'  => 'required|email',
92
            'code'   => 'required|numeric',
93
            'mobile' => 'required|numeric',
94
        ]);
95
        $email = $request->oldemail;
96
        $newEmail = $request->newemail;
97
        $number = $request->oldnumber;
98
        $newNumber = $request->newnumber;
99
        User::where('email', $email)->update(['email'=>$newEmail, 'mobile'=>$newNumber]);
100
101
        try {
102
            $code = $request->input('code');
103
            $mobile = $request->input('mobile');
104
            $userid = $request->input('id');
105
            $email = $request->input('email');
106
            $pass = $request->input('password');
107
            $number = $code.$mobile;
108
109
            $result = $this->sendOtp($mobile, $code);
110
            $method = 'POST';
111
112
            $this->sendActivation($email, $method, $pass);
0 ignored issues
show
Bug introduced by
It seems like $pass defined by $request->input('password') on line 106 can also be of type array; however, App\Http\Controllers\Aut...oller::sendActivation() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
113
            $response = ['type' => 'success', 'message' => 'Activation link has been sent to '.$email.'.<br>OTP has been sent to '.$number.'.<br>Please enter the OTP received on your mobile No below. Incase you did not recieve OTP,please get in touch with us on <a href="mailto:[email protected]">[email protected]</a>'];
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 337 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
114
115
            return response()->json($response);
116
        } catch (\Exception $ex) {
117
            $result = [$ex->getMessage()];
118
119
            return response()->json(compact('result'), 500);
120
        }
121
    }
122
123
    public function sendActivation($email, $method, $str = '')
124
    {
125
        try {
126
            $user = new User();
127
128
            $activate_model = new AccountActivate();
0 ignored issues
show
Coding Style introduced by
$activate_model does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
129
            $user = $user->where('email', $email)->first();
130
            if (!$user) {
131
                return redirect()->back()->with('fails', 'Invalid Email');
132
            }
133
134
            if ($method == 'GET') {
135
                $activate_model = $activate_model->where('email', $email)->first();
0 ignored issues
show
Coding Style introduced by
$activate_model does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
136
                $token = $activate_model->token;
0 ignored issues
show
Coding Style introduced by
$activate_model does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
137
            } else {
138
                $token = str_random(40);
139
                $activate = $activate_model->create(['email' => $email, 'token' => $token]);
0 ignored issues
show
Coding Style introduced by
$activate_model does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Bug introduced by
The method create() does not exist on App\Model\User\AccountActivate. Did you maybe mean created()?

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...
140
                $token = $activate->token;
141
            }
142
143
            $url = url("activate/$token");
144
            //check in the settings
145
            $settings = new \App\Model\Common\Setting();
146
            $settings = $settings->where('id', 1)->first();
147
148
            //template
149
            $template = new \App\Model\Common\Template();
150
            $temp_id = $settings->where('id', 1)->first()->welcome_mail;
0 ignored issues
show
Coding Style introduced by
$temp_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
151
            $template = $template->where('id', $temp_id)->first();
0 ignored issues
show
Coding Style introduced by
$temp_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
152
            $from = $settings->email;
153
            // var_dump($temp_id);
154
            //  die();
155
            $to = $user->email;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $to. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
156
            $subject = $template->name;
157
            $data = $template->data;
158
            $replace = ['name' => $user->first_name.' '.$user->last_name, 'username' => $user->email, 'password' => $str, 'url' => $url];
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 137 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
159
            $type = '';
160
161 View Code Duplication
            if ($template) {
162
                $type_id = $template->type;
0 ignored issues
show
Coding Style introduced by
$type_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
163
                $temp_type = new \App\Model\Common\TemplateType();
0 ignored issues
show
Coding Style introduced by
$temp_type does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
164
                $type = $temp_type->where('id', $type_id)->first()->name;
0 ignored issues
show
Coding Style introduced by
$temp_type does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
165
            }
166
167
            //dd($from, $to, $data, $subject, $replace, $type);
168
            $templateController = new \App\Http\Controllers\Common\TemplateController();
169
            $mail = $templateController->mailing($from, $to, $data, $subject, $replace, $type);
170
171
            return $mail;
172
        } catch (\Exception $ex) {
173
            throw new \Exception($ex->getMessage());
174
        }
175
    }
176
177
    /**
178
     * Get the post register / login redirect path.
179
     *
180
     * @return string
181
     */
182
    public function redirectPath()
183
    {
184
        if (\Session::has('session-url')) {
185
            $url = \Session::get('session-url');
186
187
            return property_exists($this, 'redirectTo') ? $this->redirectTo : '/'.$url;
0 ignored issues
show
Bug introduced by
The property redirectTo does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
188
        } else {
189
            return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
190
        }
191
    }
192
}
193