Skip to content

Commit

Permalink
0.1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrii-fediuk committed Jul 2, 2017
1 parent 45c6d68 commit c67e179
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 70 deletions.
107 changes: 55 additions & 52 deletions API/R.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ static function accounts() {return self::p(__FUNCTION__);}
/**
* 2017-07-01 «pricelevel EntityType»: https://msdn.microsoft.com/en-us/library/mt607683.aspx
* «Entity that defines pricing levels.»
* @used-by \Dfe\Dynamics365\Source\PriceList::fetch()
* @return array(string => mixed)
*/
static function pricelevels() {return self::p(__FUNCTION__);}
Expand Down Expand Up @@ -52,57 +53,59 @@ static function service() {return self::p('');}
* @return array(string => mixed)
* @throws DFE
*/
static function p($path, $method = C::GET, array $p = []) {
/** @var C $c */
$c = (new C)
->setConfig(['timeout' => 120])
->setHeaders([
'Accept' => 'application/json'
,'Authorization' => 'Bearer ' . OAuth::token()
,'OData-MaxVersion' => '4.0'
,'OData-Version' => '4.0'
])
->setMethod($method)
->setUri(df_cc_path(S::s()->url(), 'api/data/v8.2', $path))
;
C::GET === $method ? $c->setParameterGet($p) : $c->setParameterPost($p);
/** @var array(string => mixed) $r */
$r = df_json_decode($c->request()->getBody());
/**
* 2017-06-30
* An error response looks like:
* {
* "error": {
* "code": "",
* "message": "Resource not found for the segment 'dummy'.",
* "innererror": {
* "message": "Resource not found for the segment 'dummy'.",
* "type": "Microsoft.OData.Core.UriParser.ODataUnrecognizedPathException",
* "stacktrace": "<...>"
* }
* }
* }
* @var array(string => string|array(string => string))|null $e
*/
if ($e = dfa($r, 'error')) {
// 2017-06-30 It correctly works even if the key does not exist in the array.
unset($e['innererror']['stacktrace']);
/** @var string $message */
$message = dfa($e, 'message');
/** @var string $req */
$req = df_zf_http_last_req($c);
/** @var string $res */
$res = df_json_encode_pretty($e);
/** @var DFE $ex */
$ex = df_error_create(
"The «{$path}» Dynamics 365 Web API request has failed: «{$message}»."
."\nThe full error description:\n$res"
."\nThe full request:\n$req"
);
df_log_l(__CLASS__, $ex);
df_sentry(__CLASS__, $message, ['extra' => ['Request' => $req, 'Response' => $res]]);
throw $ex;
}
return array_map('df_ksort', $r['value']);
static function p($path, $method = C::GET, array $p = []) {return df_cache_get_simple(
null, function($path, $method = C::GET, array $p = []) {
/** @var C $c */
$c = (new C)
->setConfig(['timeout' => 120])
->setHeaders([
'Accept' => 'application/json'
,'Authorization' => 'Bearer ' . OAuth::token()
,'OData-MaxVersion' => '4.0'
,'OData-Version' => '4.0'
])
->setMethod($method)
->setUri(df_cc_path(S::s()->url(), 'api/data/v8.2', $path))
;
C::GET === $method ? $c->setParameterGet($p) : $c->setParameterPost($p);
/** @var array(string => mixed) $r */
$r = df_json_decode($c->request()->getBody());
/**
* 2017-06-30
* An error response looks like:
* {
* "error": {
* "code": "",
* "message": "Resource not found for the segment 'dummy'.",
* "innererror": {
* "message": "Resource not found for the segment 'dummy'.",
* "type": "Microsoft.OData.Core.UriParser.ODataUnrecognizedPathException",
* "stacktrace": "<...>"
* }
* }
* }
* @var array(string => string|array(string => string))|null $e
*/
if ($e = dfa($r, 'error')) {
// 2017-06-30 It correctly works even if the key does not exist in the array.
unset($e['innererror']['stacktrace']);
/** @var string $message */
$message = dfa($e, 'message');
/** @var string $req */
$req = df_zf_http_last_req($c);
/** @var string $res */
$res = df_json_encode_pretty($e);
/** @var DFE $ex */
$ex = df_error_create(
"The «{$path}» Dynamics 365 Web API request has failed: «{$message}»."
."\nThe full error description:\n$res"
."\nThe full request:\n$req"
);
df_log_l(__CLASS__, $ex);
df_sentry(__CLASS__, $message, ['extra' => ['Request' => $req, 'Response' => $res]]);
throw $ex;
}
return array_map('df_ksort', $r['value']);
}, ...func_get_args());
}
}
13 changes: 2 additions & 11 deletions Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Button extends AE implements ElementI, IComment {
* @param string $v
* @return string
*/
function getCommentText($v) {return $this->authenticated()
function getCommentText($v) {return S::s()->authenticatedB()
? "<b>Your Magento instance is <span class='df-ok'>successfully authenticated</span> to your Dynamics 365 instance.</b>"
: "<b>You <span class='df-warning'>need to authenticate</span> your Magento instance to your Dynamics 365 instance.</b>"
;}
Expand All @@ -34,7 +34,7 @@ function getCommentText($v) {return $this->authenticated()
* @return string
*/
function getElementHtml() {return df_block(W::class, [
'id' => $this->getHtmlId(), 'label' => __($this->authenticated() ? 'Re-authenticate' : 'Authenticate')
'id' => $this->getHtmlId(), 'label' => __(S::s()->authenticatedB() ? 'Re-authenticate' : 'Authenticate')
])->toHtml();}

/**
Expand Down Expand Up @@ -158,14 +158,5 @@ function onFormInitialized() {
] + OAuth::tokenP()));
df_fe_init($this, __CLASS__, [], ['url' => $url]);
}

/**
* 2017-06-29
* @used-by getCommentText()
* @return bool
*/
private function authenticated() {return dfc($this, function() {return
!!S::s()->refreshToken(df_scope())
;});}
}

8 changes: 8 additions & 0 deletions Settings/General/OAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
// 2017-04-23
/** @method static OAuth s() */
final class OAuth extends \Df\Config\Settings {
/**
* 2017-06-29
* @used-by \Dfe\Dynamics365\Button::getCommentText()
* @used-by \Dfe\Dynamics365\Button::getElementHtml()
* @return bool
*/
function authenticatedB() {return dfc($this, function() {return !!$this->refreshToken(df_scope());});}

/**
* 2017-04-23
* @used-by \Dfe\Dynamics365\Button::onFormInitialized()
Expand Down
10 changes: 6 additions & 4 deletions Source/PriceList.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php
namespace Dfe\Dynamics365\Source;
use Dfe\Dynamics365\API\R as R;
use Dfe\Dynamics365\Settings\General\OAuth as S;
// 2017-07-02
final class PriceList extends \Df\Config\Source\API {
/**
Expand All @@ -9,7 +11,7 @@ final class PriceList extends \Df\Config\Source\API {
* @used-by \Df\Config\Source\API::map()
* @return array(string => string)
*/
protected function fetch() {return ['Test' => 'Test'];}
protected function fetch() {return array_column(R::pricelevels(), 'name', 'pricelevelid');}

/**
* 2017-07-02
Expand All @@ -18,14 +20,14 @@ protected function fetch() {return ['Test' => 'Test'];}
* @used-by \Df\Config\Source\API::map()
* @return bool
*/
protected function isRequirementMet() {return true;}
protected function isRequirementMet() {return S::s()->authenticatedB();}

/**
* 2017-07-02
* @override
* @see \Df\Config\Source\API::requirementTitle()
* @see \Df\Config\Source\API::requirement()
* @used-by \Df\Config\Source\API::map()
* @return string
*/
protected function requirementTitle() {return '';}
protected function requirement() {return 'Setup the «General» → «OAuth 2.0» section first.';}
}
4 changes: 2 additions & 2 deletions T/Basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ function accounts() {echo df_dump(df_json_encode_pretty(R::accounts()));}
/** 2017-06-30 */
function invalid() {echo df_dump(df_json_encode_pretty(R::p('dummy')));}

/** 2017-07-01 */
/** @test 2017-07-01 */
function pricelevels() {echo df_dump(df_json_encode_pretty(R::pricelevels()));}

/** @test 2017-07-01 */
/** 2017-07-01 */
function productpricelevels() {$this->p();}

/** 2017-07-01 */
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mage2pro/dynamics365"
,"version": "0.1.4"
,"version": "0.1.5"
,"description": "The «Microsoft Dynamics 365» extension for Magento 2."
,"type": "magento2-module"
,"homepage": "https://mage2.pro/c/extensions/dynamics365"
Expand Down
1 change: 1 addition & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ https://mage2.pro/t/4076 -->
sortOrder='2'
translate='label'
>
<label>Products</label>
<field
id='priceList'
translate='label'
Expand Down

0 comments on commit c67e179

Please sign in to comment.