Completed
Push — develop ( ea9a36...1bc5bd )
by Neomerx
05:01
created

Session::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php namespace Limoncello\Application\Session;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Iterator;
20
use Limoncello\Application\Contracts\Session\SessionFunctionsInterface;
21
use Limoncello\Contracts\Session\SessionInterface;
22
23
/**
24
 * @package Limoncello\Application
25
 */
26
class Session implements SessionInterface
27
{
28
    /**
29
     * @var SessionFunctionsInterface
30
     */
31
    private $functions;
32
33
    /**
34
     * @param SessionFunctionsInterface $functions
35
     */
36
    public function __construct(SessionFunctionsInterface $functions)
37
    {
38
        $this->functions = $functions;
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public function getIterator()
45
    {
46
        $iterator = call_user_func($this->getFunctions()->getIteratorCallable());
47
48
        assert($iterator instanceof Iterator);
49
50
        return $iterator;
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56
    public function offsetExists($key)
57
    {
58
        assert(is_string($key) || is_int($key));
59
60
        $exists = call_user_func($this->getFunctions()->getHasCallable(), $key);
61
62
        assert(is_bool($exists));
63
64
        return $exists;
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70
    public function offsetGet($key)
71
    {
72
        assert(is_string($key) || is_int($key));
73
74
        $value = call_user_func($this->getFunctions()->getRetrieveCallable(), $key);
75
76
        return $value;
77
    }
78
79
    /**
80
     * @inheritdoc
81
     */
82
    public function offsetSet($key, $value)
83
    {
84
        assert(is_string($key) || is_int($key));
85
86
        call_user_func($this->getFunctions()->getPutCallable(), $key, $value);
87
    }
88
89
    /**
90
     * @inheritdoc
91
     */
92
    public function offsetUnset($key)
93
    {
94
        assert(is_string($key) || is_int($key));
95
96
        call_user_func($this->getFunctions()->getDeleteCallable(), $key);
97
    }
98
99
    /**
100
     * @return SessionFunctionsInterface
101
     */
102
    protected function getFunctions(): SessionFunctionsInterface
103
    {
104
        return $this->functions;
105
    }
106
}
107