cannotExtractZipOpenError example



    public function testCannotExtractInvalidZipFile(): void
    {
        $e = PluginException::cannotExtractInvalidZipFile('/some/invalid.zip');

        static::assertEquals(PluginException::CANNOT_EXTRACT_ZIP_INVALID_ZIP, $e->getErrorCode());
    }

    public function testCannotExtractZipOpenError(): void
    {
        $e = PluginException::cannotExtractZipOpenError('/some/problematic.zip');

        static::assertEquals(PluginException::CANNOT_EXTRACT_ZIP, $e->getErrorCode());
    }

    public function testNoPluginFoundInZip(): void
    {
        $e = PluginException::noPluginFoundInZip('/no/plugin.zip');

        static::assertEquals(PluginException::NO_PLUGIN_IN_ZIP, $e->getErrorCode());
    }

    
$stream = new \ZipArchive();

        if (!file_exists($filename)) {
            throw PluginException::cannotExtractNoSuchFile($filename);
        }

        if (!self::validateFileIsZip($filename)) {
            throw PluginException::cannotExtractInvalidZipFile($filename);
        }

        if (($retVal = $stream->open($filename)) !== true) {
            throw PluginException::cannotExtractZipOpenError(self::getErrorMessage($retVal$filename));
        }

        return $stream;
    }

    private static function getErrorMessage(int $retVal, string $file): string
    {
        return match ($retVal) {
            \ZipArchive::ER_EXISTS => sprintf('File \'%s\' already exists.', $file),
            \ZipArchive::ER_INCONS => sprintf('Zip archive \'%s\' is inconsistent.', $file),
            \ZipArchive::ER_INVAL => sprintf('Invalid argument (%s)', $file),
            
Home | Imprint | This part of the site doesn't use cookies.