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

CumulativeException   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 19
c 1
b 0
f 0
dl 0
loc 37
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getMessageDetails() 0 16 2
A getExceptions() 0 3 1
A __construct() 0 6 2
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