Browse code

Add mapping helper and configuration

Benjamin Roth authored on07/11/2024 13:35:01
Showing5 changed files
... ...
@@ -10,3 +10,6 @@ services:
10 10
 
11 11
     vonRotenberg\ShopwareApiBundle\API\Shopware:
12 12
         public: true
13
+
14
+    vonRotenberg\ShopwareApiBundle\Helper\ShopwareMappings:
15
+        public: true
... ...
@@ -217,24 +217,9 @@ class Shopware
217 217
         }
218 218
 
219 219
         // Prepare product data
220
-        $arrData = [
221
-            'taxId' => '018e65c0485071508949c072f8dc18bd',
222
-            'id' => $blnInsert ? $this->createUuid() : $Product->data[0]->id,
223
-            'price' => [
224
-                [
225
-                    'currencyId' => 'b7d2554b0ce847cd82f3ac9bd1c0dfca',
226
-                    'gross' => 9.9,
227
-                    'net' => 9.9/119*100,
228
-                    'linked' => true
229
-                ]
230
-            ],
231
-            'productNumber' => $strSku,
232
-            'stock' => 9999999,
233
-            'name' => 'Superheldenumhang',
234
-            'customFields' => [
235
-                'custom_wine_attributes_jahrgang' => "2020"
236
-            ]
237
-        ];
220
+        $arrData = array_merge([
221
+            'id' => $blnInsert ? $this->createUuid() : $Product->data[0]->id
222
+        ], $arrData);
238 223
 
239 224
         $options = [
240 225
             'headers' => [
... ...
@@ -40,6 +40,34 @@ class Configuration implements ConfigurationInterface
40 40
                         ->end()
41 41
                     ->end()
42 42
                 ->end()
43
+                ->arrayNode('mappings')
44
+                    ->addDefaultsIfNotSet()
45
+                    ->children()
46
+                        ->arrayNode('properties')
47
+                            ->info('Shopware property mappings (SWUUID: ext. value)')
48
+                            ->defaultValue([])
49
+                            ->arrayPrototype()
50
+                                ->scalarPrototype()
51
+                                    ->cannotBeEmpty()
52
+                                ->end()
53
+                            ->end()
54
+                        ->end()
55
+                        ->arrayNode('property_groups')
56
+                            ->info('Shopware property groups (SWUUID: ext. value)')
57
+                            ->defaultValue([])
58
+                            ->scalarPrototype()
59
+                                ->cannotBeEmpty()
60
+                            ->end()
61
+                        ->end()
62
+                        ->arrayNode('units')
63
+                            ->info('Shopware units (SWUUID: ext. value)')
64
+                            ->defaultValue([])
65
+                            ->scalarPrototype()
66
+                               ->cannotBeEmpty()
67
+                           ->end()
68
+                        ->end()
69
+                    ->end()
70
+                ->end()
43 71
             ->end()
44 72
         ;
45 73
 
... ...
@@ -27,6 +27,10 @@ class VonrotenbergShopwareApiExtension extends Extension
27 27
         {
28 28
             $container->setParameter('vonrotenberg_shopware_api.credentials.'.$key,$val);
29 29
         }
30
+        foreach($config['mappings'] as $key=>$val)
31
+        {
32
+            $container->setParameter('vonrotenberg_shopware_api.mappings.'.$key,$val);
33
+        }
30 34
 
31 35
         $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../../config'));
32 36
         $loader->load('services.yml');
33 37
new file mode 100644
... ...
@@ -0,0 +1,82 @@
1
+<?php
2
+
3
+declare(strict_types=1);
4
+
5
+/*
6
+ * This file is part of vonRotenberg Shopware API Bundle.
7
+ *
8
+ * (c) vonRotenberg
9
+ *
10
+ * @license proprietary
11
+ */
12
+
13
+namespace vonRotenberg\ShopwareApiBundle\Helper;
14
+
15
+use Contao\System;
16
+
17
+class ShopwareMappings
18
+{
19
+    protected $mappings = [];
20
+
21
+    protected function getMappings()
22
+    {
23
+        if (!isset($this->mappings['properties']))
24
+        {
25
+            $this->mappings['properties'] = System::getContainer()->getParameter('vonrotenberg_shopware_api.mappings.properties');
26
+        }
27
+        if (!isset($this->mappings['property_groups']))
28
+        {
29
+            $this->mappings['property_groups'] = System::getContainer()->getParameter('vonrotenberg_shopware_api.mappings.property_groups');
30
+        }
31
+        if (!isset($this->mappings['units']))
32
+        {
33
+            $this->mappings['units'] = System::getContainer()->getParameter('vonrotenberg_shopware_api.mappings.units');
34
+        }
35
+
36
+        return $this->mappings;
37
+    }
38
+
39
+    public function getPropertyIdByNameAndGroup(string $name, string $group, bool $useGroupUuid = false): ?string
40
+    {
41
+        $mappings = $this->getMappings();
42
+        $propertyId = null;
43
+
44
+        if (!$useGroupUuid)
45
+        {
46
+            $group = array_search($group, $mappings['property_groups']);
47
+        }
48
+
49
+        if ($group !== false && ($property = array_search($name, $mappings['properties'][$group])) !== false)
50
+        {
51
+            $propertyId = $property;
52
+        }
53
+
54
+        return $propertyId;
55
+    }
56
+
57
+    public function getPropertyGroupIdByName(string $name): ?string
58
+    {
59
+        $mappings = $this->getMappings();
60
+        $groupId = null;
61
+
62
+        if (($group = array_search($name, $mappings['property_groups'])) !== false)
63
+        {
64
+            $groupId = $group;
65
+        }
66
+
67
+        return $groupId;
68
+    }
69
+
70
+    public function getUnitIdByName(string $name): ?string
71
+    {
72
+        $mappings = $this->getMappings();
73
+        $unitId = null;
74
+
75
+        if (($unit = array_search($name, $mappings['units'])) !== false)
76
+        {
77
+            $unitId = $unit;
78
+        }
79
+
80
+        return $unitId;
81
+    }
82
+}