<commit_before><?php

namespace TmpFile;

use TmpFile\DeferredPurgeHandler\DummyDeferredPurgeHandler;
use TmpFile\Exception\TmpFileIOException;
use TmpFile\Exception\TmpFileCreateException;

final class TmpFileManager
{
    /**
     * @var ContainerInterface $container
     * @var TmpFileHandlerInterface $tmpFileHandler
     * @var ConfigInterface $config
     */
    private
        $container,
        $tmpFileHandler,
        $config
    ;

    public function __construct(
        ContainerInterface $container,
        TmpFileHandlerInterface $tmpFileHandler,
        ConfigInterface $config
    ) {
        $this->container = $container;
        $this->tmpFileHandler = $tmpFileHandler;
        $this->config = $config;

        $this->initDeferredPurgeHandler();
    }

    private function initDeferredPurgeHandler(): void
    {
        $deferredPurgeHandler = $this->config->getDeferredPurgeHandler();

        if (!$deferredPurgeHandler instanceof DummyDeferredPurgeHandler) {
            $deferredPurgeHandler($this);
        }
    }

    /**
     * @return TmpFile
     *
     * @throws TmpFileIOException
     * @throws TmpFileCreateException
     */
    public function createTmpFile(): TmpFile
    {
        $temporaryDirectory = $this->config->getTemporaryDirectory();
        $tmpFilePrefix = $this->config->getTmpFilePrefix();

        $fileName = $this->tmpFileHandler->getTmpFileName($temporaryDirectory, $tmpFilePrefix);

        try {
            $tmpFile = $this->makeTmpFile($fileName);
        } catch (\ReflectionException $e) {
            throw new TmpFileCreateException(
                $e->getMessage()
            );
        }

        $this->container->addTmpFile($tmpFile);

        return $tmpFile;
    }

    /**
     * @param string $realPath
     *
     * @return TmpFile
     *
     * @throws \ReflectionException
     */
    protected function makeTmpFile(string $realPath): TmpFile
    {
        $tmpFileReflection = new \ReflectionClass(TmpFile::class);

        /** @var TmpFile $tmpFile */
        $tmpFile = $tmpFileReflection->newInstanceWithoutConstructor();

        $fileName = $tmpFileReflection->getProperty('fileName');

        $fileName->setAccessible(true);

        $fileName->setValue($tmpFile, $realPath);

        return $tmpFile;
    }

    /**
     * @param callable $callback
     *
     * @throws TmpFileIOException
     * @throws TmpFileCreateException
     */
    public function createTmpFileContext(callable $callback): void
    {
        $tmpFile = $this->createTmpFile();

        try {
            $callback($tmpFile);
        } finally {
            $this->removeTmpFile($tmpFile);
        }
    }

    /**
     * @param TmpFile $tmpFile
     *
     * @throws TmpFileIOException
     */
    public function removeTmpFile(TmpFile $tmpFile): void
    {
        if ($this->container->hasTmpFile($tmpFile)) {
            $this->container->removeTmpFile($tmpFile);
        }

        if ($this->tmpFileHandler->existsTmpFile($tmpFile)) {
            $this->tmpFileHandler->removeTmpFile($tmpFile);
        }
    }

