Passed
Pull Request — master (#171)
by Zaahid
06:32 queued 03:18
created

PartChildrenContainer   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Test Coverage

Coverage 92.45%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 24
eloc 36
c 1
b 0
f 0
dl 0
loc 137
ccs 49
cts 53
cp 0.9245
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A key() 0 3 1
A current() 0 3 1
A offsetExists() 0 3 1
A getChildren() 0 6 2
A valid() 0 3 1
A offsetGet() 0 3 2
A offsetUnset() 0 5 2
A __construct() 0 3 1
A rewind() 0 3 1
A next() 0 3 1
A remove() 0 9 3
A offsetSet() 0 11 4
A hasChildren() 0 4 2
A add() 0 8 2
1
<?php
2
/**
3
 * This file is part of the ZBateson\MailMimeParser project.
4
 *
5
 * @license http://opensource.org/licenses/bsd-license.php BSD
6
 */
7
namespace ZBateson\MailMimeParser\Message;
8
9
use ArrayAccess;
10
use InvalidArgumentException;
11
use RecursiveIterator;
12
13
/**
14
 * Container of IMessagePart items for a parent IMultiPart.
15
 *
16
 * @author Zaahid Bateson
17
 */
18
class PartChildrenContainer implements RecursiveIterator, ArrayAccess
19
{
20
    /**
21
     * @var IMessagePart[] array of child parts of the IMultiPart object that is
22
     *      holding this container.
23
     */
24
    protected $children;
25
26
    /**
27
     * @var int current key position within $children for iteration.
28
     */
29
    protected $position = 0;
30
31 107
    public function __construct(array $children = [])
32
    {
33 107
        $this->children = $children;
34 107
    }
35
36
    /**
37
     * Returns true if the current element is an IMultiPart and doesn't return
38
     * null for {@see IMultiPart::getChildIterator()}.  Note that the iterator
39
     * may still be empty.
40
     * 
41
     * @return bool
42
     */
43 70
    public function hasChildren()
44
    {
45 70
        return ($this->current() instanceof IMultiPart
46 70
            && $this->current()->getChildIterator() !== null);
47
    }
48
49
    /**
50
     * If the current element points to an IMultiPart, its child iterator is
51
     * returned by calling {@see IMultiPart::getChildIterator()}.
52
     *
53
     * @return RecursiveIterator|null the iterator
54
     */
55 69
    public function getChildren()
56
    {
57 69
        if ($this->current() instanceof IMultiPart) {
58 69
            return $this->current()->getChildIterator();
59
        }
60 1
        return null;
61
    }
62
63 76
    public function current()
64
    {
65 76
        return $this->offsetGet($this->position);
66
    }
67
68 70
    public function key()
69
    {
70 70
        return $this->position;
71
    }
72
73 76
    public function next()
74
    {
75 76
        ++$this->position;
76 76
    }
77
78 102
    public function rewind()
79
    {
80 102
        $this->position = 0;
81 102
    }
82
83 102
    public function valid()
84
    {
85 102
        return $this->offsetExists($this->position);
86
    }
87
88
    /**
89
     * Adds the passed IMessagePart to the container in the passed position.
90
     *
91
     * If position is not passed or null, the part is added to the end, as the
92
     * last child in the container.
93
     *
94
     * @param IMessagePart $part The part to add
95
     * @param int $position An optional index position (0-based) to add the
96
     *        child at.
97
     */
98 75
    public function add(IMessagePart $part, $position = null)
99
    {
100 75
        $index = ($position === null) ? count($this->children) : $position;
101 75
        array_splice(
102 75
            $this->children,
103 75
            $index,
104 75
            0,
105 75
            [ $part ]
106
        );
107 75
    }
108
109
    /**
110
     * Removes the passed part, and returns the integer position it occupied.
111
     *
112
     * @param IMessagePart $part The part to remove.
113
     * @return int the 0-based position it previously occupied.
114
     */
115 14
    public function remove(IMessagePart $part)
116
    {
117 14
        foreach ($this->children as $key => $child) {
118 14
            if ($child === $part) {
119 14
                $this->offsetUnset($key);
120 14
                return $key;
121
            }
122
        }
123
        return null;
124
    }
125
126 104
    public function offsetExists($offset)
127
    {
128 104
        return isset($this->children[$offset]);
129
    }
130
131 77
    public function offsetGet($offset)
132
    {
133 77
        return $this->offsetExists($offset) ? $this->children[$offset] : null;
134
    }
135
136 1
    public function offsetSet($offset, $value)
137
    {
138 1
        if (!$value instanceof IMessagePart) {
139
            throw new InvalidArgumentException(
140
                get_class($value) . ' is not a ZBateson\MailMimeParser\Message\IMessagePart'
141
            );
142
        }
143 1
        $index = ($offset === null) ? count($this->children) : $offset;
144 1
        $this->children[$index] = $value;
145 1
        if ($index < $this->position) {
146
            ++$this->position;
147
        }
148 1
    }
149
150 14
    public function offsetUnset($offset)
151
    {
152 14
        array_splice($this->children, $offset, 1);
153 14
        if ($this->position >= $offset) {
154 14
            --$this->position;
155
        }
156 14
    }
157
}
158