<?php
/** 
 * @package     VikRestaurants
 * @subpackage  core
 * @author      E4J s.r.l.
 * @copyright   Copyright (C) 2023 E4J s.r.l. All Rights Reserved.
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @link        https://vikwp.com
 */

// No direct access
defined('ABSPATH') or die('No script kiddies please!');

/**
 * Mockup for fake operator record.
 *
 * @since 1.11.1
 */
final class VREOperatorUserMockup extends VREOperatorUser
{
    /**
     * Class constructor.
     *
     * @param  array|object  $operator  The operator data we want to use.
     */
    public function __construct($operator = [])
    {
        // obtain a clean operator table record with default properties
        JTableVRE::addIncludePath(VREADMIN . DIRECTORY_SEPARATOR . 'tables');
        $table = JTableVRE::getInstance('operator', 'VRETable');

        if ($table)
        {
            $table->reset();
            $default = $table->getProperties();

            // get currently logged in user
            $user = JFactory::getUser();

            // extract first and last name
            $names = explode(' ', $user->name ?: 'Guest (site)');

            // get website host name
            $host = JUri::getInstance()->toString(['host']);
            $host = preg_replace("/^www./", '', $host);

            $default['id'] = 0;
            $default['lastname'] = array_pop($names);
            $default['firstname'] = implode(' ', $names);
            $default['email'] = $user->email ?: 'no-reply@' . $host;
            $default['jid'] = $user->id;

            // override default properties with the specified ones
            $operator = array_merge($default, (array) $operator);
        }

        // construct through parent to preserve original behavior
        parent::__construct($operator);
    }
}
