Completed
Push — master ( 37faaa...541bbf )
by Raffael
10:18 queued 06:30
created

Session   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 8
cbo 0
dl 0
loc 76
rs 10
c 0
b 0
f 0
lcom 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isFinalized() 0 4 1
A set() 0 6 1
A getSize() 0 4 1
A getId() 0 4 1
A getHashContext() 0 4 1
A getHash() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * balloon
7
 *
8
 * @copyright   Copryright (c) 2012-2019 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Balloon;
13
14
use Balloon\Session\SessionInterface;
15
use MD5Context;
16
use MongoDB\BSON\ObjectIdInterface;
17
18
class Session implements SessionInterface
19
{
20
    /**
21
     * Kind.
22
     */
23
    public const KIND = 'Session';
24
25
    /**
26
     * Resource.
27
     *
28
     * @var array
29
     */
30
    protected $resource = [];
31
32
    /**
33
     * Session.
34
     */
35
    public function __construct(array $resource)
36
    {
37
        $this->resource = $resource;
38
    }
39
40
    /**
41
     * Is finalized.
42
     */
43
    public function isFinalized(): bool
44
    {
45
        return isset($this->resource['hash']);
46
    }
47
48
    /**
49
     * Set.
50
     */
51
    public function set(array $data): self
52
    {
53
        $this->resource = array_merge($this->resource, $data);
54
55
        return $this;
56
    }
57
58
    /**
59
     * Get size.
60
     */
61
    public function getSize(): int
62
    {
63
        return $this->resource['size'] ?? 0;
64
    }
65
66
    /**
67
     * Get ID.
68
     */
69
    public function getId(): ObjectIdInterface
70
    {
71
        return $this->resource['_id'];
72
    }
73
74
    /**
75
     * Get hash context.
76
     */
77
    public function getHashContext(): MD5Context
78
    {
79
        return unserialize($this->resource['context']);
80
    }
81
82
    /**
83
     * Finalize hash.
84
     */
85
    public function getHash(): string
86
    {
87
        if (isset($this->resource['hash'])) {
88
            return $this->resource['hash'];
89
        }
90
91
        return $this->resource['hash'] = md5_final($this->getHashContext());
92
    }
93
}
94