Browse code

Optimize API functions and CLI commands

Benjamin Roth authored on21/10/2024 15:41:50
Showing1 changed files
... ...
@@ -48,6 +48,8 @@ class ShopwareGetProductsBySku extends Command
48 48
     protected function execute(InputInterface $input, OutputInterface $output): int
49 49
     {
50 50
         $this->framework->initialize();
51
+        $cloner = new VarCloner();
52
+        $dumper = new CliDumper();
51 53
 
52 54
         $io = new SymfonyStyle($input, $output);
53 55
         $io->title('Shopware products search by SKU');
... ...
@@ -56,21 +58,22 @@ class ShopwareGetProductsBySku extends Command
56 58
         /** @var Shopware $Shopware */
57 59
         $Shopware = System::getContainer()->get(Shopware::class);
58 60
 
59
-        if (($Products = $Shopware->getProductsForSku($strSku,true, $input->getOption('short') ? true : false)) !== false)
61
+        $Result = $Shopware->getProductsForSku($strSku,true, $input->getOption('short') ? true : false);
62
+
63
+        if ($Result->getStatusCode() === 200)
60 64
         {
61
-            $cloner = new VarCloner();
62
-            $dumper = new CliDumper();
65
+            $Content = json_validate($Result->getContent()) ? json_decode($Result->getContent(), true) : $Result;
63 66
 
64
-            if ($Products->total)
67
+            if ($Content['total'])
65 68
             {
66
-                $dumper->dump(($cloner->cloneVar($Products->data)));
69
+                $dumper->dump(($cloner->cloneVar($Content['data'])));
67 70
             }
68
-            $io->success(sprintf('Found %s product(s)',$Products->total));
71
+            $io->success(sprintf('Found %s product(s)',$Content['total']));
69 72
 
70 73
             return self::SUCCESS;
71 74
         }
75
+        $io->error(sprintf('Could not retrieve products by SKU. HTTP Status returned: %s',$Result->getStatusCode()));
72 76
 
73
-        $io->error('Could not retrieve products by SKU');
74 77
         return self::FAILURE;
75 78
     }
76 79
 }
Browse code

Optimize get products by sku console command

Benjamin Roth authored on21/10/2024 12:21:03
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,76 @@
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\Command;
14
+
15
+use Contao\CoreBundle\Framework\ContaoFramework;
16
+use Contao\System;
17
+use Symfony\Component\Console\Command\Command;
18
+use Symfony\Component\Console\Input\InputArgument;
19
+use Symfony\Component\Console\Input\InputInterface;
20
+use Symfony\Component\Console\Input\InputOption;
21
+use Symfony\Component\Console\Output\OutputInterface;
22
+use Symfony\Component\Console\Style\SymfonyStyle;
23
+use Symfony\Component\VarDumper\Cloner\VarCloner;
24
+use Symfony\Component\VarDumper\Dumper\CliDumper;
25
+use vonRotenberg\ShopwareApiBundle\API\Shopware;
26
+
27
+class ShopwareGetProductsBySku extends Command
28
+{
29
+    protected static $defaultName = 'shopware:get-products-by-sku';
30
+
31
+    protected $framework;
32
+
33
+    public function __construct(ContaoFramework $framework)
34
+    {
35
+        $this->framework = $framework;
36
+
37
+        parent::__construct();
38
+    }
39
+
40
+    protected function configure(): void
41
+    {
42
+        $this
43
+            ->setName(self::$defaultName)
44
+            ->setDescription('Search Shopware products by SKU')
45
+            ->addOption('short','s',InputOption::VALUE_NONE, 'Short result: Only show name and basic product infos')
46
+        ;
47
+    }
48
+    protected function execute(InputInterface $input, OutputInterface $output): int
49
+    {
50
+        $this->framework->initialize();
51
+
52
+        $io = new SymfonyStyle($input, $output);
53
+        $io->title('Shopware products search by SKU');
54
+        $strSku = $io->ask('Enter SKU to search for');
55
+
56
+        /** @var Shopware $Shopware */
57
+        $Shopware = System::getContainer()->get(Shopware::class);
58
+
59
+        if (($Products = $Shopware->getProductsForSku($strSku,true, $input->getOption('short') ? true : false)) !== false)
60
+        {
61
+            $cloner = new VarCloner();
62
+            $dumper = new CliDumper();
63
+
64
+            if ($Products->total)
65
+            {
66
+                $dumper->dump(($cloner->cloneVar($Products->data)));
67
+            }
68
+            $io->success(sprintf('Found %s product(s)',$Products->total));
69
+
70
+            return self::SUCCESS;
71
+        }
72
+
73
+        $io->error('Could not retrieve products by SKU');
74
+        return self::FAILURE;
75
+    }
76
+}