ModuleConfig.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Model\Config;
  6. use Magento\Framework\DataObjectFactory;
  7. use Magento\Framework\Module\ModuleList;
  8. use Temando\Shipping\Webservice\Config\WsConfigInterface;
  9. /**
  10. * Temando Config Values Handler
  11. *
  12. * @package Temando\Shipping\Model
  13. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  14. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  15. * @author Max Melzer <max.melzer@netresearch.de>
  16. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  17. * @link https://www.temando.com/
  18. */
  19. class ModuleConfig implements ModuleConfigInterface, WsConfigInterface
  20. {
  21. const CONFIG_XML_PATH_CHECKOUT_ENABLED = 'carriers/temando/active';
  22. const CONFIG_XML_PATH_LOGGING_ENABLED = 'carriers/temando/logging_enabled';
  23. const CONFIG_XML_PATH_SESSION_ENDPOINT = 'carriers/temando/session_endpoint';
  24. const CONFIG_XML_PATH_API_ENDPOINT = 'carriers/temando/sovereign_endpoint';
  25. const CONFIG_XML_PATH_API_VERSION = 'carriers/temando/api_version';
  26. const CONFIG_XML_PATH_ACCOUNT_ID = 'carriers/temando/account_id';
  27. const CONFIG_XML_PATH_BEARER_TOKEN = 'carriers/temando/bearer_token';
  28. const CONFIG_XML_PATH_BEARER_TOKEN_EXPIRY = 'carriers/temando/bearer_token_expiry';
  29. const CONFIG_XML_PATH_SYNC_ENABLED = 'carriers/temando/sync_enabled';
  30. const CONFIG_XML_PATH_SYNC_SHIPMENTS_ENABLED = 'carriers/temando/sync_shipments_enabled';
  31. const CONFIG_XML_PATH_SYNC_ORDERS_ENABLED = 'carriers/temando/sync_orders_enabled';
  32. const CONFIG_XML_PATH_SYNC_STREAM_ID = 'carriers/temando/sync_stream_id';
  33. const CONFIG_XML_PATH_CHECKOUT_FIELDS = 'carriers/temando/additional_checkout_fields';
  34. const CONFIG_XML_PATH_REGISTER_ACCOUNT_URL = 'carriers/temando/register_account_url';
  35. const CONFIG_XML_PATH_SHIPPING_PORTAL_URL = 'carriers/temando/shipping_portal_url';
  36. const CONFIG_XML_PATH_TEMANDO_RETURNS_ACTIVE = 'carriers/temando/rma_enabled';
  37. const CONFIG_XML_PATH_COLLECTION_POINTS_ENABLED = 'carriers/temando/collectionpoints_enabled';
  38. const CONFIG_XML_PATH_COLLECTION_POINTS_COUNTRIES = 'carriers/temando/collectionpoints_countries';
  39. const CONFIG_XML_PATH_CLICK_AND_COLLECT_ENABLED = 'carriers/temando/clickandcollect_enabled';
  40. const CONFIG_XML_PATH_CLICK_AND_COLLECT_COUNTRIES = 'carriers/temando/clickandcollect_countries';
  41. /**
  42. * @var DataObjectFactory
  43. */
  44. private $dataObjectFactory;
  45. /**
  46. * @var ConfigAccessor
  47. */
  48. private $configAccessor;
  49. /**
  50. * @var ModuleList
  51. */
  52. private $moduleList;
  53. /**
  54. * ModuleConfig constructor.
  55. *
  56. * @param DataObjectFactory $dataObjectFactory
  57. * @param ConfigAccessor $configAccessor
  58. * @param ModuleList $moduleList
  59. */
  60. public function __construct(
  61. DataObjectFactory $dataObjectFactory,
  62. ConfigAccessor $configAccessor,
  63. ModuleList $moduleList
  64. ) {
  65. $this->dataObjectFactory = $dataObjectFactory;
  66. $this->configAccessor = $configAccessor;
  67. $this->moduleList = $moduleList;
  68. }
  69. /**
  70. * Check if shipping module is enabled in checkout.
  71. *
  72. * @param int $storeId
  73. *
  74. * @return bool
  75. */
  76. public function isEnabled($storeId = null)
  77. {
  78. return (bool) $this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_CHECKOUT_ENABLED, $storeId);
  79. }
  80. /**
  81. * Check if webservice communication logging is enabled.
  82. *
  83. * @return bool
  84. */
  85. public function isLoggingEnabled()
  86. {
  87. return (bool) $this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_LOGGING_ENABLED);
  88. }
  89. /**
  90. * @param int $storeId
  91. * @return \Magento\Framework\DataObject
  92. */
  93. public function getStoreInformation($storeId = null)
  94. {
  95. $storeInformation = $this->configAccessor->getConfigValue(
  96. 'general/store_information',
  97. $storeId
  98. );
  99. $storeInfo = $this->dataObjectFactory->create(['data' => (array)$storeInformation]);
  100. return $storeInfo;
  101. }
  102. /**
  103. * @param int $storeId
  104. * @return \Magento\Framework\DataObject
  105. */
  106. public function getShippingOrigin($storeId = null)
  107. {
  108. $shippingOrigin = $this->configAccessor->getConfigValue(
  109. 'shipping/origin',
  110. $storeId
  111. );
  112. $storeInfo = $this->dataObjectFactory->create(['data' => (array)$shippingOrigin]);
  113. return $storeInfo;
  114. }
  115. /**
  116. * @param int $storeId
  117. * @return string
  118. */
  119. public function getWeightUnit($storeId = null)
  120. {
  121. return $this->configAccessor->getConfigValue('general/locale/weight_unit', $storeId);
  122. }
  123. /**
  124. * Obtain Register Account Url.
  125. *
  126. * @return string
  127. */
  128. public function getRegisterAccountUrl()
  129. {
  130. return (string) $this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_REGISTER_ACCOUNT_URL);
  131. }
  132. /**
  133. * Obtain Shipping Portal Url.
  134. *
  135. * @return string
  136. */
  137. public function getShippingPortalUrl()
  138. {
  139. return (string) $this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_SHIPPING_PORTAL_URL);
  140. }
  141. /**
  142. * Check if merchant registered an account at Temando.
  143. *
  144. * @return bool
  145. */
  146. public function isRegistered()
  147. {
  148. return ($this->getAccountId() !== '') && ($this->getBearerToken() !== '');
  149. }
  150. /**
  151. * Read URL of Temando REST API.
  152. *
  153. * @return string
  154. */
  155. public function getSessionEndpoint()
  156. {
  157. return $this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_SESSION_ENDPOINT);
  158. }
  159. /**
  160. * Read URL of Temando REST API.
  161. *
  162. * @return string
  163. */
  164. public function getApiEndpoint()
  165. {
  166. return $this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_API_ENDPOINT);
  167. }
  168. /**
  169. * Save URL of Temando REST API.
  170. *
  171. * @param string $apiEndpoint
  172. * @return void
  173. */
  174. public function saveApiEndpoint($apiEndpoint)
  175. {
  176. $this->configAccessor->saveConfigValue(self::CONFIG_XML_PATH_API_ENDPOINT, $apiEndpoint);
  177. }
  178. /**
  179. * Obtain the API version to connect to.
  180. *
  181. * @return string
  182. */
  183. public function getApiVersion()
  184. {
  185. return (string) $this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_API_VERSION);
  186. }
  187. /**
  188. * Read Temando Account Id.
  189. *
  190. * @return string
  191. */
  192. public function getAccountId()
  193. {
  194. return (string) $this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_ACCOUNT_ID);
  195. }
  196. /**
  197. * Save Temando Account Id.
  198. *
  199. * @param string $accountId
  200. * @return void
  201. */
  202. public function saveAccountId($accountId)
  203. {
  204. $this->configAccessor->saveConfigValue(self::CONFIG_XML_PATH_ACCOUNT_ID, $accountId);
  205. }
  206. /**
  207. * Read Temando Authentication Token.
  208. *
  209. * @return string
  210. */
  211. public function getBearerToken()
  212. {
  213. return (string)$this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_BEARER_TOKEN);
  214. }
  215. /**
  216. * Save Temando Authentication Token.
  217. *
  218. * @param string $bearerToken
  219. * @return void
  220. */
  221. public function saveBearerToken($bearerToken)
  222. {
  223. $this->configAccessor->saveConfigValue(self::CONFIG_XML_PATH_BEARER_TOKEN, $bearerToken);
  224. }
  225. /**
  226. * Read Temando Authentication Token Expiry Timestamp.
  227. *
  228. * @return string
  229. */
  230. public function getBearerTokenExpiry()
  231. {
  232. return (string) $this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_BEARER_TOKEN_EXPIRY);
  233. }
  234. /**
  235. * Save Temando Authentication Token Expiry Timestamp.
  236. *
  237. * @param string $bearerTokenExpiry
  238. * @return void
  239. */
  240. public function saveBearerTokenExpiry($bearerTokenExpiry)
  241. {
  242. $this->configAccessor->saveConfigValue(self::CONFIG_XML_PATH_BEARER_TOKEN_EXPIRY, $bearerTokenExpiry);
  243. }
  244. /**
  245. * Check whether stream events should be processed or not.
  246. *
  247. * @return bool
  248. */
  249. public function isSyncEnabled()
  250. {
  251. return (bool) $this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_SYNC_ENABLED);
  252. }
  253. /**
  254. * Save new stream event processing configuration.
  255. *
  256. * @param string $value
  257. * @return void
  258. */
  259. public function saveSyncEnabled($value)
  260. {
  261. $this->configAccessor->saveConfigValue(self::CONFIG_XML_PATH_SYNC_ENABLED, $value);
  262. }
  263. /**
  264. * Check whether shipment events should be processed or not.
  265. *
  266. * @return bool
  267. */
  268. public function isSyncShipmentEnabled()
  269. {
  270. return (bool) $this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_SYNC_SHIPMENTS_ENABLED);
  271. }
  272. /**
  273. * Save new stream event processing configuration.
  274. *
  275. * @param string $value
  276. * @return void
  277. */
  278. public function saveSyncShipmentEnabled($value)
  279. {
  280. $this->configAccessor->saveConfigValue(self::CONFIG_XML_PATH_SYNC_SHIPMENTS_ENABLED, $value);
  281. }
  282. /**
  283. * Check whether order events should be processed or not.
  284. *
  285. * @return bool
  286. */
  287. public function isSyncOrderEnabled()
  288. {
  289. return (bool) $this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_SYNC_ORDERS_ENABLED);
  290. }
  291. /**
  292. * Save new stream event processing configuration.
  293. *
  294. * @param string $value
  295. * @return void
  296. */
  297. public function saveSyncOrderEnabled($value)
  298. {
  299. $this->configAccessor->saveConfigValue(self::CONFIG_XML_PATH_SYNC_ORDERS_ENABLED, $value);
  300. }
  301. /**
  302. * Multiple streams may exists. Obtain the stream ID to request events from.
  303. *
  304. * @return string
  305. */
  306. public function getStreamId()
  307. {
  308. return (string) $this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_SYNC_STREAM_ID);
  309. }
  310. /**
  311. * Get checkout field definitions. This is the plain serialized string as stored in config.
  312. *
  313. * @return string
  314. */
  315. public function getCheckoutFieldsDefinition()
  316. {
  317. $checkoutFields = (string) $this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_CHECKOUT_FIELDS);
  318. if (!$checkoutFields) {
  319. $checkoutFields = '[]';
  320. }
  321. return $checkoutFields;
  322. }
  323. /**
  324. * Check if RMA feature is enabled.
  325. *
  326. * @return bool
  327. */
  328. public function isRmaEnabled()
  329. {
  330. return (bool) $this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_TEMANDO_RETURNS_ACTIVE);
  331. }
  332. /**
  333. * Check if RMA module is installed
  334. *
  335. * @return bool
  336. */
  337. public function isRmaAvailable()
  338. {
  339. return (bool) $this->moduleList->has('Magento_Rma');
  340. }
  341. /**
  342. * Save checkout field definitions in config.
  343. *
  344. * @param string $fieldsDefinition
  345. * @return void
  346. */
  347. public function saveCheckoutFieldsDefinition($fieldsDefinition)
  348. {
  349. $this->configAccessor->saveConfigValue(self::CONFIG_XML_PATH_CHECKOUT_FIELDS, $fieldsDefinition);
  350. }
  351. /**
  352. * Check if collection points feature is enabled in config.
  353. *
  354. * @param int $storeId
  355. *
  356. * @return bool
  357. */
  358. public function isCollectionPointsEnabled($storeId = null)
  359. {
  360. return (bool) $this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_COLLECTION_POINTS_ENABLED, $storeId);
  361. }
  362. /**
  363. * Obtain country codes enabled for collection point deliveries.
  364. *
  365. * @param int $storeId
  366. *
  367. * @return string[]
  368. */
  369. public function getCollectionPointDeliveryCountries($storeId = null)
  370. {
  371. return $this->configAccessor->getConfigValue(
  372. self::CONFIG_XML_PATH_COLLECTION_POINTS_COUNTRIES,
  373. $storeId
  374. );
  375. }
  376. /**
  377. * Check if click and collect feature is enabled in config.
  378. *
  379. * @param int $storeId
  380. *
  381. * @return bool
  382. */
  383. public function isClickAndCollectEnabled($storeId = null)
  384. {
  385. return (bool) $this->configAccessor->getConfigValue(self::CONFIG_XML_PATH_CLICK_AND_COLLECT_ENABLED, $storeId);
  386. }
  387. /**
  388. * Save new account data.
  389. *
  390. * @param string $accountId
  391. * @param string $bearerToken
  392. * @param string $bearerTokenExpiry
  393. * @return void
  394. */
  395. public function setAccount($accountId, $bearerToken, $bearerTokenExpiry)
  396. {
  397. $this->saveAccountId($accountId);
  398. $this->saveBearerToken($bearerToken);
  399. $this->saveBearerTokenExpiry($bearerTokenExpiry);
  400. }
  401. /**
  402. * Unset all account data.
  403. *
  404. * @return void
  405. */
  406. public function unsetAccount()
  407. {
  408. $this->configAccessor->deleteConfigValue(self::CONFIG_XML_PATH_ACCOUNT_ID);
  409. $this->configAccessor->deleteConfigValue(self::CONFIG_XML_PATH_BEARER_TOKEN);
  410. $this->configAccessor->deleteConfigValue(self::CONFIG_XML_PATH_BEARER_TOKEN_EXPIRY);
  411. }
  412. }