Skip to content

Commit

Permalink
Add RabbitMQ consumer
Browse files Browse the repository at this point in the history
  • Loading branch information
zerossB committed Jul 31, 2024
1 parent 22fd056 commit 98b4e04
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Console/Commands/Messages/Resources/Helpers/BrandsHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace BildVitta\SpHub\Console\Commands\Messages\Resources\Helpers;

use BildVitta\Hub\Entities\HubBrand;

trait BrandsHelper
{
protected function brandCreateOrUpdate(\stdClass $message)
{
if (! $brand = HubBrand::withTrashed()->where('uuid', $message->uuid)->first()) {
$brand = new HubBrand;
$brand->uuid = $message->uuid;
}

$brand->name = $message->name;
$brand->created_at = $message->created_at;
$brand->updated_at = $message->updated_at;
$brand->deleted_at = $message->deleted_at;

$brand->save();
}

protected function brandDelete(\stdClass $message)
{
HubBrand::where('uuid', $message->uuid)->delete();
}
}
10 changes: 10 additions & 0 deletions src/Console/Commands/Messages/Resources/Helpers/CompanyHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace BildVitta\SpHub\Console\Commands\Messages\Resources\Helpers;

use BildVitta\Hub\Entities\HubBrand;
use BildVitta\SpHub\Models\HubCompany;
use stdClass;

Expand All @@ -27,6 +28,15 @@ private function companyCreateOrUpdate(stdClass $message): void
$company->main_company_id = $message->main_company_id ?? null;
}

if (property_exists($message, 'brand_uuid')) {
$company->brand_id = null;
if ($message->brand_uuid) {
$company->brand_id = HubBrand::withTrashed()->where('uuid', $message->brand_uuid)->value('id');
}
} else {
$company->brand_id = $message->brand_id ?? null;
}

$userModel = app(config('hub.model_user'));
if ($this->userHasExtraFields($userModel->getFillable())) {
$company->document = $message->document;
Expand Down
20 changes: 20 additions & 0 deletions src/Console/Commands/Messages/Resources/MessageProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace BildVitta\SpHub\Console\Commands\Messages\Resources;

use BildVitta\SpHub\Console\Commands\Messages\Resources\Helpers\BrandsHelper;
use BildVitta\SpHub\Console\Commands\Messages\Resources\Helpers\CompanyHelper;
use BildVitta\SpHub\Console\Commands\Messages\Resources\Helpers\CompanyLinksHelper;
use BildVitta\SpHub\Console\Commands\Messages\Resources\Helpers\LogHelper;
Expand All @@ -15,6 +16,7 @@

class MessageProcessor
{
use BrandsHelper;
use CompanyHelper;
use CompanyLinksHelper;
use LogHelper;
Expand Down Expand Up @@ -53,6 +55,8 @@ class MessageProcessor
*/
public const USER_COMPANIES = 'user_companies';

public const BRANDS = 'brands';

/**
* @var string
*/
Expand Down Expand Up @@ -87,6 +91,9 @@ public function process(AMQPMessage $message): void
$operation = $properties[1];

switch ($type) {
case self::BRANDS:
$this->brands($messageData, $operation);
break;
case self::USERS:
$this->users($messageData, $operation);
break;
Expand Down Expand Up @@ -114,6 +121,19 @@ public function process(AMQPMessage $message): void
}
}

private function brands(stdClass $message, string $operation): void
{
switch ($operation) {
case self::CREATED:
case self::UPDATED:
$this->brandCreateOrUpdate($message);
break;
case self::DELETED:
$this->brandDelete($message);
break;
}
}

private function users(stdClass $message, string $operation): void
{
switch ($operation) {
Expand Down

0 comments on commit 98b4e04

Please sign in to comment.