Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 29 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| ConnectNuvemshopCommand | |
0.00% |
0 / 29 |
|
0.00% |
0 / 2 |
72 | |
0.00% |
0 / 1 |
| handle | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
12 | |||
| resolvePayload | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
30 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Console\Commands; |
| 4 | |
| 5 | use App\Models\Store; |
| 6 | use App\Services\Nuvemshop\NuvemshopAuthService; |
| 7 | use Illuminate\Console\Command; |
| 8 | use Throwable; |
| 9 | |
| 10 | class ConnectNuvemshopCommand extends Command |
| 11 | { |
| 12 | protected $signature = 'orvox:nuvemshop:connect |
| 13 | {--code= : Código authorization_code recebido da Nuvemshop} |
| 14 | {--access-token= : Access token já gerado pela Nuvemshop} |
| 15 | {--user-id= : User ID / Store ID da loja Nuvemshop} |
| 16 | {--scope= : Scopes separados por vírgula} |
| 17 | {--store=orvox-oficial : Slug da loja local}'; |
| 18 | |
| 19 | protected $description = 'Conecta uma loja Nuvemshop usando authorization_code ou access_token já gerado.'; |
| 20 | |
| 21 | public function handle(NuvemshopAuthService $authService): int |
| 22 | { |
| 23 | $storeSlug = (string) $this->option('store'); |
| 24 | |
| 25 | $store = Store::query() |
| 26 | ->where('slug', $storeSlug) |
| 27 | ->first(); |
| 28 | |
| 29 | if (! $store) { |
| 30 | $this->error('Loja não encontrada: ' . $storeSlug); |
| 31 | |
| 32 | return self::FAILURE; |
| 33 | } |
| 34 | |
| 35 | try { |
| 36 | $payload = $this->resolvePayload($authService); |
| 37 | |
| 38 | $integration = $authService->connectStore($store, $payload); |
| 39 | } catch (Throwable $exception) { |
| 40 | $this->error($exception->getMessage()); |
| 41 | |
| 42 | return self::FAILURE; |
| 43 | } |
| 44 | |
| 45 | $this->info('Integração Nuvemshop conectada com sucesso.'); |
| 46 | $this->line('Store integration ID: ' . $integration->id); |
| 47 | $this->line('External store/user ID: ' . $integration->external_store_id); |
| 48 | |
| 49 | return self::SUCCESS; |
| 50 | } |
| 51 | |
| 52 | private function resolvePayload(NuvemshopAuthService $authService): array |
| 53 | { |
| 54 | $accessToken = $this->option('access-token'); |
| 55 | $userId = $this->option('user-id'); |
| 56 | |
| 57 | if (filled($accessToken) && filled($userId)) { |
| 58 | return [ |
| 59 | 'access_token' => (string) $accessToken, |
| 60 | 'token_type' => 'bearer', |
| 61 | 'scope' => (string) $this->option('scope'), |
| 62 | 'user_id' => (string) $userId, |
| 63 | ]; |
| 64 | } |
| 65 | |
| 66 | $code = (string) ($this->option('code') ?: $this->secret('Cole o code da Nuvemshop')); |
| 67 | |
| 68 | if (blank($code)) { |
| 69 | throw new \RuntimeException('Informe --code ou --access-token com --user-id.'); |
| 70 | } |
| 71 | |
| 72 | return $authService->exchangeAuthorizationCode($code); |
| 73 | } |
| 74 | } |