Passed
Pull Request — master (#171)
by Zaahid
07:22 queued 03:34
created

MultiPart::getPartCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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 ZBateson\MailMimeParser\MailMimeParser;
10
use ZBateson\MailMimeParser\Message\PartStreamContainer;
11
use ZBateson\MailMimeParser\Message\PartFilter;
12
use Iterator;
13
use AppendIterator;
14
use ArrayIterator;
15
use RecursiveIteratorIterator;
16
17
/**
18
 * A message part that contains children.
19
 *
20
 * @author Zaahid Bateson
21
 */
22
abstract class MultiPart extends MessagePart implements IMultiPart
23
{
24
    /**
25
     * @var PartChildrenContainer child part container
26
     */
27
    protected $partChildrenContainer;
28
29 138
    public function __construct(
30
        IMimePart $parent = null,
31
        PartStreamContainer $streamContainer = null,
32
        PartChildrenContainer $partChildrenContainer = null
33
    ) {
34 138
        parent::__construct($streamContainer, $parent);
0 ignored issues
show
Bug introduced by
It seems like $streamContainer can also be of type null; however, parameter $streamContainer of ZBateson\MailMimeParser\...sagePart::__construct() does only seem to accept ZBateson\MailMimeParser\...age\PartStreamContainer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
        parent::__construct(/** @scrutinizer ignore-type */ $streamContainer, $parent);
Loading history...
35 138
        if ($partChildrenContainer === null) {
36
            $di = MailMimeParser::getDependencyContainer();
37
            $partChildrenContainer = $di['ZBateson\MailMimeParser\Message\PartChildrenContainer'];
38
        }
39 138
        $this->partChildrenContainer = $partChildrenContainer;
40 138
    }
41
42 108
    private function getAllPartsIterator()
43
    {
44 108
        $iter = new AppendIterator();
45 108
        $iter->append(new ArrayIterator([ $this ]));
46 108
        $iter->append(new RecursiveIteratorIterator($this->partChildrenContainer, RecursiveIteratorIterator::SELF_FIRST));
47 108
        return $iter;
48
    }
49
50 98
    private function iteratorFindAt(Iterator $iter, $index, $fnFilter = null)
51
    {
52 98
        $pos = 0;
53 98
        foreach ($iter as $part) {
54 98
            if (($fnFilter === null || $fnFilter($part))) {
55 98
                if ($index === $pos) {
56 98
                    return $part;
57
                }
58 71
                ++$pos;
59
            }
60
        }
61 17
    }
62
63 90
    public function getPart($index, $fnFilter = null)
64
    {
65 90
        return $this->iteratorFindAt(
66 90
            $this->getAllPartsIterator(),
67 90
            $index,
68 90
            $fnFilter
69
        );
70
    }
71
72 101
    public function getAllParts($fnFilter = null)
73
    {
74 101
        $array = iterator_to_array($this->getAllPartsIterator(), false);
75 101
        if ($fnFilter !== null) {
76 63
            return array_values(array_filter($array, $fnFilter));
77
        }
78 78
        return $array;
79
    }
80
81 4
    public function getPartCount($fnFilter = null)
82
    {
83 4
        return count($this->getAllParts($fnFilter));
84
    }
85
86 24
    public function getChild($index, $fnFilter = null)
87
    {
88 24
        return $this->iteratorFindAt(
89 24
            $this->partChildrenContainer,
90 24
            $index,
91 24
            $fnFilter
92
        );
93
    }
94
95 79
    public function getChildIterator()
96
    {
97 79
        return $this->partChildrenContainer;
98
    }
99
100 101
    public function getChildParts($fnFilter = null)
101
    {
102 101
        $array = iterator_to_array($this->partChildrenContainer, false);
103 101
        if ($fnFilter !== null) {
104 3
            return array_values(array_filter($array, $fnFilter));
105
        }
106 100
        return $array;
107
    }
108
109 97
    public function getChildCount($fnFilter = null)
110
    {
111 97
        return count($this->getChildParts($fnFilter));
112
    }
113
114 10
    public function getPartByMimeType($mimeType, $index = 0)
115
    {
116 10
        return $this->getPart($index, PartFilter::fromContentType($mimeType));
117
    }
118
119 1
    public function getAllPartsByMimeType($mimeType)
120
    {
121 1
        return $this->getAllParts(PartFilter::fromContentType($mimeType));
122
    }
123
124 1
    public function getCountOfPartsByMimeType($mimeType)
125
    {
126 1
        return $this->getPartCount(PartFilter::fromContentType($mimeType));
127
    }
128
129 2
    public function getPartByContentId($contentId)
130
    {
131 2
        $sanitized = preg_replace('/^\s*<|>\s*$/', '', $contentId);
132
        return $this->getPart(0, function (IMessagePart $part) use ($sanitized) {
133 2
            $cid = $part->getContentId();
134 2
            return ($cid !== null && strcasecmp($cid, $sanitized) === 0);
135 2
        });
136
    }
137
138 22
    public function addChild(IMessagePart $part, $position = null)
139
    {
140 22
        if ($part !== $this) {
0 ignored issues
show
introduced by
The condition $part !== $this is always true.
Loading history...
141 22
            $part->parent = $this;
0 ignored issues
show
Bug introduced by
Accessing parent on the interface ZBateson\MailMimeParser\Message\IMessagePart suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
142 22
            $this->partChildrenContainer->add($part, $position);
143 22
            $this->notify();
144
        }
145 22
    }
146
147 19
    public function removePart(IMessagePart $part)
148
    {
149 19
        $parent = $part->getParent();
150 19
        if ($this !== $parent && $parent !== null) {
151 6
            return $parent->removePart($part);
152
        } else {
153 19
            $position = $this->partChildrenContainer->remove($part);
154 19
            if ($position !== null) {
0 ignored issues
show
introduced by
The condition $position !== null is always true.
Loading history...
155 19
                $this->notify();
156
            }
157 19
            return $position;
158
        }
159
    }
160
161 3
    public function removeAllParts($fnFilter = null)
162
    {
163 3
        $parts = $this->getAllParts($fnFilter);
164 3
        $count = count($parts);
165 3
        foreach ($parts as $part) {
166 3
            if ($part === $this) {
167 2
                --$count;
168 2
                continue;
169
            }
170 3
            $this->removePart($part);
171
        }
172 3
        return $count;
173
    }
174
}
175