File: /home4/cca63905/public_html/nueva/modules/wnetsecurity/src/Environment.php
<?php
/**
* Copyright since 2014 Waynet Sp. z o.o.
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to kontakt@waynet.pl so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop-project.org/ for more information.
*
* @author Waynet Sp. z o.o. <kontakt@waynet.pl>
* @copyright since 2014 Waynet Sp. z o.o.
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
declare(strict_types=1);
namespace Waynet\Security;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\Dotenv\Exception\PathException;
class Environment
{
private const API_URL = 'WNET_SECURITY_API_URL';
private const OAUTH_SERVER_URL = 'WNET_SECURITY_OAUTH_SERVER_URL';
private const OAUTH_TOKEN_ENDPOINT_URI = 'WNET_SECURITY_OAUTH_TOKEN_ENDPOINT_URI';
private const OAUTH_CLIENT_REGISTRATION_ENDPOINT_URI = 'WNET_SECURITY_OAUTH_CLIENT_REGISTRATION_ENDPOINT';
private const PS_BILLING_SANDBOX = 'WNET_SECURITY_PS_BILLING_SANDBOX';
private $path;
private $environment;
private $dotenv;
private $loaded = false;
private $values = [];
public function __construct(
string $directory,
string $environment = null,
Dotenv $dotenv = null
) {
if (null === $environment) {
$environment = defined('_PS_MODE_DEV_') && _PS_MODE_DEV_ ? 'dev' : 'prod';
}
$this->path = rtrim($directory, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '.env';
$this->environment = $environment;
$this->dotenv = $dotenv ?? new Dotenv();
}
public function getApiUrl(): string
{
return (string) $this->get(self::API_URL);
}
public function getOAuthServerUrl(): string
{
return (string) $this->get(self::OAUTH_SERVER_URL);
}
public function getOAuthTokenEndpointUri(): string
{
return (string) $this->get(self::OAUTH_TOKEN_ENDPOINT_URI);
}
public function getOAuthClientRegistrationEndpointUri(): string
{
return (string) $this->get(self::OAUTH_CLIENT_REGISTRATION_ENDPOINT_URI);
}
public function useSandboxBilling(): bool
{
return (bool) $this->get(self::PS_BILLING_SANDBOX);
}
public function getPsAccountsVersion(): string
{
return \Tools::version_compare($this->getPrestaShopVersion(), '8.0.0')
? '5.0'
: '6.0';
}
public function getPrestaShopVersion(): string
{
return _PS_VERSION_;
}
private function get(string $key)
{
if (array_key_exists($key, $_ENV)) {
return $_ENV[$key];
}
if (!$this->loaded) {
$this->load();
}
if (!isset($this->values[$key])) {
throw new \RuntimeException(sprintf('Environment variable "%s" is not set', $key));
}
return $this->values[$key];
}
private function load(): void
{
$this->values = array_merge(...array_map(function ($path) {
if (!is_readable($path) || is_dir($path)) {
throw new PathException($path);
}
return $this->dotenv->parse(\Tools::file_get_contents($path), $path);
}, $this->getPaths()));
$this->loaded = true;
}
/** @return string[] */
private function getPaths(): array
{
$paths = [];
if (is_file($this->path)) {
$paths[] = $this->path;
}
if (is_file($path = $this->path . '.local')) {
$paths[] = $path;
}
if (is_file($path = $this->path . '.' . $this->environment)) {
$paths[] = $path;
}
if (is_file($path .= '.local')) {
$paths[] = $path;
}
return $paths;
}
}