<?php

namespace App\Actions\Init;

class EnsureTargetDirectory
{
    public function handle(string $projectDir): void
    {
        if (!file_exists($projectDir)) {
            mkdir($projectDir, 0777, true);
        }

        $files = array_values(array_diff(scandir($projectDir) ?: [], ['.', '..']));

        if ($files !== []) {
            throw new \RuntimeException(sprintf('Target directory `%s` is not empty.', $projectDir));
        }
    }
}
