Passed
Push — feature/66 ( 0be2df...3c1553 )
by Max
02:31
created

BaseStorage   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 6
c 1
b 0
f 1
dl 0
loc 43
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A key() 0 3 1
A added() 0 6 2
A __construct() 0 8 1
A value() 0 3 1
1
<?php
2
3
namespace MaxGoryunov\SavingIterator\Src;
4
5
use Iterator;
6
7
/**
8
 * Base storage implementation.
9
 */
10
final class BaseStorage
11
{
12
13
    /**
14
     * Ctor.
15
     * 
16
     * @param array $stored
17
     */
18
    public function __construct(
19
        /**
20
         * Stored iterator values.
21
         * 
22
         * @var array
23
         */
24
        private array $stored = []
25
    ) {
26
    }
27
28
    /**
29
     * {@inheritDoc}
30
     */
31
    public function added(Iterator $source): static
32
    {
33
        if ($source->valid()) {
34
            $this->stored[$source->key()] = $source->current();
35
        }
36
        return $this;
37
    }
38
39
    /**
40
     * {@inheritDoc}
41
     */
42
    public function value(): mixed
43
    {
44
        return current($this->stored);
45
    }
46
47
    /**
48
     * {@inheritDoc}
49
     */
50
    public function key(): mixed
51
    {
52
        return key($this->stored);
53
    }
54
}
55