AbstractPersistentCookieJar   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 2
dl 0
loc 56
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A __destruct() 0 4 1
A serialize() 0 6 1
A unserialize() 0 19 2
1
<?php
2
3
/*
4
 * This file is part of the Ivory Http Adapter package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\HttpAdapter\Event\Cookie\Jar;
13
14
use Ivory\HttpAdapter\Event\Cookie\CookieFactoryInterface;
15
use Ivory\HttpAdapter\Event\Cookie\CookieInterface;
16
17
/**
18
 * @author GeLo <[email protected]>
19
 */
20
abstract class AbstractPersistentCookieJar extends CookieJar implements PersistentCookieJarInterface
21
{
22
    /**
23
     * @param CookieFactoryInterface|null $cookieFactory
24
     * @param bool                        $load
25
     */
26 189
    public function __construct(CookieFactoryInterface $cookieFactory = null, $load = true)
27
    {
28 189
        parent::__construct($cookieFactory);
29
30 189
        if ($load) {
31 117
            $this->load();
32 91
        }
33 189
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 81
    public function __destruct()
39
    {
40 81
        $this->save();
41 72
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 99
    public function serialize()
47
    {
48
        return json_encode(array_map(function (CookieInterface $cookie) {
49 54
            return $cookie->toArray();
50 99
        }, $this->getCookies()));
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 99
    public function unserialize($serialized)
57
    {
58 99
        $data = json_decode($serialized, true);
59
60 99
        if (empty($data)) {
61 72
            $this->clear();
62 56
        } else {
63 45
            $cookieFactory = $this->getCookieFactory();
64
65 45
            $this->setCookies(array_map(function (array $cookie) use ($cookieFactory) {
66 45
                return $cookieFactory->create(
67 45
                    $cookie['name'],
68 45
                    $cookie['value'],
69 45
                    $cookie['attributes'],
70 45
                    $cookie['created_at']
71 35
                );
72 45
            }, $data));
73
        }
74 99
    }
75
}
76