FileCookieJar::getFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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\HttpAdapterException;
16
17
/**
18
 * @author GeLo <[email protected]>
19
 */
20
class FileCookieJar extends AbstractPersistentCookieJar
21
{
22
    /**
23
     * @var string
24
     */
25
    private $file;
26
27
    /**
28
     * @param string                      $file
29
     * @param CookieFactoryInterface|null $cookieFactory
30
     */
31 81
    public function __construct($file, CookieFactoryInterface $cookieFactory = null)
32
    {
33 81
        $this->setFile($file);
34
35 81
        parent::__construct($cookieFactory, file_exists($file));
36 81
    }
37
38
    /**
39
     * @return string
40
     */
41 18
    public function getFile()
42
    {
43 18
        return $this->file;
44
    }
45
46
    /**
47
     * @param string $file
48
     */
49 81
    public function setFile($file)
50
    {
51 81
        $this->file = $file;
52 81
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 27
    public function load()
58
    {
59 27
        if (($data = @file_get_contents($this->file)) === false) {
60 9
            $error = error_get_last();
61 9
            throw HttpAdapterException::cannotLoadCookieJar($error['message']);
62
        }
63
64 18
        $this->unserialize($data);
65 18
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 45
    public function save()
71
    {
72 45
        if (@file_put_contents($this->file, $this->serialize()) === false) {
73 9
            $error = error_get_last();
74 9
            throw HttpAdapterException::cannotSaveCookieJar($error['message']);
75
        }
76 36
    }
77
}
78