<?php

namespace DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Service\Logger;

use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\File\FileObject;
/**
 * Class ExportLoggerService.
 *
 * @package WPDesk\Library\DropshippingExport\Service\Logger
 */
class ExportLoggerService
{
    /**
     * @var array
     */
    private $messages = [];
    public function error($message)
    {
        $this->add_message('error', $message);
    }
    public function notice($message)
    {
        $this->add_message('notice', $message);
    }
    public function debug($message)
    {
        $this->add_message('debug', $message);
    }
    public function flush_to_file(FileObject $file)
    {
        $fp = fopen($file->getRealPath(), 'a');
        fwrite($fp, $this->get_formated_messages());
        fclose($fp);
    }
    public function get_formated_messages(): string
    {
        $str = '';
        foreach ($this->messages as $message) {
            $str .= $this->format_data($message['type'], $message['message'], $message['time']) . "\n";
        }
        return $str;
    }
    private function format_data(string $type, string $message, int $time): string
    {
        return '[' . date('d-m-Y H:i:s', $time) . '] ' . strtoupper($type) . ': ' . $message;
    }
    private function add_message(string $type, string $message)
    {
        $this->messages[] = ['type' => $type, 'message' => $message, 'time' => time()];
    }
}
