Skip to content

Commit

Permalink
Remove unnecessary config for declaring Constants
Browse files Browse the repository at this point in the history
  • Loading branch information
hlu2 committed Apr 7, 2017
1 parent 3e2a58e commit f6ab162
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 181 deletions.
222 changes: 137 additions & 85 deletions src/Core/Configuration/LocalConfigReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


/**
* Specifies the Default Configuration Reader implmentation used by the SDK.
* Specifies the Default Configuration Reader implmentation used by the SDK. The ConfigReader can either read a file or from passed arrays
*
* @hao - separate each function to its own method
*
Expand Down Expand Up @@ -76,7 +76,7 @@ class LocalConfigReader
* A Customer FilePath. If different than the default sdk.config file
* @return IppConfiguration The custom config object.
*/
public static function ReadConfiguration($filePath = null, $OAuthOption = CoreConstants::OAUTH1)
public static function ReadConfigurationFromFile($filePath, $OAuthOption = CoreConstants::OAUTH1)
{
$ippConfig = new IppConfiguration();

Expand All @@ -86,7 +86,8 @@ public static function ReadConfiguration($filePath = null, $OAuthOption = CoreCo
$xmlObj = simplexml_load_file($filePath);
}else
{
$xmlObj = simplexml_load_file(PATH_SDK_ROOT . 'sdk.config');
// $xmlObj = simplexml_load_file(PATH_SDK_ROOT . 'sdk.config');
throw new \Exception("Can't Read Configuration from file: ". $filePath);
}

LocalConfigReader::initializeOAuthSettings($xmlObj, $ippConfig, $OAuthOption);
Expand All @@ -102,6 +103,38 @@ public static function ReadConfiguration($filePath = null, $OAuthOption = CoreCo
}


}

public static function ReadConfigurationFromParameters($OAuthConfig, $baseUrl, $defaultLoggingLocation = CoreConstatnts::DEFAULT_LOGGINGLOCATION, $minorVersion = 3){
$ippConfig = new IppConfiguration();
try {
//Set OAuth
if(isset($OAuthConfig)){
$ippConfig->Security = $OAuthConfig;
}else{
throw new \Exception("Empty OAuth Config from Constuct IPP Configuration on LocalConfigReader");
}
//Set Logger and Searlization format. The default one is XML
LocalConfigReader::intializeMessage($ippConfig);
LocalConfigReader::setRequestAndResponseSerializationFormat($ippConfig, CompressionFormat::None, CompressionFormat::None, SerializationFormat::Xml, SerializationFormat::Xml);
//Set base Urls
$ippConfig->BaseUrl = new BaseUrl();
$ippConfig->BaseUrl->Qbo = $baseUrl;
//Set content writer and logger
LocalConfigReader::setupLogger($ippConfig, $defaultLoggingLocation, "TRUE");
LocalConfigReader::setupContentWriter($ippConfig, CoreConstants::FILE_STRATEGY, CoreConstants::PHP_CLASS_PREFIX, null, false);
//Set API Entity Rules
$rules=CoreConstants::getQuickBooksOnlineAPIEntityRules();
LocalConfigReader::initOperationControlList($ippConfig, $rules);
//Set minor version
$ippConfig->minorVersion = $minorVersion;

return $ippConfig;

} catch (\Exception $e) {
throw new \Exception("Can't Config Environments from passed parameters");
}

}

/**
Expand Down Expand Up @@ -220,10 +253,7 @@ public static function initializeOAuthSettings($xmlObj, $ippConfig, $OAuthOption
* @Hao
*/
public static function initializeRequestAndResponseSerializationAndCompressionFormat($xmlObj, $ippConfig){
// Initialize Request Configuration Object
$ippConfig->Message = new Message();
$ippConfig->Message->Request = new Request();
$ippConfig->Message->Response = new Response();
LocalConfigReader::intializeMessage($ippConfig);

$requestSerializationFormat = NULL;
$requestCompressionFormat = NULL;
Expand All @@ -246,76 +276,87 @@ public static function initializeRequestAndResponseSerializationAndCompressionFo
$responseSerializationFormat = (string)$responseAttr->serializationFormat;
$responseCompressionFormat = (string)$responseAttr->compressionFormat;
}
LocalConfigReader::setRequestAndResponseSerializationFormat($ippConfig, $requestCompressionFormat, $responseCompressionFormat, $requestSerializationFormat, $responseSerializationFormat);

}

public static function intializeMessage($ippConfig){
// Initialize Request Configuration Object
$ippConfig->Message = new Message();
$ippConfig->Message->Request = new Request();
$ippConfig->Message->Response = new Response();
}