    /**
     * @throws TmpFileIOException
     */
    public function purge(): void
    {
        $tmpFiles = $this->container->getTmpFiles();
        $checkUnclosedResources = $this->config->getCheckUnclosedResources();

        if ($checkUnclosedResources) {
            $this->closeOpenedResources($tmpFiles);
        }

        foreach ($tmpFiles as $tmpFile) {<commit_msg>Added public getters to TmpFileManager<commit_after><?php

namespace TmpFile;

use TmpFile\DeferredPurgeHandler\DummyDeferredPurgeHandler;
use TmpFile\Exception\TmpFileIOException;
use TmpFile\Exception\TmpFileCreateException;

final class TmpFileManager
{
    /**
     * @var ContainerInterface $container
     * @var TmpFileHandlerInterface $tmpFileHandler
     * @var ConfigInterface $config
     */
    private
        $container,
        $tmpFileHandler,
        $config
    ;

    public function __construct(
        ContainerInterface $container,
        TmpFileHandlerInterface $tmpFileHandler,
        ConfigInterface $config
    ) {
        $this->container = $container;
        $this->tmpFileHandler = $tmpFileHandler;
        $this->config = $config;

        $this->initDeferredPurgeHandler();
    }

    private function initDeferredPurgeHandler(): void
    {
        $deferredPurgeHandler = $this->getConfig()->getDeferredPurgeHandler();

        if (!$deferredPurgeHandler instanceof DummyDeferredPurgeHandler) {
            $deferredPurgeHandler($this);
        }
    }

    public function getContainer(): ContainerInterface
    {
        return $this->container;
    }

    public function getTmpFileHandler(): TmpFileHandlerInterface
    {
        return $this->tmpFileHandler;
    }

    public function getConfig(): ConfigInterface
    {
        return $this->config;
    }

    /**
     * @return TmpFile
     *
     * @throws TmpFileIOException
     * @throws TmpFileCreateException
     */
    public function createTmpFile(): TmpFile
    {
        $temporaryDirectory = $this->getConfig()->getTemporaryDirectory();
        $tmpFilePrefix = $this->getConfig()->getTmpFilePrefix();

        $fileName = $this->getTmpFileHandler()->getTmpFileName($temporaryDirectory, $tmpFilePrefix);

        try {
            $tmpFile = $this->makeTmpFile($fileName);
        } catch (\ReflectionException $e) {
            throw new TmpFileCreateException(
                $e->getMessage()
            );
        }

        $this->getContainer()->addTmpFile($tmpFile);

        return $tmpFile;
    }

    /**
     * @param string $realPath
     *
     * @return TmpFile
     *
     * @throws \ReflectionException
     */
    protected function makeTmpFile(string $realPath): TmpFile
    {
        $tmpFileReflection = new \ReflectionClass(TmpFile::class);

        /** @var TmpFile $tmpFile */
        $tmpFile = $tmpFileReflection->newInstanceWithoutConstructor();

        $fileName = $tmpFileReflection->getProperty('fileName');

        $fileName->setAccessible(true);

        $fileName->setValue($tmpFile, $realPath);

        return $tmpFile;
    }

    /**
     * @param callable $callback
     *
     * @throws TmpFileIOException
     * @throws TmpFileCreateException
     */
    public function createTmpFileContext(callable $callback): void
    {
        $tmpFile = $this->createTmpFile();

        try {
            $callback($tmpFile);
        } finally {
            $this->removeTmpFile($tmpFile);
        }
    }

    /**
     * @param TmpFile $tmpFile
     *
     * @throws TmpFileIOException
     */
    public function removeTmpFile(TmpFile $tmpFile): void
    {
        if ($this->getContainer()->hasTmpFile($tmpFile)) {
            $this->getContainer()->removeTmpFile($tmpFile);
        }

        if ($this->getTmpFileHandler()->existsTmpFile($tmpFile)) {
            $this->getTmpFileHandler()->removeTmpFile($tmpFile);
        }
    }

    /**
     * @throws TmpFileIOException
     */
    public function purge(): void
    {
        $tmpFiles = $this->getContainer()->getTmpFiles();
        $checkUnclosedResources = $this->getConfig()->getCheckUnclosedResources();

        if ($checkUnclosedResources) {
            $this->closeOpenedResources($tmpFiles);
        }

        foreach ($tmpFiles as $tmpFile) {
{'fraction_non_alphanumeric': 0.11670543165959775, 'fraction_numerical': 0.0, 'mean_word_length': 2.43031440638198, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 76, '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': '7021412', 'n_tokens_mistral': 2097, 'n_tokens_neox': 2049, 'n_words': 342}