Browse code

Initial commit

Benjamin Roth authored on18/03/2025 13:06:28
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,111 @@
1
+<?php
2
+
3
+declare(strict_types=1);
4
+
5
+/*
6
+ * This file is part of vonRotenberg WMFGO Cevisio Bundle.
7
+ *
8
+ * (c) vonRotenberg
9
+ *
10
+ * @license proprietary
11
+ */
12
+
13
+namespace vonRotenberg\AldegottSwNlBundle\Command;
14
+
15
+use Contao\CoreBundle\Framework\ContaoFramework;
16
+use Contao\System;
17
+use Contao\Validator;
18
+use Symfony\Component\Console\Command\Command;
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 vonRotenberg\AldegottSwNlBundle\Cron\ShopwareExportNewsletterJob;
24
+use vonRotenberg\AldegottSwNlBundle\Cron\ShopwareExportOrdersJob;
25
+use vonRotenberg\AldegottSwNlBundle\Cron\ShopwareImportProductsJob;
26
+
27
+class ShopwareExportNewsletterCommand extends Command
28
+{
29
+    protected static $defaultName = 'shopware:export-newsletter-recipients';
30
+
31
+    /**
32
+     * @var ContaoFramework
33
+     */
34
+    private $framework;
35
+
36
+    private $start;
37
+    private $stop;
38
+    private $email;
39
+
40
+    public function __construct(ContaoFramework $framework)
41
+    {
42
+        $this->framework = $framework;
43
+
44
+        parent::__construct();
45
+    }
46
+
47
+    protected function configure(): void
48
+    {
49
+        $this
50
+            ->setName(self::$defaultName)
51
+            ->addOption('start', 'b', InputOption::VALUE_REQUIRED, 'Custom start date')
52
+            ->addOption('stop', 'x', InputOption::VALUE_REQUIRED, 'Custom stop date')
53
+            ->addOption('email', 't', InputOption::VALUE_OPTIONAL, 'Email address to send exported recipients file to')
54
+            ->setDescription('Export newsletter recipients from Shopware instance')
55
+        ;
56
+    }
57
+
58
+    protected function interact(InputInterface $input, OutputInterface $output): void
59
+    {
60
+        if ((null !== $input->getOption('start') && null === $input->getOption('stop'))
61
+            || (null === $input->getOption('start') && null !== $input->getOption('stop')))
62
+        {
63
+            throw new \RuntimeException('You need to provide start and stop date.');
64
+        }
65
+
66
+        if (null !== $input->getOption('start')) {
67
+            try {
68
+                new \DateTime($input->getOption('start'));
69
+                $this->start = $input->getOption('start');
70
+            } catch(\Exception $e)
71
+            {
72
+                throw new \InvalidArgumentException('Start date is invalid. Please provide an ISO date.');
73
+            }
74
+        }
75
+
76
+        if (null !== $input->getOption('stop')) {
77
+            try {
78
+                new \DateTime($input->getOption('stop'));
79
+                $this->stop = $input->getOption('stop');
80
+            } catch(\Exception $e)
81
+            {
82
+                throw new \InvalidArgumentException('Stop date is invalid. Please provide an ISO date.');
83
+            }
84
+        }
85
+
86
+        if (null !== $input->getOption('email')) {
87
+            if (Validator::isEmail($input->getOption('email')))
88
+            {
89
+                $this->email = $input->getOption('email');
90
+            } else
91
+            {
92
+                throw new \InvalidArgumentException('Stop date is invalid. Please provide an ISO date.');
93
+            }
94
+        }
95
+    }
96
+
97
+    protected function execute(InputInterface $input, OutputInterface $output): int
98
+    {
99
+        $this->framework->initialize();
100
+
101
+        $io = new SymfonyStyle($input, $output);
102
+        $io->title('Newsletter recipients export from Shopware');
103
+
104
+        /** @var ShopwareExportNewsletterJob $export */
105
+        $export = System::getContainer()->get(ShopwareExportNewsletterJob::class);
106
+
107
+        $export->export('cli', $this->start,$this->stop, $this->email,$io);
108
+
109
+        return 0;
110
+    }
111
+}