<?php

namespace Tricks\CampaignMonitor\Helpers;

use Exception;

class Exceptional
{
    /**
     * @param string $key
     * @return mixed
     * @throws Exception
     */
    public static function config(string $key)
    {
        return self::returnOrThrow(config($key), 'config value for \'' . $key . '\'.');
    }

    /**
     * @param $value
     * @param $message
     * @return mixed
     * @throws Exception
     */
    private static function returnOrThrow($value, $message)
    {
        if (is_null($value)) {
            throw new Exception('Missing ' . $message);
        }

        if (empty($value)) {
            throw new Exception('Empty ' . $message);
        }
        return $value;
    }

    /**
     * @param string $key
     * @return mixed
     * @throws Exception
     */
    public static function env(string $key, $default = null)
    {
        return self::returnOrThrow(env($key, $default), 'environment value for \'' . $key . '\'. Check your .env file.');
    }
}
