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

Cookie::setValue()   C

Complexity

Conditions 5
Paths 5

Size

Total Lines 13
Code Lines 6

Duplication

Lines 1
Ratio 7.69 %

Code Coverage

Tests 6
CRAP Score 5

Importance

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