Passed
Pull Request — master (#54)
by Aleksei
15:02
created

CumulativeException::getExceptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Cycle\Exception;
6
7
use RuntimeException;
8
use Throwable;
9
10
class CumulativeException extends RuntimeException
11
{
12
    /** @var Throwable[] */
13
    private array $exceptions;
14
15
    public function __construct(Throwable ...$exceptions)
16
    {
17
        $this->exceptions = $exceptions;
18
        $count = count($exceptions);
19
        $message = $count === 1 ? 'One exception was thrown.' : $count . ' exceptions were thrown.';
20
        parent::__construct($message . $this->getMessageDetails());
21
    }
22
23
    /**
24
     * @return Throwable[]
25
     */
26
    public function getExceptions(): array
27
    {
28
        return $this->exceptions;
29
    }
30
31
    private function getMessageDetails(): string
32
    {
33
        $result = '';
34
        $num = 0;
35
        foreach ($this->exceptions as $exception) {
36
            $result .= sprintf(
37
                "\n\n%d) %s:%s\n[%s] #%d: %s",
38
                ++$num,
39
                $exception->getFile(),
40
                $exception->getLine(),
41
                get_class($exception),
42
                $exception->getCode(),
43
                $exception->getMessage()
44
            );
45
        }
46
        return $result;
47
    }
48
}
49