<commit_before><?php

namespace Shopware\PluginCreator\Services;

use Shopware\PluginCreator\Services\IoAdapter\IoAdapter;
use Shopware\PluginCreator\Services\TemplateFileProvider\ApiFileProvider;
use Shopware\PluginCreator\Services\TemplateFileProvider\BackendControllerFileProvider;
use Shopware\PluginCreator\Services\TemplateFileProvider\BackendFileProvider;
use Shopware\PluginCreator\Services\TemplateFileProvider\CommandFileProvider;
use Shopware\PluginCreator\Services\TemplateFileProvider\ControllerPathFileProvider;
use Shopware\PluginCreator\Services\TemplateFileProvider\DefaultFileProvider;
use Shopware\PluginCreator\Services\TemplateFileProvider\FileProviderInterface;
use Shopware\PluginCreator\Services\TemplateFileProvider\FilterFileProvider;
use Shopware\PluginCreator\Services\TemplateFileProvider\FrontendFileProvider;
use Shopware\PluginCreator\Services\TemplateFileProvider\ModelFileProvider;
use Shopware\PluginCreator\Services\TemplateFileProvider\WidgetFileProvider;
use Shopware\PluginCreator\Struct\Configuration;

class Generator
{
    const TEMPLATE_DIRECTORY = 'template';

    protected $outputDirectory = null;
    /**
     * @var Configuration
     */
    private $configuration;
    /**
     * @var Template
     */
    private $template;
    /**
     * @var NameGenerator
     */
    private $nameGenerator;
    /**
     * @var IoAdapter
     */
    private $ioAdapter;

    /**
     * @param IoAdapter     $ioAdapter
     * @param Configuration $configuration
     * @param NameGenerator $nameGenerator
     * @param Template      $template
     */
    public function __construct(IoAdapter $ioAdapter, Configuration $configuration, NameGenerator $nameGenerator, Template $template)
    {
        $this->configuration = $configuration;
        $this->template = $template;
        $this->nameGenerator = $nameGenerator;
        $this->ioAdapter = $ioAdapter;
    }

    /**
     * Returns path where the plugin should be created
     *
     * @return string
     */
    public function getOutputDirectory()
    {
        if (is_null($this->outputDirectory)) {
            $basePath = getcwd();
            $this->outputDirectory = $basePath . '/' . $this->configuration->name . '/';
        }

        return $this->outputDirectory;
    }

    public function setOutputDirectory($path)
    {
        $this->outputDirectory = $path;
    }

    /**
     * Creates the actual plugin from template files
     */
    public function run()
    {
        $this->configureTemplate();

        $path = $this->getOutputDirectory();
        if ($this->ioAdapter->exists($path)) {
            throw new \RuntimeException("Could not create »{$path}«. Directory already exists");
        }
        $this->ioAdapter->createDirectory($path);

        $this->processTemplateFiles();
    }

    /**
     * Creates files from an array of template files
     *
     * @param $files
     */
    private function createFilesFromTemplate($files)
    {
        foreach ($files as $from => $to) {
            $fileContent = $this->template->fetch($from);

            $this->ioAdapter->createDirectory(dirname($this->getOutputDirectory() . $to));

            $this->ioAdapter->createFile($this->getOutputDirectory() . $to, $fileContent);
        }
    }

    /**
     * Creates a given directory
     *
     * @param $path
     * @param  bool              $throwExceptionIfExists
     * @throws \RuntimeException
     */
    private function createDirectory($path, $throwExceptionIfExists = false)
    {
        if (file_exists($path)) {
            if (!$throwExceptionIfExists) {
                return;
            }
            throw new \RuntimeException("Could not create »{$path}«. Directory already exists");
        }

        $success = mkdir($path, 0777, true);

        if (!$success) {
            throw new \RuntimeException("Could not create »{$path}«. Check your directory permission");
        }
    }

    /**
     * setup the template<commit_msg>Remove unused method<commit_after><?php

namespace Shopware\PluginCreator\Services;

use Shopware\PluginCreator\Services\IoAdapter\IoAdapter;
use Shopware\PluginCreator\Services\TemplateFileProvider\ApiFileProvider;
use Shopware\PluginCreator\Services\TemplateFileProvider\BackendControllerFileProvider;
use Shopware\PluginCreator\Services\TemplateFileProvider\BackendFileProvider;
use Shopware\PluginCreator\Services\TemplateFileProvider\CommandFileProvider;
use Shopware\PluginCreator\Services\TemplateFileProvider\ControllerPathFileProvider;
use Shopware\PluginCreator\Services\TemplateFileProvider\DefaultFileProvider;
use Shopware\PluginCreator\Services\TemplateFileProvider\FileProviderInterface;
use Shopware\PluginCreator\Services\TemplateFileProvider\FilterFileProvider;
use Shopware\PluginCreator\Services\TemplateFileProvider\FrontendFileProvider;
use Shopware\PluginCreator\Services\TemplateFileProvider\ModelFileProvider;
use Shopware\PluginCreator\Services\TemplateFileProvider\WidgetFileProvider;
use Shopware\PluginCreator\Struct\Configuration;

class Generator
{
    const TEMPLATE_DIRECTORY = 'template';

    protected $outputDirectory = null;
    /**
     * @var Configuration
     */
    private $configuration;
    /**
     * @var Template
     */
    private $template;
    /**
     * @var NameGenerator
     */
    private $nameGenerator;
    /**
     * @var IoAdapter
     */
    private $ioAdapter;

    /**
     * @param IoAdapter     $ioAdapter
     * @param Configuration $configuration
     * @param NameGenerator $nameGenerator
     * @param Template      $template
     */
    public function __construct(IoAdapter $ioAdapter, Configuration $configuration, NameGenerator $nameGenerator, Template $template)
    {
        $this->configuration = $configuration;
        $this->template = $template;
        $this->nameGenerator = $nameGenerator;
        $this->ioAdapter = $ioAdapter;
    }

    /**
     * Returns path where the plugin should be created
     *
     * @return string
     */
    public function getOutputDirectory()
    {
        if (is_null($this->outputDirectory)) {
            $basePath = getcwd();
            $this->outputDirectory = $basePath . '/' . $this->configuration->name . '/';
        }

        return $this->outputDirectory;
    }

    public function setOutputDirectory($path)
    {
        $this->outputDirectory = $path;
    }

    /**
     * Creates the actual plugin from template files
     */
    public function run()
    {
        $this->configureTemplate();

        $path = $this->getOutputDirectory();
        if ($this->ioAdapter->exists($path)) {
            throw new \RuntimeException("Could not create »{$path}«. Directory already exists");
        }
        $this->ioAdapter->createDirectory($path);

        $this->processTemplateFiles();
    }

    /**
     * Creates files from an array of template files
     *
     * @param $files
     */
    private function createFilesFromTemplate($files)
    {
        foreach ($files as $from => $to) {
            $fileContent = $this->template->fetch($from);

            $this->ioAdapter->createDirectory(dirname($this->getOutputDirectory() . $to));

            $this->ioAdapter->createFile($this->getOutputDirectory() . $to, $fileContent);
        }
    }

    /**
     * setup the template
{'fraction_non_alphanumeric': 0.11071526445601534, 'fraction_numerical': 0.0005480953685941354, 'mean_word_length': 2.9668478260869566, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 55, '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': '2274285', 'n_tokens_mistral': 1919, 'n_tokens_neox': 1890, 'n_words': 383}