Completed
Push — master ( 04748f...585c72 )
by Marco
07:11
created

src/Cookie.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php namespace Comodojo\Cookies;
2
3
use \Comodojo\Cookies\CookieInterface\CookieInterface;
4
use \Comodojo\Exception\CookieException;
5
use \Comodojo\Cookies\CookieBase;
6
7
/**
8
 * Plain cookie
9
 * 
10
 * @package     Comodojo Spare Parts
11
 * @author      Marco Giovinazzi <[email protected]>
12
 * @license     MIT
13
 *
14
 * LICENSE:
15
 * 
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
25
class Cookie extends CookieBase implements CookieInterface {
26
27
    /**
28
     * Cookie constructor
29
     *
30
     * Setup cookie name
31
     *
32
     * @param  string   $name
33
     *
34
     * @throws \Comodojo\Exception\CookieException
35
     */
36 57
    public function __construct($name) {
37
38
        try {
39
40 57
            $this->setName($name);
41
            
42 19
        } catch (CookieException $ce) {
43
            
44
            throw $ce;
45
46
        }
47
48 57
        if ( defined("COMODOJO_COOKIE_MAX_SIZE") ) {
49
50
            $this->max_cookie_size = filter_var(COMODOJO_COOKIE_MAX_SIZE, FILTER_VALIDATE_INT, array(
51
                'options' => array(
52
                    'default' => 4000
53
                )
54
            ));
55
56
        }
57
58 57
    }
59
60
    /**
61
     * Set cookie content
62
     *
63
     * @param   mixed   $value      Cookie content
64
     * @param   bool    $serialize  If true (default) cookie will be serialized first
65
     *
66
     * @return  \Comodojo\Cookies\Cookie
67
     *
68
     * @throws  \Comodojo\Exception\CookieException
69
     */
70 15
    public function setValue($value, $serialize = true) {
71
72 15
        if ( !is_scalar($value) AND $serialize === false ) throw new CookieException("Cannot set non-scalar value without serialization");
73
74 15
        $cookie_value = $serialize === true ? serialize($value) : $value;
75
76 15
        if ( strlen($cookie_value) > $this->max_cookie_size ) throw new CookieException("Cookie size larger than 4KB");
77
78 12
        $this->value = $cookie_value;
79
80 12
        return $this;
81
82
    }
83
84
    /**
85
     * Get cookie content
86
     *
87
     * @param   bool    $unserialize    If true (default) cookie will be unserialized first
88
     *
89
     * @return  mixed
90
     */
91 12
    public function getValue($unserialize = true) {
92
93 12
        return $unserialize ? unserialize($this->value) : $this->value;
94
95
    }
96
97
    /**
98
     * Static method to create a cookie quickly
99
     *
100
     * @param   string   $name  The cookie name
101
     * 
102
     * @param   array    $properties    Array of properties cookie should have
103
     *
104
     * @return  \Comodojo\Cookies\Cookie
105
     *
106
     * @throws  \Comodojo\Exception\CookieException
107
     */
108 3 View Code Duplication
    public static function create($name, $properties = array(), $serialize = true) {
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
110
        try {
111
112 3
            $cookie = new Cookie($name);
113
114 3
            self::cookieProperties($cookie, $properties, $serialize);
115
116 1
        } catch (CookieException $ce) {
117
            
118
            throw $ce;
119
120
        }
121
122 3
        return $cookie;
123
124
    }
125
126
    /**
127
     * Static method to get a cookie quickly
128
     *
129
     * @param   string   $name  The cookie name
130
     *
131
     * @return  \Comodojo\Cookies\Cookie
132
     *
133
     * @throws  \Comodojo\Exception\CookieException
134
     */
135 3 View Code Duplication
    public static function retrieve($name) {
136
137
        try {
138
139 3
            $cookie = new Cookie($name);
140
141 3
            $return = $cookie->load();
142
143 3
        } catch (CookieException $ce) {
144
            
145 3
            throw $ce;
146
147
        }
148
149
        return $return;
150
151
    }
152
153
}
154