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

InvalidArchiveException::getZipErrorString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 5
rs 10
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