public static function setRequestAndResponseSerializationFormat($ippConfig, $requestCompressionFormat, $responseCompressionFormat, $requestSerializationFormat, $responseSerializationFormat){
switch ($requestCompressionFormat)
{
case CompressionFormat::None:
$ippConfig->Message->Request->CompressionFormat = CompressionFormat::None;
break;
case CompressionFormat::GZip:
$ippConfig->Message->Request->CompressionFormat = CompressionFormat::GZip;
break;
case CompressionFormat::Deflate:
$ippConfig->Message->Request->CompressionFormat = CompressionFormat::Deflate;
break;
default:
case CompressionFormat::None:
$ippConfig->Message->Request->CompressionFormat = CompressionFormat::None;
break;
case CompressionFormat::GZip:
$ippConfig->Message->Request->CompressionFormat = CompressionFormat::GZip;
break;
case CompressionFormat::Deflate:
$ippConfig->Message->Request->CompressionFormat = CompressionFormat::Deflate;
break;
default:
//Default compression set to None
$ippConfig->Message->Request->CompressionFormat = CompressionFormat::None;
break;
break;
}

switch ($responseCompressionFormat)
{
case CompressionFormat::None:
$ippConfig->Message->Response->CompressionFormat = CompressionFormat::None;
break;
case CompressionFormat::GZip:
$ippConfig->Message->Response->CompressionFormat = CompressionFormat::GZip;
break;
case CompressionFormat::Deflate:
$ippConfig->Message->Response->CompressionFormat = CompressionFormat::Deflate;
break;
case CompressionFormat::None:
$ippConfig->Message->Response->CompressionFormat = CompressionFormat::None;
break;
case CompressionFormat::GZip:
$ippConfig->Message->Response->CompressionFormat = CompressionFormat::GZip;
break;
case CompressionFormat::Deflate:
$ippConfig->Message->Response->CompressionFormat = CompressionFormat::Deflate;
break;
default:
//Default compression set to None
$ippConfig->Message->Response->CompressionFormat = CompressionFormat::None;
break;
}

switch ($requestSerializationFormat)
{
//case Intuit\Ipp\Utility\SerializationFormat::DEFAULT:
case SerializationFormat::Xml:
$ippConfig->Message->Request->SerializationFormat = SerializationFormat::Xml;
break;
case SerializationFormat::Json:
$ippConfig->Message->Request->SerializationFormat = SerializationFormat::Json;
break;
case SerializationFormat::Custom:
$ippConfig->Message->Request->SerializationFormat = SerializationFormat::Custom;
break;
//Default compression set to None
$ippConfig->Message->Response->CompressionFormat = CompressionFormat::None;
break;
}

switch ($requestSerializationFormat)
{
//case Intuit\Ipp\Utility\SerializationFormat::DEFAULT:
case SerializationFormat::Xml:
$ippConfig->Message->Request->SerializationFormat = SerializationFormat::Xml;
break;
case SerializationFormat::Json:
$ippConfig->Message->Request->SerializationFormat = SerializationFormat::Json;
break;
case SerializationFormat::Custom:
$ippConfig->Message->Request->SerializationFormat = SerializationFormat::Custom;
break;
default:
//Default compression set to XML
$ippConfig->Message->Request->SerializationFormat = SerializationFormat::Xml;
break;
}

switch ($responseSerializationFormat)
{
case SerializationFormat::Xml:
$ippConfig->Message->Response->SerializationFormat = SerializationFormat::Xml;
break;
//case Intuit\Ipp\Utility\SerializationFormat::DEFAULT:
case SerializationFormat::Json:
$ippConfig->Message->Response->SerializationFormat = SerializationFormat::Json;
break;
case SerializationFormat::Custom:
$ippConfig->Message->Response->SerializationFormat = SerializationFormat::Custom;
break;
//Default compression set to XML
$ippConfig->Message->Request->SerializationFormat = SerializationFormat::Xml;
break;
}

switch ($responseSerializationFormat)
{
case SerializationFormat::Xml:
$ippConfig->Message->Response->SerializationFormat = SerializationFormat::Xml;
break;
//case Intuit\Ipp\Utility\SerializationFormat::DEFAULT:
case SerializationFormat::Json:
$ippConfig->Message->Response->SerializationFormat = SerializationFormat::Json;
break;
case SerializationFormat::Custom:
$ippConfig->Message->Response->SerializationFormat = SerializationFormat::Custom;
break;
default:
//Default compression set to XML
$ippConfig->Message->Response->SerializationFormat = SerializationFormat::Xml;
break;
}
$ippConfig->Message->Response->SerializationFormat = SerializationFormat::Xml;
break;
}
}

