Passed
Push — spaghetti ( b73260...8fd548 )
by simpletoimplement
02:04
created

InvalidArchiveException   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 17
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getErrorMessage() 0 3 1
A getZipErrorString() 0 5 2
1
<?php declare(strict_types = 1);
2
3
namespace Spaghetti\XLSXParser\Exception;
4
5
use ReflectionClass;
6
use ReflectionProperty;
7
use RuntimeException;
8
use Throwable;
9
use ZipArchive;
10
11
use function array_flip;
12
use function array_key_exists;
13
use function sprintf;
14
15
class InvalidArchiveException extends RuntimeException
16
{
17
    public function __construct(int $code, ?Throwable $previous = null)
18
    {
19
        parent::__construct(message: 'Error opening file: ' . $this->getErrorMessage(errorCode: $code), code: 0, previous: $previous);
20
    }
21
22
    private function getErrorMessage(int $errorCode): string
23
    {
24
        return sprintf('An error has occured: %s::%s (%d)', ZipArchive::class, $this->getZipErrorString(value: $errorCode), $errorCode);
25
    }
26
27
    private function getZipErrorString(int $value): string
28
    {
29
        $map = array_flip(array: (new ReflectionClass(objectOrClass: ZipArchive::class))->getConstants(filter: ReflectionProperty::IS_PUBLIC));
30
31
        return array_key_exists(key: $value, array: $map) ? $map[$value] : 'UNKNOWN';
32
    }
33
}
34