99use Magento \Framework \App \Cache \Frontend \Pool ;
1010use Magento \Framework \App \Cache \TypeListInterface ;
1111use Magento \Framework \App \Config \ScopeConfigInterface ;
12- use Magento \Framework \App \Config \Storage \WriterInterface ;
1312use Magento \Framework \Controller \Result \JsonFactory ;
13+ use MyParcelNL \Magento \Service \AccountSettings \Importer ;
14+ use MyParcelNL \Magento \Service \AccountSettings \Maintenance as AccountSettingsMaintenance ;
1415use MyParcelNL \Magento \Service \Config ;
15- use MyParcelNL \Sdk \Model \Account \CarrierOptions ;
16- use MyParcelNL \Sdk \Services \Web \AccountWebService ;
17- use MyParcelNL \Sdk \Services \Web \CarrierOptionsWebService ;
18- use MyParcelNL \Sdk \Support \Collection ;
1916
2017class CarrierConfigurationImport extends Action
2118{
@@ -25,38 +22,41 @@ class CarrierConfigurationImport extends Action
2522 /**
2623 * @var mixed
2724 */
28- private $ typeListInterface ;
29- private WriterInterface $ configWriter ;
25+ private $ typeListInterface ;
26+ private Importer $ importer ;
27+ private AccountSettingsMaintenance $ accountSettingsMaintenance ;
3028
3129 /**
32- * @param \Magento\Framework\Controller\Result\JsonFactory $resultFactory
33- * @param \Magento\Backend\App\Action\Context $context
34- * @param \Magento\Framework\Model\ResourceModel\Db\Context $dbContext
35- * @param \Magento\Framework\App\Cache\TypeListInterface $typeListInterface
36- * @param \Magento\Framework\App\Config\ScopeConfigInterface $config
37- * @param \Magento\Framework\App\Cache\Frontend\Pool $pool
30+ * @param Context $context
31+ * @param ScopeConfigInterface $config
32+ * @param JsonFactory $resultFactory
33+ * @param TypeListInterface $typeListInterface
34+ * @param Pool $pool
35+ * @param Importer $importer
36+ * @param AccountSettingsMaintenance $accountSettingsMaintenance
3837 */
3938 public function __construct (
40- Context $ context ,
41- WriterInterface $ configWriter ,
42- ScopeConfigInterface $ config ,
43- JsonFactory $ resultFactory ,
44- TypeListInterface $ typeListInterface ,
45- Pool $ pool
39+ Context $ context ,
40+ ScopeConfigInterface $ config ,
41+ JsonFactory $ resultFactory ,
42+ TypeListInterface $ typeListInterface ,
43+ Pool $ pool ,
44+ Importer $ importer ,
45+ AccountSettingsMaintenance $ accountSettingsMaintenance
4646 )
4747 {
4848 parent ::__construct ($ context );
4949 $ params = $ this ->_request ->getParams ();
5050 $ scope = $ params ['scope ' ] ?? ScopeConfigInterface::SCOPE_TYPE_DEFAULT ;
5151 $ scopeId = $ params ['scopeId ' ] ?? 0 ;
5252
53- // Let’s save the carrier configuration settings per api key, so it can be retrieved per api key as well.
54- $ this ->apiKey = $ config ->getValue (Config::XML_PATH_GENERAL . 'api/key ' , $ scope , $ scopeId );
53+ $ this ->apiKey = $ config ->getValue (Config::XML_PATH_API_KEY , $ scope , $ scopeId );
5554
56- $ this ->configWriter = $ configWriter ;
57- $ this ->resultFactory = $ resultFactory ;
58- $ this ->typeListInterface = $ typeListInterface ;
59- $ this ->pool = $ pool ;
55+ $ this ->resultFactory = $ resultFactory ;
56+ $ this ->typeListInterface = $ typeListInterface ;
57+ $ this ->pool = $ pool ;
58+ $ this ->importer = $ importer ;
59+ $ this ->accountSettingsMaintenance = $ accountSettingsMaintenance ;
6060 }
6161
6262 /**
@@ -66,15 +66,11 @@ public function __construct(
6666 */
6767 public function execute ()
6868 {
69- $ configuration = $ this ->fetchConfigurations ();
70- $ this ->configWriter ->save (
71- Config::XML_PATH_GENERAL . "account_settings_ $ this ->apiKey " ,
72- json_encode ($ this ->createArray ($ configuration ))
73- );
69+ $ this ->importer ->importFor ($ this ->apiKey );
7470
75- // Clear configuration cache right after saving the account settings, so the modal in the carrier specific
76- // configuration view will be showing the updated drop-off point.
7771 $ this ->clearCache ();
72+ // After the flush, because it reads config.
73+ $ this ->accountSettingsMaintenance ->reconcile ();
7874
7975 return $ this ->resultFactory ->create ()
8076 ->setData (
@@ -86,33 +82,6 @@ public function execute()
8682 ;
8783 }
8884
89- /**
90- * @return \MyParcelNL\Sdk\Support\Collection
91- * @throws \MyParcelNL\Sdk\Exception\AccountNotActiveException
92- * @throws \MyParcelNL\Sdk\Exception\ApiException
93- * @throws \MyParcelNL\Sdk\Exception\MissingFieldException
94- */
95- public function fetchConfigurations (): Collection
96- {
97- $ accountService = (new AccountWebService ())->setApiKey ($ this ->apiKey );
98-
99- $ account = $ accountService ->getAccount ();
100- $ shop = $ account ->getShops ()
101- ->first ()
102- ;
103- $ shopId = $ shop ->getId ();
104- $ optionConfigurationService = (new CarrierOptionsWebService ())->setApiKey ($ this ->apiKey );
105- $ optionConfiguration = $ optionConfigurationService ->getCarrierOptions ($ shopId );
106-
107- return new Collection (
108- [
109- 'shop ' => $ shop ,
110- 'account ' => $ account ,
111- 'carrier_options ' => $ optionConfiguration ,
112- ]
113- );
114- }
115-
11685 private function clearCache (): void
11786 {
11887 $ cacheFrontendPool = $ this ->pool ;
@@ -124,42 +93,4 @@ private function clearCache(): void
12493 ;
12594 }
12695 }
127-
128- /**
129- * @param \MyParcelNL\Sdk\Support\Collection $settings
130- *
131- * @return array
132- * @TODO sdk#326 remove this entire function and replace with toArray
133- */
134- private function createArray (Collection $ settings ): array
135- {
136- /** @var \MyParcelNL\Sdk\Model\Account\Shop $shop */
137- $ shop = $ settings ->get ('shop ' );
138- /** @var \MyParcelNL\Sdk\Model\Account\Account $account */
139- $ account = $ settings ->get ('account ' );
140- /** @var \MyParcelNL\Sdk\Model\Account\CarrierOptions[]|Collection $carrierOptions */
141- $ carrierOptions = $ settings ->get ('carrier_options ' );
142-
143- return [
144- 'shop ' => [
145- 'id ' => $ shop ->getId (),
146- 'name ' => $ shop ->getName (),
147- ],
148- 'account ' => $ account ->toArray (),
149- 'carrier_options ' => array_map (static function (CarrierOptions $ carrierOptions ) {
150- $ carrier = $ carrierOptions ->getCarrier ();
151- return [
152- 'carrier ' => [
153- 'human ' => $ carrier ->getHuman (),
154- 'id ' => $ carrier ->getId (),
155- 'name ' => $ carrier ->getName (),
156- ],
157- 'enabled ' => $ carrierOptions ->isEnabled (),
158- 'label ' => $ carrierOptions ->getLabel (),
159- 'optional ' => $ carrierOptions ->isOptional (),
160- 'type ' => $ carrierOptions ->getType (),
161- ];
162- }, $ carrierOptions ->all ()),
163- ];
164- }
16596}
0 commit comments