<?php
namespace Axdlee\Config;

use Illuminate\Filesystem\Filesystem;

class FileWriter
{
    /**
     * The filesystem instance.
     *
     * @var \Illuminate\Filesystem\Filesystem
     */
    protected $files;

    /**
     * The default configuration path.
     *
     * @var string
     */
    protected $defaultPath;

    /**
     * The config rewriter object.
     *
     * @var \Axdlee\Config\Rewrite
     */
    protected $rewriter;

    /**
     * Create a new file configuration loader.
     *
     * @param  \Illuminate\Filesystem\Filesystem $files
     * @param  string $defaultPath
     */
    public function __construct(Filesystem $files, $defaultPath)
    {
        $this->files = $files;
        $this->defaultPath = $defaultPath;
        $this->rewriter = new Rewrite;
    }

    public function write($item, $value, $filename, $useValidation = true)
    {
        $path = $this->getPath($item, $filename, $useValidation);
        if (!$path)
            return false;

        $contents = $this->files->get($path);
        $contents = $this->rewriter->toContent($contents, [$item => $value], false);
        return !($this->files->put($path, $contents) === false);
    }

    private function getPath($item, $filename, $useValidation = true)
    {
        $file = "{$this->defaultPath}/{$filename}.php";
        if ( $this->files->exists($file) &&
            $this->hasKey($file, $item, $useValidation)
        )
            return $file;

        return null;
    }

    private function hasKey($path, $key, $useValidation = true)
    {
        if ($useValidation) {
            $contents = file_get_contents($path);
            $vars = eval('?>'.$contents);

            $keys = explode('.', $key);

            $isset = false;
            while ($key = array_shift($keys)) {
                $isset = isset($vars[$key]);
                if (is_array($vars[$key])) $vars = $vars[$key]; // Go down the rabbit hole
            }

            return $isset;
        } else {
            return true;
        }
    }
}