Completed
Push — master ( 20f323...6ae55b )
by Jan
05:32
created

PageController::checkActivationCode()   C

Complexity

Conditions 9
Paths 21

Size

Total Lines 83
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 83
rs 5.48
c 0
b 0
f 0
cc 9
eloc 25
nc 21
nop 3

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
 * Page module controller
4
 * 
5
 * Controller for displaying pages
6
 * 
7
 * @category Controller
8
 * @subpackage Backend
9
 * @package Olapus
10
 * @author Jan Drda <[email protected]>
11
 * @copyright Jan Drda
12
 * @license https://opensource.org/licenses/MIT MIT
13
 */
14
15
namespace App\Http\Controllers\Frontend;
16
17
18
use App\Application;
19
use App\Campaign;
20
use App\Payment;
21
use App\Transaction;
22
use App\User;
23
use App\Recommendation;
24
use Illuminate\Http\Request;
25
use Illuminate\Support\Facades\View;
26
use Illuminate\Support\Facades\Mail;
27
use Illuminate\Support\Facades\Hash;
28
use DB;
29
use Carbon\Carbon;
30
use Cache;
31
32
33
34
class PageController extends FrontendController
35
{
36
37
    /**
38
     * Get homepage
39
     *
40
     * @param Request $request
41
     * @return View|void
42
     */
43
    public function getHomepage(Request $request){
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
44
45
        /**
46
         * Load data
47
         */
48
        $this->_arViewData = $this->_loadPageByUrl('/');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->_loadPageByUrl('/') of type * is incompatible with the declared type array of property $_arViewData.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
49
50
        /**
51
         * Number of users
52
         */
53
        $this->_arViewData['users_no'] = User::count() + 135000;
54
55
        /**
56
         * Number of campaigns
57
         */
58
        $this->_arViewData['campaigns_no'] = Campaign::count();
59
60
        /**
61
         * Number of payments total
62
         */
63
        $this->_arViewData['payments_no_total'] = Payment::sum('amount');
64
65
        /**
66
         * Number of payments last month
67
         */
68
        $this->_arViewData['payments_no_last_month'] = Payment::where('created_at', '>=', Carbon::now()->subMonth())->count();
69
70
        /**
71
         * Return view
72
         */
73
        return $this->_showViewOr404('frontend.homepage');
74
    }
75
76
    /**
77
     * Get contact
78
     *
79
     * @param Request $request
80
     * @return View|void
81
     */
82
    public function getContact(Request $request){
83
84
        /**
85
         * Clear sessions
86
         */
87
        $this->_resetSessionFields($request);
88
89
        /**
90
         * Load page
91
         */
92
        $this->_arViewData = $this->_loadPageByUrl('kontaktujte-nas');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->_loadPageByUrl('kontaktujte-nas') of type * is incompatible with the declared type array of property $_arViewData.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
93
94
        /**
95
         * Return view
96
         */
97
        return $this->_showViewOr404('frontend.contact');
98
    }
99
100
    /**
101
     * Send e-mail to DB
102
     *
103
     * @param Request $request
104
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
105
     */
106
    public function registerEmail(Request $request){
107
108
        /**
109
         * Validate
110
         */
111
        if(env('RECAPTCHA_ENABLED') == 1){
112
            $this->validate($request, [
113
                'email' => 'email|required|max:255',
114
                'g-recaptcha-response' => 'required|recaptcha'
115
            ]);
116
        }
117
        else {
118
119
            $this->validate($request, [
120
                'email' => 'email|required|max:255'
121
            ]);
122
        }
123
124
        /**
125
         * User data
126
         */
127
        $ip = $request->ip();
128
        $email = $request->email;
129
130
        /**
131
         * TEMPORARY
132
         */
133
        //DB::table('users')->where('email', $email)->delete();
0 ignored issues
show
Unused Code Comprehensibility introduced by
74% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
134
135
        /**
136
         * Check if user exists
137
         */
138
        $user = User::where('email', $email)->orWhere('ip', $ip)->first();
139
        $login = NULL;
0 ignored issues
show
Unused Code introduced by
$login is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
140
        $password = NULL;
0 ignored issues
show
Unused Code introduced by
$password is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
141
        $activation_code = base64_encode(Hash::make($email . time()));
142
143
        /**
144
         * User not exists
145
         */
146
        if(empty($user)){
147
148
            $login = $email;
149
            $password = str_random(10);
150
151
            $user = new User();
152
            $user->email = $login;
0 ignored issues
show
Documentation introduced by
The property email does not exist on object<App\User>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
153
            $user->name = '';
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<App\User>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
154
            $user->password = Hash::make($password);
0 ignored issues
show
Documentation introduced by
The property password does not exist on object<App\User>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
155
            $user->activation_code = $activation_code;
0 ignored issues
show
Documentation introduced by
The property activation_code does not exist on object<App\User>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
156
            $user->ip = $ip;
0 ignored issues
show
Documentation introduced by
The property ip does not exist on object<App\User>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
157
            $user->usergroup_id = 2;
0 ignored issues
show
Documentation introduced by
The property usergroup_id does not exist on object<App\User>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
158
159
            /**
160
             * Save recommendation
161
             */
162
            if(Cache::has('recommendation_id')){
163
164
                $recommendation_id = Cache::get('recommendation_id');
165
166
                /**
167
                 * Delete
168
                 */
169
                Cache::forget('recommendation_id');
170
171
                /**
172
                 * Check if recommendation was not used
173
                 */
174
                $test = User::where('recommendation_id', $recommendation_id)->first();
175
176
177
                /**
178
                 * If not , add to user
179
                 */
180
                if(empty($test)){
181
                    $user->recommendation_id = $recommendation_id;
0 ignored issues
show
Documentation introduced by
The property recommendation_id does not exist on object<App\User>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
182
183
                }
184
            }
185
186
            $user->save();
187
188
            /**
189
             * Send e-mail
190
             */
191
            Mail::send(['emails.html.registration', 'emails.plain.registration'], [
192
                'user' => $user,
193
                'login' => $login,
194
                'password' => $password,
195
                'activation_code' => $activation_code
196
197
            ], function ($message) {
198
                $message->from(env('MAIL_FROM_EMAIL', '[email protected]'), env('MAIL_FROM_NAME', 'Surimail.cz'));
199
                $message->to(request('email'), request('email'));
200
                $message->subject('Registrace na serveru Surimail.cz');
201
202
            });
203
204
            $request->session()->flash('success', 'Děkujeme za registraci! Další instrukce naleznete ve Vašem e-mailu!');
205
        }
206
207
        /**
208
         * User exists - error
209
         */
210
        else{
211
212
            $request->session()->flash('custom_error', 'Zadaný e-mail nebo IP již existuje');
213
        }
214
215
        return redirect(route('frontend.homepage.index'));
216
    }
217
218
    /**
219
     * Check activation code
220
     *
221
     * @param $email
222
     * @param $activationCode
223
     * @param Request $request
224
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
225
     */
226
    public function checkActivationCode($email, $activationCode, Request $request){
227
228
        /**
229
         * Check if user exists
230
         */
231
        $user = User::where('email', $email)->where('activation_code', $activationCode)->first();
232
233
        /**
234
         * User not exists or code is not valid
235
         */
236
        if(empty($user)){
237
            $request->session()->flash('custom_error', 'Zadaný e-mail neexistuje nebo je neplatný aktivační kód');
238
        }
239
240
        /**
241
         * User exists - activate him
242
         */
243
        else{
244
            $user->active = true;
245
            $user->activation_code = str_random(16);
246
            $user->save();
247
248
            /**
249
             * Add reward for registration
250
             */
251
            $rewardsCount = Transaction::where('transactionstatus_id', 5)->where('user_id', $user->id)->count();
252
            if($rewardsCount < 1){
253
254
                /**
255
                 * Save transaction
256
                 */
257
                $this->_saveTransation(5, $user->id, $this->_getSettings(6));
258
            }
259
260
            /**
261
             * Add reward for recommedation
262
             */
263
264
            if(!empty($user->recomendation_id) ){
265
266
                $recommendation = Recommendation::find($user->reccomendation_id);
267
268
                if(!empty($recommendation)){
269
270
                   /**
271
                    * Check transaction
272
                    */
273
                    $transactionCount = Transaction::where('recommendation_id', $recommendation->id)->count();
274
275
                    if($transactionCount < 1){
276
277
                        /**
278
                         * Find user and save money
279
                         */
280
                        $rUser = User::find($recommendation->user_id);
281
282
                        if(!empty($rUser)){
283
284
                            $this->_saveTransation(2, $rUser->id, $this->_getSettings(2));
285
                        }
286
                    }
287
                }
288
            }
289
290
            $request->session()->flash('success', 'Gratulujeme, váš účet byl úspěšně aktivován. Přihlásit se můžete zde: '
291
            . '<br><a href="'.route('admin.dashboard.index').'">'.route('admin.dashboard.index').'</a>');
292
293
            /**
294
             * Save reward
295
             */
296
            if($user->recommendation_id != null && $user->recommendation_id > 0){
297
298
                /**
299
                 * Load recommendation
300
                 */
301
                $recommendation = Recommendation::find($user->recommendation_id);
302
303
                $this->_saveTransation(2, $recommendation->user_id, $this->_getSettings(2), null, null, $user->recommendation_id);
304
            }
305
        }
306
307
        return redirect(route('frontend.homepage.index'));
308
    }
309
310
    /**
311
     * Get blank page
312
     *
313
     * @param $pageUrl
314
     * @param Request $request
315
     * @return View|void
316
     */
317
    public function index($pageUrl, Request $request){
318
319
        /**
320
         * Clear sessions
321
         */
322
        $this->_resetSessionFields($request);
323
324
        /**
325
         * Load page
326
         */
327
        $this->_arViewData = $this->_loadPageByUrl($pageUrl);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->_loadPageByUrl($pageUrl) of type * is incompatible with the declared type array of property $_arViewData.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
328
329
        /**
330
         * Return view
331
         */
332
        return $this->_showViewOr404('frontend.blank');
333
    }
334
335
    /**
336
     * Check recommendation
337
     *
338
     * @param $id
339
     * @param Request $request
340
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
341
     */
342
    public function checkRecommmendation($id, Request $request){
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
343
344
        /**
345
         * @todo Use validator
346
         */
347
        if(is_numeric($id)){
348
349
            $recommendation = Recommendation::find($id);
350
351
            /**
352
             * If exists, than remember
353
             */
354
            if(!empty($recommendation)){
355
                Cache::forever('recommendation_id', $id);
356
            }
357
        }
358
359
        return redirect(route('frontend.homepage.index'));
360
    }
361
362
}
363