<?php

namespace App\Providers;

use App;
use Config;
use DB;
use Illuminate\Support\ServiceProvider;
use Request;


class EmailServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register(): void
    {
        if(Request::ip()!=='127.0.0.1' || env('APP_ENV')!=='local')
        {
            $mail = DB::table('email_sender_define')->where('aktivni', '=', 1)->first();
            if ($mail)
            {
                $config = array(
                    'driver'     => $mail->driver,
                    'host'       => $mail->host,
                    'port'       => $mail->port,
                    'from'       => array('address' => $mail->username_email, 'name' => $mail->username_email),
                    'encryption' => $mail->encryption,
                    'username'   => $mail->username_email,
                    'password'   => $mail->password,
                    'sendmail'   => '/usr/sbin/sendmail -bs',
                    'pretend'    => false,
                );
                Config::set('mail', $config);
            }
        }

    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot(): void
    {
        $this->register();
    }
}
