3
3
namespace Algolia \AlgoliaSearch \Console \Command ;
4
4
5
5
use Algolia \AlgoliaSearch \Api \Product \ReplicaManagerInterface ;
6
+ use Algolia \AlgoliaSearch \Exceptions \AlgoliaException ;
7
+ use Algolia \AlgoliaSearch \Exceptions \ExceededRetriesException ;
8
+ use Algolia \AlgoliaSearch \Helper \Entity \ProductHelper ;
9
+ use Magento \Framework \App \Area ;
10
+ use Magento \Framework \App \State ;
6
11
use Magento \Framework \Console \Cli ;
12
+ use Magento \Framework \Exception \LocalizedException ;
13
+ use Magento \Framework \Exception \NoSuchEntityException ;
14
+ use Magento \Store \Model \StoreManagerInterface ;
7
15
use Symfony \Component \Console \Command \Command ;
8
16
use Symfony \Component \Console \Input \InputInterface ;
9
17
use Symfony \Component \Console \Input \InputArgument ;
10
18
use Symfony \Component \Console \Output \OutputInterface ;
11
19
12
20
class ReplicaCommand extends Command
13
21
{
14
- const STORE_ARGUMENT = 'store ' ;
22
+ protected const STORE_ARGUMENT = 'store ' ;
23
+
24
+ protected ?OutputInterface $ output = null ;
25
+
26
+ /** @var string[] */
27
+ protected array $ _storeNames = [];
15
28
16
29
/**
30
+ * @param ProductHelper $productHelper
17
31
* @param ReplicaManagerInterface $replicaManager
32
+ * @param StoreManagerInterface $storeManager
18
33
* @param string|null $name
19
34
*/
20
35
public function __construct (
36
+ protected State $ state ,
37
+ protected ProductHelper $ productHelper ,
21
38
protected ReplicaManagerInterface $ replicaManager ,
22
- ?string $ name = null
39
+ protected StoreManagerInterface $ storeManager ,
40
+ ?string $ name = null
23
41
)
24
42
{
25
43
parent ::__construct ($ name );
@@ -33,25 +51,105 @@ protected function configure(): void
33
51
$ this ->setName ('algolia:replicas:sync ' )
34
52
->setDescription ('Sync configured sorting attributes in Magento to Algolia replica indices ' )
35
53
->setDefinition ([
36
- new InputArgument (self ::STORE_ARGUMENT , InputArgument::OPTIONAL , 'ID for store to be synced with Algolia (optional), if not specified all stores will be synced ' ),
54
+ new InputArgument (
55
+ self ::STORE_ARGUMENT ,
56
+ InputArgument::OPTIONAL | InputArgument::IS_ARRAY ,
57
+ 'ID(s) for store to be synced with Algolia (optional), if not specified all stores will be synced '
58
+ )
37
59
]);
38
60
39
61
parent ::configure ();
40
62
}
41
63
42
- /** @inheritDoc */
64
+ /**
65
+ * @inheritDoc
66
+ * @param InputInterface $input
67
+ * @param OutputInterface $output
68
+ * @return int
69
+ * @throws AlgoliaException
70
+ * @throws ExceededRetriesException
71
+ * @throws LocalizedException
72
+ * @throws NoSuchEntityException
73
+ */
43
74
protected function execute (InputInterface $ input , OutputInterface $ output ): int
44
75
{
45
- $ storeIds = $ input ->getArgument (self ::STORE_ARGUMENT );
76
+ $ storeIds = ( array ) $ input ->getArgument (self ::STORE_ARGUMENT );
46
77
78
+ $ msg = 'Syncing replicas for ' . ($ storeIds ? count ($ storeIds ) : 'all ' ) . ' store ' . (!$ storeIds || count ($ storeIds ) > 1 ? 's ' : '' );
47
79
if ($ storeIds ) {
48
- $ output ->writeln ('<info>Syncing store ' . $ storeIds . '!</info> ' );
80
+ /** @var string[] $storeNames */
81
+ $ storeNames = array_map (
82
+ function ($ storeId ) {
83
+ return $ this ->getStoreName ($ storeId );
84
+ },
85
+ $ storeIds
86
+ );
87
+ $ output ->writeln ("<info> $ msg: " . join (", " , $ storeNames ) . '</info> ' );
49
88
} else {
50
- $ output ->writeln (' <info>Syncing all stores </info> ' );
89
+ $ output ->writeln (" <info> $ msg </info> " );
51
90
}
52
91
53
- // $this->replicaManager->syncReplicasToAlgolia();
92
+ $ this ->output = $ output ;
93
+ $ this ->state ->setAreaCode (Area::AREA_ADMINHTML );
94
+ $ this ->syncReplicas ($ storeIds );
54
95
55
96
return Cli::RETURN_SUCCESS ;
56
97
}
98
+
99
+ /**
100
+ * @throws NoSuchEntityException
101
+ */
102
+ protected function getStoreName (int $ storeId ): string
103
+ {
104
+ if (!isset ($ this ->_storeNames [$ storeId ])) {
105
+ $ this ->_storeNames [$ storeId ] = $ this ->storeManager ->getStore ($ storeId )->getName ();
106
+ }
107
+ return $ this ->_storeNames [$ storeId ];
108
+ }
109
+
110
+ /**
111
+ * @param int[] $storeIds
112
+ * @return void
113
+ * @throws AlgoliaException
114
+ * @throws ExceededRetriesException
115
+ * @throws LocalizedException
116
+ * @throws NoSuchEntityException
117
+ */
118
+ protected function syncReplicas (array $ storeIds = []): void
119
+ {
120
+ if (count ($ storeIds )) {
121
+ foreach ($ storeIds as $ storeId ) {
122
+ $ this ->syncReplicasForStore ($ storeId );
123
+ }
124
+ } else {
125
+ $ this ->syncReplicasForAllStores ();
126
+ }
127
+ }
128
+
129
+ /**
130
+ * @throws NoSuchEntityException
131
+ * @throws ExceededRetriesException
132
+ * @throws AlgoliaException
133
+ * @throws LocalizedException
134
+ */
135
+ protected function syncReplicasForStore (int $ storeId ): void
136
+ {
137
+ $ this ->output ->writeln ('<info>Syncing ' . $ this ->getStoreName ($ storeId ) . '...</info> ' );
138
+ $ this ->replicaManager ->syncReplicasToAlgolia ($ storeId , $ this ->productHelper ->getIndexSettings ($ storeId ));
139
+ }
140
+
141
+ /**
142
+ * @throws NoSuchEntityException
143
+ * @throws ExceededRetriesException
144
+ * @throws AlgoliaException
145
+ * @throws LocalizedException
146
+ */
147
+ protected function syncReplicasForAllStores (): void
148
+ {
149
+ $ storeIds = array_keys ($ this ->storeManager ->getStores ());
150
+ foreach ($ storeIds as $ storeId ) {
151
+ $ this ->syncReplicasForStore ($ storeId );
152
+ }
153
+
154
+ }
57
155
}
0 commit comments