|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Grayloon\MagentoStorage\Console; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use Grayloon\Magento\Magento; |
| 7 | +use Grayloon\MagentoStorage\Models\MagentoCustomerGroup; |
| 8 | +use Grayloon\MagentoStorage\Support\HasMagentoCustomerGroups; |
| 9 | +use Illuminate\Console\Command; |
| 10 | + |
| 11 | +class SyncMagentoCustomerGroups extends Command |
| 12 | +{ |
| 13 | + use HasMagentoCustomerGroups; |
| 14 | + |
| 15 | + protected $signature = 'magento:sync-customer-groups'; |
| 16 | + protected $description = 'Syncs the Magento Customer Groups from the Magento API.'; |
| 17 | + |
| 18 | + protected $bar; |
| 19 | + protected $count; |
| 20 | + protected $pages; |
| 21 | + protected $pageSize = 100; |
| 22 | + protected $magento; |
| 23 | + |
| 24 | + public function handle(Magento $magento) |
| 25 | + { |
| 26 | + $this->magento = $magento; |
| 27 | + |
| 28 | + $this->info('Retrieving Magento Customer Groups from Magento...'); |
| 29 | + $this->resolveTotalGroups(); |
| 30 | + $this->info("{$this->count} Groups Found."); |
| 31 | + |
| 32 | + if (! $this->count) { |
| 33 | + return $this->error('No Customer Groups found. Sync Customer Group cancelled.'); |
| 34 | + } |
| 35 | + |
| 36 | + $this->newLine(); |
| 37 | + $this->bar->start(); |
| 38 | + |
| 39 | + for ($currentPage = 1; $this->pages > $currentPage; $currentPage++) { |
| 40 | + $groups = $this->magento->api('customerGroups') |
| 41 | + ->search($this->pageSize, $currentPage); |
| 42 | + |
| 43 | + if (! $groups->ok()) { |
| 44 | + throw new Exception('Unable to locate Customer Groups.'); |
| 45 | + } |
| 46 | + |
| 47 | + $this->updateOrCreateCustomerGroups($groups->json()); |
| 48 | + } |
| 49 | + |
| 50 | + $this->bar->finish(); |
| 51 | + $this->newLine(); |
| 52 | + $this->newLine(); |
| 53 | + $this->info(MagentoCustomerGroup::count() . ' groups synced from the Magento API to your application.'); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * The total count of Customer Groups. |
| 58 | + * |
| 59 | + * @throws \Exception |
| 60 | + * @return $this |
| 61 | + */ |
| 62 | + protected function resolveTotalGroups() |
| 63 | + { |
| 64 | + if (config('magento.store_code')) { |
| 65 | + $this->newLine(); |
| 66 | + $this->info('Default Store Code: "'. config('magento.store_code') .'" is configured in your env.'); |
| 67 | + $this->info('We will only sync those customer groups associated with "'. config('magento.store_code') .'".'); |
| 68 | + } |
| 69 | + |
| 70 | + $groups = $this->magento->api('customerGroups')->search(); |
| 71 | + |
| 72 | + if (! $groups->ok() || ! isset($groups->json()['total_count'])) { |
| 73 | + throw new Exception('Unable to locate Customer Groups.'); |
| 74 | + } |
| 75 | + |
| 76 | + $this->count = $groups->json()['total_count']; |
| 77 | + $this->bar = $this->output |
| 78 | + ->createProgressBar($this->count); |
| 79 | + $this->pages = ceil(($this->count / $this->pageSize) + 1); |
| 80 | + |
| 81 | + return $this; |
| 82 | + } |
| 83 | +} |
0 commit comments