MOON
Server: Apache
System: Linux server1.studioinfinity.com.br 2.6.32-954.3.5.lve1.4.90.el6.x86_64 #1 SMP Tue Feb 21 12:26:30 UTC 2023 x86_64
User: artinside (517)
PHP: 7.4.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: /home/artinside/www/raquel/vendor/frenet/frenet-php/src/Config/Service.php
<?php

declare(strict_types = 1);

namespace Frenet\Config;

use Frenet\Framework\Exception\WrongDataTypeException;

/**
 * Class Service
 *
 * @package Frenet\Config
 */
class Service implements ConfigInterface
{
    /**
     * @var array
     */
    private $validProtocols = [
        'http', 'https'
    ];

    /**
     * @var string
     */
    private $protocol = 'http';

    /**
     * @var string
     */
    private $defaultHost = 'api.frenet.com.br';

    /**
     * @var string
     */
    private $host = null;

    /**
     * @return string
     */
    public function getProtocol()
    {
        return (string) $this->protocol;
    }

    /**
     * @param string $protocol
     *
     * @return $this
     *
     * @throws WrongDataTypeException
     */
    public function setProtocol($protocol)
    {
        $protocol = trim(strtolower($protocol));

        if (!$this->validateProtocol($protocol)) {
            throw new WrongDataTypeException('Invalid protocol provided.');
        }

        $this->protocol = $protocol;

        return $this;
    }

    /**
     * @return string
     */
    public function getHost()
    {
        return $this->getHostname();
    }

    /**
     * @return string
     */
    public function getHostname()
    {
        if (empty($this->host)) {
            return $this->defaultHost;
        }

        return (string) $this->normalizeHostname($this->host);
    }

    /**
     * @param string $host
     *
     * @return $this
     *
     * @throws WrongDataTypeException
     */
    public function setHostname($host)
    {
        if (!$this->validateHostname($host)) {
            throw new WrongDataTypeException('Invalid hostname format provided.');
        }

        $this->host = $this->normalizeHostname($host);

        return $this;
    }

    /**
     * @param string $host
     *
     * @return bool
     */
    private function validateHostname($host)
    {
        $host = $this->normalizeHostname($host);

        if (empty($host)) {
            return false;
        }

        return true;
    }

    /**
     * @param string $host
     *
     * @return string
     */
    private function normalizeHostname($host)
    {
        $host = trim($host);
        $host = strtolower($host);

        return $host;
    }

    /**
     * @param string $protocol
     *
     * @return bool
     */
    private function validateProtocol($protocol)
    {
        $protocol = strtolower($protocol);

        if (empty($protocol)) {
            return false;
        }

        if (!in_array($protocol, $this->validProtocols)) {
            return false;
        }

        return true;
    }
}