/**
Expand All @@ -336,38 +377,49 @@ public static function intializaeServiceBaseURLAndLogger($xmlObj, $ippConfig){
}

// Initialize Logger
$ippConfig->Logger = new Logger();
if ( isset($xmlObj) &&
isset($xmlObj->intuit->ipp->logger->requestLog))
{
$requestLogAttr = $xmlObj->intuit->ipp->logger->requestLog->attributes();
$ippConfig->Logger->RequestLog->ServiceRequestLoggingLocation = (string)$requestLogAttr->requestResponseLoggingDirectory;
$ippConfig->Logger->RequestLog->EnableRequestResponseLogging = (string)$requestLogAttr->enableRequestResponseLogging;
}else {
throw new \Exception("Log settings is not available from Config file.");
}
if ( isset($xmlObj) &&
isset($xmlObj->intuit->ipp->logger->requestLog))
{
$requestLogAttr = $xmlObj->intuit->ipp->logger->requestLog->attributes();
$ServiceRequestLoggingLocation = (string)$requestLogAttr->requestResponseLoggingDirectory;
$EnableRequestResponseLogging = (string)$requestLogAttr->enableRequestResponseLogging;
LocalConfigReader::setupLogger($ippConfig, $ServiceRequestLoggingLocation, $EnableRequestResponseLogging);
}else {
throw new \Exception("Log settings is not available from Config file.");
}

// A developer is forced to write in the same style.
// This should be refactored
$ippConfig->ContentWriter = new ContentWriterSettings();
if ( isset($xmlObj) &&
isset($xmlObj->intuit->ipp->contentWriter))
{
$contentWriterAttr = $xmlObj->intuit->ipp->contentWriter->attributes();
$ippConfig->ContentWriter->strategy = ContentWriterSettings::checkStrategy((string)$contentWriterAttr->strategy);
$ippConfig->ContentWriter->prefix = (string)$contentWriterAttr->prefix;
$ippConfig->ContentWriter->exportDir = $contentWriterAttr->exportDirectory
? (string)$contentWriterAttr->exportDirectory
: null;
$ippConfig->ContentWriter->returnOject = $contentWriterAttr->returnObject
? filter_var((string)$contentWriterAttr->returnObject, FILTER_VALIDATE_BOOLEAN)
: false;
$ippConfig->ContentWriter->verifyConfiguration();
$strategy = ContentWriterSettings::checkStrategy((string)$contentWriterAttr->strategy);
$prefix = (string)$contentWriterAttr->prefix;
$exportDir = $contentWriterAttr->exportDirectory;
$returnOject = $contentWriterAttr->returnObject;
LocalConfigReader::setupContentWriter($ippConfig, $strategy, $prefix, $exportDir, $returnOject);
}else{
throw new \Exception("Content Writer Settings is not available from Config file.");
}
}

public static function setupLogger($ippConfig, $ServiceRequestLoggingLocation, $EnableRequestResponseLogging)
{
$ippConfig->Logger = new Logger();
$ippConfig->Logger->RequestLog->ServiceRequestLoggingLocation = $ServiceRequestLoggingLocation;
$ippConfig->Logger->RequestLog->EnableRequestResponseLogging = $EnableRequestResponseLogging;
}

public static function setupContentWriter($ippConfig, $strategy, $prefix, $exportDir, $returnOject ){
$ippConfig->ContentWriter = new ContentWriterSettings();
$ippConfig->ContentWriter->strategy = $strategy;
$ippConfig->ContentWriter->prefix = $prefix;
$ippConfig->ContentWriter->exportDir = $exportDir ? (string)$exportDir : null;
$ippConfig->ContentWriter->returnOject = $returnOject ? filter_var($returnOject, FILTER_VALIDATE_BOOLEAN): false;
$ippConfig->ContentWriter->verifyConfiguration();
}


/**
* Creates PHP class entity from intuit name
Expand All @@ -376,7 +428,7 @@ public static function intializaeServiceBaseURLAndLogger($xmlObj, $ippConfig){
*/
private static function decorateEntity($name)
{
return PHP_CLASS_PREFIX . $name;
return CoreConstants::PHP_CLASS_PREFIX . $name;
}

}
3 changes: 3 additions & 0 deletions src/Core/CoreConstants.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
class CoreConstants
{

const DEFAULT_LOGGINGLOCATION = "/tmp/IdsLogs";

const PHP_CLASS_PREFIX = 'IPP';
/*
* All content writer strategy we support. Append this lists with new strategies
*/
Expand Down
6 changes: 4 additions & 2 deletions src/Core/Http/Serialization/IEntitySerializer.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

namespace QuickBooksOnline\API\Core\Http\Serialization;
use QuickBooksOnline\API\Core\CoreConstants;
/**
* Entity serialize contract.
*/
Expand Down Expand Up @@ -35,10 +36,11 @@ public function getResourceURL(){}
public static function cleanPhpClassNameToIntuitEntityName($phpClassName)
{
//Add namespace to it
$phpClassName = trim($phpClassName);
$listArray = explode('\\', $phpClassName);
$trimedPhpClassName = end($listArray);
if (0==strpos($trimedPhpClassName, PHP_CLASS_PREFIX))
return substr($trimedPhpClassName, strlen(PHP_CLASS_PREFIX));
if (0==strpos($trimedPhpClassName, CoreConstants::PHP_CLASS_PREFIX))
return substr($trimedPhpClassName, strlen(CoreConstants::PHP_CLASS_PREFIX));

throw new \Exception("Cannot convert php Class name:{" . $phpClassName . "} to Intuit Entity name");
}
Expand Down
3 changes: 2 additions & 1 deletion src/Core/Http/Serialization/JsonObjectSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use QuickBooksOnline\API\Utility\MetadataExtractor;
use QuickBooksOnline\API\Diagnostics\TraceLogger;
use QuickBooksOnline\API\Exception\IdsExceptionManager;
use QuickBooksOnline\API\Core\CoreConstants;

/**
* Json Serializer to serialize and de serialize.
Expand Down Expand Up @@ -99,7 +100,7 @@ private function collectResourceURL($entity)
*/
private static function decorateIntuitEntityToPhpClassName($intuitEntityName)
{
return PHP_CLASS_PREFIX . $intuitEntityName;
return CoreConstatnts::PHP_CLASS_PREFIX . $intuitEntityName;
}

/**
Expand Down
11 changes: 6 additions & 5 deletions src/Core/Http/Serialization/XmlObjectSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use QuickBooksOnline\API\XSD2PHP\src\com\mikebevz\xsd2php\Php2Xml;
use QuickBooksOnline\API\XSD2PHP\src\com\mikebevz\xsd2php\Bind;


/**
* Xml Serialize(r) to serialize and de serialize.
*/
Expand Down Expand Up @@ -39,7 +40,7 @@ private static function getXmlFromObj($phpObj)
return FALSE;
}

