cannotExtractNoSuchFile example

class PluginExceptionTest extends TestCase
{
    public function testCannotDeleteManaged(): void
    {
        $e = PluginException::cannotDeleteManaged('MyPlugin');

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

    public function testCannotExtractNoSuchFile(): void
    {
        $e = PluginException::cannotExtractNoSuchFile('/some/file/that/does/not/exist.zip');

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

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

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

    
#[Package('core')] class ZipUtils
{
    private const HEADER_SIGNATURE = '504b0304';

    public static function openZip(string $filename): \ZipArchive
    {
        $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;
    }
Home | Imprint | This part of the site doesn't use cookies.