<commit_before><?php

namespace TightenCo\Jigsaw\Loaders;

use TightenCo\Jigsaw\Collection\CollectionRemoteItem;
use TightenCo\Jigsaw\File\Filesystem;

class CollectionRemoteItemLoader
{
    private $files;
    private $tempDirectories;

    public function __construct(Filesystem $files)
    {
        $this->files = $files;
    }

    public function write($collections, $source)
    {
        collect($collections)->each(function ($collection, $collectionName) use ($source) {
            $items = $this->getItems($collection);

            if (collect($items)->count()) {
                $this->writeTempFiles($items, $this->createTempDirectory($source, $collectionName), $collectionName);
            }
        });
    }

    private function createTempDirectory($source, $collectionName)
    {
        $tempDirectory = $source . '/_' . $collectionName . '/_tmp';
        $this->prepareDirectory($tempDirectory, true);
        $this->tempDirectories[] = $tempDirectory;

        return $tempDirectory;
    }

    public function cleanup()
    {
        collect($this->tempDirectories)->each(function ($path) {
            $this->files->deleteDirectory($path);

            $tempParent = $this->files->dirname($path);
            if ( $this->files->isDirectory($tempParent) ) {
                // Get all files in this directory.
                $files = $this->files->files($tempParent);
                // Check if directory is empty.
                if (empty($files)) {
                    // Yes, delete the directory.
                    $this->files->deleteDirectory($tempParent);
                }

              }
        });
    }

    private function getItems($collection)
    {
        if (! $collection->items) {
            return;
        }

        return is_callable($collection->items) ?
            $collection->items->__invoke() :
            $collection->items->toArray();
    }

    private function prepareDirectory($directory, $clean = false)
    {
        if (! $this->files->isDirectory($directory)) {
            $this->files->makeDirectory($directory, 0755, true);
        }

        if ($clean) {
            $this->files->cleanDirectory($directory);<commit_msg>Use `isEmptyDirectory` method<commit_after><?php

namespace TightenCo\Jigsaw\Loaders;

use TightenCo\Jigsaw\Collection\CollectionRemoteItem;
use TightenCo\Jigsaw\File\Filesystem;

class CollectionRemoteItemLoader
{
    private $files;
    private $tempDirectories;

    public function __construct(Filesystem $files)
    {
        $this->files = $files;
    }

    public function write($collections, $source)
    {
        collect($collections)->each(function ($collection, $collectionName) use ($source) {
            $items = $this->getItems($collection);

            if (collect($items)->count()) {
                $this->writeTempFiles($items, $this->createTempDirectory($source, $collectionName), $collectionName);
            }
        });
    }

    private function createTempDirectory($source, $collectionName)
    {
        $tempDirectory = $source . '/_' . $collectionName . '/_tmp';
        $this->prepareDirectory($tempDirectory, true);
        $this->tempDirectories[] = $tempDirectory;

        return $tempDirectory;
    }

    public function cleanup()
    {
        collect($this->tempDirectories)->each(function ($path) {
            $this->files->deleteDirectory($path);

            if ($this->files->isEmptyDirectory($parent = $this->files->dirname($path))) {
                $this->files->deleteDirectory($parent);
            }
        });
    }

    private function getItems($collection)
    {
        if (! $collection->items) {
            return;
        }

        return is_callable($collection->items) ?
            $collection->items->__invoke() :
            $collection->items->toArray();
    }

    private function prepareDirectory($directory, $clean = false)
    {
        if (! $this->files->isDirectory($directory)) {
            $this->files->makeDirectory($directory, 0755, true);
        }

        if ($clean) {
            $this->files->cleanDirectory($directory);
{'fraction_non_alphanumeric': 0.14024835646457268, 'fraction_numerical': 0.0019478938397857316, 'mean_word_length': 2.3209377526273243, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '813031', 'n_tokens_mistral': 1112, 'n_tokens_neox': 1074, 'n_words': 187}