bebeboutik/override/classes/Environment.php
2017-11-15 17:31:44 +01:00

211 lines
5.3 KiB
PHP

<?php
class Environment
{
const PRODUCTION = 'production';
const DEV = 'development';
const BACKOFFICE = 'backoffice';
const UNDEFINED = 'unknown';
const DOMAIN_NAME = 'bebeboutik';
const DEFAULT_SUB_DOMAIN = 'www';
const DEFAULT_VERSION = 'com';
private $envt;
private $subDomain;
private $topLevelDomain;
private $version;
private $iso_lang;
private static $paymentSubDomain = array(
self::DEV => 'pp',
self::PRODUCTION => 'payment',
self::BACKOFFICE => 'payment',
);
private static $subDomains = array(
self::DEV => array(
'local',
'm.local',
'pp',
'm.pp',
'292567.srv',
'dev',
'api.pp'
),
self::PRODUCTION => array(
'payment',
'www',
'm',
'api',
),
self::BACKOFFICE => array(
'bo',
'bo2',
)
);
/**
* @return bool|string
*/
public function getIsoLang()
{
return $this->iso_lang;
}
/**
* @return boolean
*/
public function isProduction()
{
return $this->envt === Environment::PRODUCTION || $this->envt === Environment::BACKOFFICE ;
}
/**
* @return boolean
*/
public function isBackOffice()
{
return $this->envt === Environment::BACKOFFICE ;
}
/**
* @return boolean
*/
public function isDev()
{
return $this->envt === Environment::DEV;
}
/**
* @return boolean
*/
public function isUnknown()
{
return $this->envt === Environment::UNDEFINED;
}
/**
* @return string
*/
public function getVersion()
{
if (!$this->version) {
$this->topLevelDomain = self::DEFAULT_VERSION;
$this->qualifyExtension();
}
return $this->version;
}
/**
* In this case backoffice is never returned and considered as production
* Use isBackOffice() to make the distinction
* @return string Environment name in full string
*/
public function getEnvironmentName()
{
if ($this->envt === self::BACKOFFICE) {
return self::PRODUCTION;
}
return $this->envt;
}
/**
* @return bool|string
*/
public function getSubDomain()
{
return $this->subDomain;
}
public function __construct()
{
$domain = Tools::getHttpHost();
if (substr_count($domain, '.')) {
$this->subDomain = substr($domain, 0, strpos($domain, '.' . self::DOMAIN_NAME));
$this->topLevelDomain = substr($domain, strpos($domain, self::DOMAIN_NAME) + strlen(self::DOMAIN_NAME) + 1);
$this->qualifySubDomain();
$this->qualifyExtension();
$this->qualifyLanguage();
}
}
/**
* Get default domain
* @return string default domain
*/
public function getDefaultDomain()
{
return Tools::getCurrentUrlProtocolPrefix() . self::DEFAULT_SUB_DOMAIN . '.' . self::DOMAIN_NAME . '.' . $this->getVersion();
}
/**
* Get domain for lang ID
* @param int $id_lang Lang ID
* @param bool $extension_only return domain extension only
* @return string
*/
public static function getDomainForLangId($id_lang, $extension_only = false)
{
$returnedValue = '';
if (!$extension_only) {
$returnedValue .= Tools::getCurrentUrlProtocolPrefix() . 'www.' . self::DOMAIN_NAME . '.' ;
}
switch ($id_lang) {
case 2:
default:
$returnedValue.= 'com';
break;
case 3:
$returnedValue.= 'es';
break;
}
return $returnedValue;
}
private function qualifySubDomain()
{
$productionSubDomainArray = self::$subDomains[self::PRODUCTION];
$backOfficeSubDomainArray = self::$subDomains[self::BACKOFFICE];
$developmentSubDomainArray = self::$subDomains[self::DEV];
if (in_array($this->subDomain, $productionSubDomainArray, true)) {
$this->envt = Environment::PRODUCTION;
} elseif (in_array($this->subDomain, $backOfficeSubDomainArray, true)) {
$this->envt = Environment::BACKOFFICE;
} elseif (in_array($this->subDomain, $developmentSubDomainArray, true)) {
$this->envt = Environment::DEV;
} else {
$this->envt = Environment::UNDEFINED;
}
}
private function qualifyExtension()
{
return $this->version = Tools::getIsoVersion($this->topLevelDomain);
}
private function qualifyLanguage()
{
$this->iso_lang = Tools::getVersionIso($this->topLevelDomain);
}
/**
* Payment domain url
* @return string payment domain url
*/
public function getPaymentDomain($http = false)
{
$url = '';
if ($http) {
$url = Tools::getCurrentUrlProtocolPrefix();
}
$sub_domain = self::$paymentSubDomain[$this->envt];
return $url . $sub_domain . '.' . self::DOMAIN_NAME . '.' . self::DEFAULT_VERSION;
}
}