<?php
namespace App\Service;

use Cake\Core\App;

App::className('AppModel', 'Service');

/**
 * Config.php
 * @author tomasznicieja
 *
 * Created by tomasznicieja.
 * Date: 15.01.2018
 * Time: 08:42
 */

class Config extends AppModel
{
    /**
     * @var bool
     */
    public $useTable = false;

    /**
     * @var array
     */
    private $config = array();

    /**
     * @var string
     */
    private $path = '';

    /**
     * Config constructor.
     */
    public function __construct()
    {
        $this->path = __DIR__ . '/../../config/.env';

        if (file_exists($this->path)) {
            $handle = fopen($this->path, "r");
            if ($handle) {
                while (($line = fgets($handle)) !== false) {
                    $lineArray = explode('=', $line, 2);
                    $value = isset($lineArray[1]) ? substr($lineArray[1], 0, strlen($lineArray[1]) - 1) : '';
                    $this->config[$lineArray[0]] = $value;
                }

                fclose($handle);
            }
        }

        return $this->config;
    }

    /**
     * @return array
     */
    public function getConfig()
    {
        return $this->config;
    }

    /**
     * @return string
     */
    public function getPath()
    {
        return $this->path;
    }
}
