Completed
Pull Request — master (#257)
by David de
05:25
created

ExceptionCollection::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 4
cts 5
cp 0.8
rs 9.4286
cc 2
eloc 3
nc 2
nop 1
crap 2.032
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 40
    public function __construct(array $exceptions = [])
23
    {
24 40
        foreach ($exceptions as $exception) {
25
            $this->add($exception);
26 40
        }
27 40
    }
28
29
    /**
30
     * Add an exception to the collection
31
     *
32
     * @param \Exception $e
33
     *
34
     * @return $this
35
     */
36 3
    public function add(\Exception $e)
37
    {
38 3
        if (null == $this->message) {
39 3
            $this->message = $e->getMessage();
40 3
        }
41
42 3
        $this->exceptions[] = $e;
43
44 3
        return $this;
45
    }
46
47
    /**
48
     * Get first exception in collection or null, if there is none.
49
     *
50
     * @return \Exception|null
51
     */
52 2
    public function getFirst()
53
    {
54 2
        if ($this->count() > 0) {
55 2
            return $this->exceptions[0];
56
        }
57
58 1
        return null;
59
    }
60
61
    /**
62
     * Get exception iterator
63
     *
64
     * @return \ArrayIterator
65
     */
66 2
    public function getIterator()
67
    {
68 2
        return new \ArrayIterator($this->exceptions);
69
    }
70
71
    /**
72
     * Get number of exceptions in collection
73
     *
74
     * @return int
75
     */
76 39
    public function count()
77
    {
78 39
        return count($this->exceptions);
79
    }
80
}
81