<?php

namespace Dipper\Sms\Gateways;

use Dipper\Sms\Contracts\MessageInterface;
use Dipper\Sms\Support\Config;

class ErrorlogGateway extends Gateway
{
    /**
     * Get gateway name.
     *
     * @return string
     */
    public function getName()
    {
        return 'errorlog';
    }

    /**
     * @param array|int|string                             $to
     * @param \Dipper\Sms\Contracts\MessageInterface $message
     * @param \Dipper\Sms\Support\Config             $config
     *
     * @return array
     */
    public function send($to, MessageInterface $message, Config $config)
    {
        if (is_array($to)) {
            $to = implode(',', $to);
        }

        $message = sprintf(
            "[%s] to: %s | message: \"%s\"  | template: \"%s\" | data: %s\n",
            date('Y-m-d H:i:s'),
            $to,
            $message->getContent(),
            $message->getTemplate($this),
            json_encode($message->getData($this))
        );

        $file = $this->config->get('file', ini_get('error_log'));
        $status = error_log($message, 3, $file);

        return compact('status', 'file');
    }
}
