<?php

namespace Aweos\Resizer\Classes;

use Aweos\Resizer\Lib\MediaPath;
use Storage;

class FileObserver
{

    public function delete(MediaPath $path): void
    {
        foreach ($path->versions() as $version) {
            Storage::delete($version->get('path'));
        }

        if (empty(Storage::files($path->versionsDirPath()))) {
            Storage::deleteDirectory($path->versionsDirPath());
        }
    }

    public function rename(MediaPath $old, MediaPath $new): void
    {
        foreach ($old->versions() as $version) {
            Storage::move(
                $version->get('path'),
                $new->versionsDirPath().'/'.$new->filename().$version->get('size').$version->get('ext')
            );
        }
    }

}