RememberMeCookie::remove()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of Jitamin.
5
 *
6
 * Copyright (C) Jitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Jitamin\Foundation\Http;
13
14
use Jitamin\Foundation\Base;
15
16
/**
17
 * Remember Me Cookie.
18
 */
19
class RememberMeCookie extends Base
20
{
21
    /**
22
     * Cookie name.
23
     *
24
     * @var string
25
     */
26
    const COOKIE_NAME = 'JM_RM';
27
28
    /**
29
     * Encode the cookie.
30
     *
31
     * @param string $token    Session token
32
     * @param string $sequence Sequence token
33
     *
34
     * @return string
35
     */
36
    public function encode($token, $sequence)
37
    {
38
        return implode('|', [$token, $sequence]);
39
    }
40
41
    /**
42
     * Decode the value of a cookie.
43
     *
44
     * @param string $value Raw cookie data
45
     *
46
     * @return array
47
     */
48
    public function decode($value)
49
    {
50
        list($token, $sequence) = explode('|', $value);
51
52
        return [
53
            'token'    => $token,
54
            'sequence' => $sequence,
55
        ];
56
    }
57
58
    /**
59
     * Return true if the current user has a RememberMe cookie.
60
     *
61
     * @return bool
62
     */
63
    public function hasCookie()
64
    {
65
        return $this->request->getCookie(self::COOKIE_NAME) !== '';
0 ignored issues
show
Documentation introduced by
The property request does not exist on object<Jitamin\Foundation\Http\RememberMeCookie>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
66
    }
67
68
    /**
69
     * Write and encode the cookie.
70
     *
71
     * @param string $token      Session token
72
     * @param string $sequence   Sequence token
73
     * @param string $expiration Cookie expiration
74
     *
75
     * @return bool
76
     */
77
    public function write($token, $sequence, $expiration)
78
    {
79
        return setcookie(
80
            self::COOKIE_NAME,
81
            $this->encode($token, $sequence),
82
            $expiration,
83
            $this->helper->url->dir(),
0 ignored issues
show
Documentation introduced by
The property helper does not exist on object<Jitamin\Foundation\Http\RememberMeCookie>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
84
            null,
85
            $this->request->isHTTPS(),
0 ignored issues
show
Documentation introduced by
The property request does not exist on object<Jitamin\Foundation\Http\RememberMeCookie>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
86
            true
87
        );
88
    }
89
90
    /**
91
     * Read and decode the cookie.
92
     *
93
     * @return mixed
94
     */
95
    public function read()
96
    {
97
        $cookie = $this->request->getCookie(self::COOKIE_NAME);
0 ignored issues
show
Documentation introduced by
The property request does not exist on object<Jitamin\Foundation\Http\RememberMeCookie>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
98
99
        if (empty($cookie)) {
100
            return false;
101
        }
102
103
        return $this->decode($cookie);
104
    }
105
106
    /**
107
     * Remove the cookie.
108
     *
109
     * @return bool
110
     */
111
    public function remove()
112
    {
113
        return setcookie(
114
            self::COOKIE_NAME,
115
            '',
116
            time() - 3600,
117
            $this->helper->url->dir(),
0 ignored issues
show
Documentation introduced by
The property helper does not exist on object<Jitamin\Foundation\Http\RememberMeCookie>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
118
            null,
119
            $this->request->isHTTPS(),
0 ignored issues
show
Documentation introduced by
The property request does not exist on object<Jitamin\Foundation\Http\RememberMeCookie>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
120
            true
121
        );
122
    }
123
}
124