<?php

namespace DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Archive;

use ZipArchive;
class ArchvieService
{
    const ZIP_EXTENSION = '.zip';
    /**
     *
     * @var bool
     */
    private $create_new_file = \false;
    public function set_export_as_new(bool $create_new_file)
    {
        $this->create_new_file = $create_new_file;
    }
    public function compress_files(string $path, array $files)
    {
        $zip = new ZipArchive();
        $create_new_file = $this->should_create_new_file($path);
        if ($create_new_file && \false !== realpath($path)) {
            \unlink($path);
        }
        if (!empty($path) && !empty($files)) {
            $create_new_file ? $zip->open($path, ZipArchive::CREATE) : $zip->open($path);
            $zip->setCompressionIndex(0, ZipArchive::CM_STORE);
            foreach ($files as $name => $file) {
                if (!is_dir($file)) {
                    $zip->addFile($file, basename($file));
                }
            }
            $zip->close();
        }
    }
    private function should_create_new_file(string $location): bool
    {
        if ($this->create_new_file) {
            return \true;
        }
        return \false === realpath($location);
    }
}
