Completed
Pull Request — master (#312)
by David
06:00
created

ExceptionCollection::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the FOSHttpCache package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\HttpCache\Exception;
13
14
/**
15
 * A collection of exceptions that might occur during the flush operation of a
16
 * ProxyClientInterface implementation.
17
 */
18
class ExceptionCollection extends \Exception implements \IteratorAggregate, \Countable, HttpCacheExceptionInterface
19
{
20
    private $exceptions = [];
21
22 35
    public function __construct(array $exceptions = [])
23
    {
24 35
        foreach ($exceptions as $exception) {
25 1
            $this->add($exception);
26 35
        }
27 35
    }
28
29
    /**
30
     * Add an exception to the collection.
31
     *
32
     * @param \Exception $e
33
     *
34
     * @return $this
35
     */
36 6
    public function add(\Exception $e)
37
    {
38 6
        if (null == $this->message) {
39 6
            $this->message = $e->getMessage();
40 6
        }
41
42 6
        $this->exceptions[] = $e;
43
44 6
        return $this;
45
    }
46
47
    /**
48
     * Get first exception in collection or null, if there is none.
49
     *
50
     * @return \Exception|null
51
     */
52 5
    public function getFirst()
53
    {
54 5
        if ($this->count() > 0) {
55 5
            return $this->exceptions[0];
56
        }
57 1
    }
58
59
    /**
60
     * Get exception iterator.
61
     *
62
     * @return \ArrayIterator
63
     */
64 3
    public function getIterator()
65
    {
66 3
        return new \ArrayIterator($this->exceptions);
67
    }
68
69
    /**
70
     * Get number of exceptions in collection.
71
     *
72
     * @return int
73
     */
74 34
    public function count()
75
    {
76 34
        return count($this->exceptions);
77
    }
78
}
79