$php2xml = new Php2Xml(PHP_CLASS_PREFIX);
$php2xml = new Php2Xml(CoreConstants::PHP_CLASS_PREFIX);
$php2xml->overrideAsSingleNamespace='http://schema.intuit.com/finance/v3';

try {
Expand Down Expand Up @@ -92,7 +93,7 @@ private static function PhpObjFromXml($className, $xmlStr)
}else{
throw new \Exception("Can't find corresponding CLASS for className" . $className . "during unmarshall XML into POPO Object");
}
$bind = new Bind(PHP_CLASS_PREFIX);
$bind = new Bind(CoreConstants::PHP_CLASS_PREFIX);
$bind->overrideAsSingleNamespace='http://schema.intuit.com/finance/v3';
$bind->bindXml($xmlStr, $phpObj);
return $phpObj;
Expand Down Expand Up @@ -150,7 +151,7 @@ private static function ParseArbitraryResultObjects($responseXml, $bLimitToOne)
private static function decorateIntuitEntityToPhpClassName($intuitEntityName)
{
$intuitEntityName = trim($intuitEntityName);
return PHP_CLASS_PREFIX . $intuitEntityName;
return CoreConstants::PHP_CLASS_PREFIX . $intuitEntityName;
}

/**
Expand All @@ -165,8 +166,8 @@ public static function cleanPhpClassNameToIntuitEntityName($phpClassName)
//if the className has delimiters, get the last part
$separetes = explode('\\', $phpClassName);
$phpClassName = end($separetes);
if (0==strpos($phpClassName, PHP_CLASS_PREFIX))
return substr($phpClassName, strlen(PHP_CLASS_PREFIX));
if (0==strpos($phpClassName, CoreConstants::PHP_CLASS_PREFIX))
return substr($phpClassName, strlen(CoreConstants::PHP_CLASS_PREFIX));

return NULL;
}
Expand Down
Loading

0 comments on commit f6ab162

Please sign in to comment.