GameController::checkCookie()   B
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 10
nc 6
nop 0
1
<?php
2
class GameController extends Controller
3
{
4
    public $pageID;
5
    public $contentClass;
6
    public $layout='//layouts/column1';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 0 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
7
    private $lastRefresh = 0;
8
9
    protected function beforeAction($action)
0 ignored issues
show
Coding Style introduced by
beforeAction uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
10
    {
11
        $this->checkCookie();
12
13
        if (!Yii::app()->player->uid) {
14
            if (Yii::app()->params['isPartOfWline'] === true) {
15
                throw new CHttpException(403, 'Regisztráció nélkül a játék nem használható.'); //own nick
16
            } else {
17
                if (isset($_SESSION['uid'])) {
18
                    unset($_SESSION['uid']);
19
                }
20
                Yii::app()->user->logout();
21
                $this->redirect(['/?ba']);
22
            }
23
        }
24
25
        if (Yii::app()->params['isPartOfWline'] === true) {
26
            $this->updateInternals();
27
            $this->autoLogout();
28
            $this->setTimers();
29
        }
30
31
        //init Player component, increase energy
32
        Yii::app()->player->rest();
33
34
        return true;
35
    }
36
37
    private function checkCookie()
38
    {
39
        $missingGameVar = true;
40
        $enabledCookie = isset(Yii::app()->request->cookies['PHPSESSID']->value);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
41
        if ($enabledCookie) {
42
            $session = Yii::app()->session;
43
            $session->open();
44
            if ($session->get('fish_game') == 1) {
45
                $missingGameVar = false;
46
            }
47
        }
48
49
        if (Yii::app()->params['isPartOfWline'] === true && $missingGameVar) {
50
            $this->redirect(['gate/cookie']);
51
        }
52
    }
53
54
    private function updateInternals()
55
    {
56
        $session = Yii::app()->session;
57
58
        $lastRefresh = Yii::app()->dbWline->createCommand()
59
            ->select(Yii::app()->params['wlineRefreshAttribute'])
60
            ->from(Yii::app()->params['wlineUsersTable'])
61
            ->where('uid=' . Yii::app()->player->uid)
62
            ->queryScalar();
63
64
        if (!isset($session['r_time'])) {
65
            $session['r_time'] = time();
66
        }
67
68
        $this->lastRefresh = $lastRefresh;
69
    }
70
71
    private function autoLogout()
72
    {
73
        $session = Yii::app()->session;
74
75
        if (time() - $session['r_time'] > Yii::app()->params['maxtime']) {
76
            $wid =  @Yii::app()->request->cookies['PHPSESSID']->value;
77
            header('Location: ' . Yii::app()->params['wlineHost'] . "menu.php?wid=$wid#autoLogout");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $wid instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
78
            Yii::app()->end();
79
        }
80
    }
81
82
    private function setTimers()
83
    {
84
        if (Yii::app()->request->getParam('auto', 0) == 1) {
85
            return false;
86
        }
87
88
        //set r_time
89
        Yii::app()->session['r_time'] = time();
90
91
92
        if (time() - $this->lastRefresh < 30) {
93
            return false;
94
        }
95
96
        Yii::app()->dbWline->createCommand()->update(Yii::app()->params['wlineUsersTable'], [
97
            Yii::app()->params['wlineRefreshAttribute'] => time(),
98
                ], 'uid=' . Yii::app()->player->uid);
99
100
    }
101
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
102