Skip to content

Commit

Permalink
Extract logic from Reactant Listeners to Reactant Jobs (#146)
Browse files Browse the repository at this point in the history
Extract logic from Reactant Listeners to Reactant Jobs
  • Loading branch information
antonkomarev committed Feb 8, 2020
1 parent 9c0c058 commit be15cae
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to `laravel-love` will be documented in this file.

## [Unreleased]

- ([#146]) Extracted logic from `Cog\Laravel\Love\Reactant\Listeners\IncrementAggregates` listener to `Cog\Laravel\Love\Reactant\Jobs\IncrementAggregatesJob`
- ([#146]) Extracted logic from `Cog\Laravel\Love\Reactant\Listeners\DecrementAggregates` listener to `Cog\Laravel\Love\Reactant\Jobs\DecrementAggregatesJob`

## [8.2.0] - 2020-01-30

### Added
Expand Down Expand Up @@ -445,6 +448,7 @@ Follow [upgrade instructions](UPGRADING.md#from-v5-to-v6) to migrate database to
[1.1.1]: https://github.com/cybercog/laravel-love/compare/1.1.0...1.1.1
[1.1.0]: https://github.com/cybercog/laravel-love/compare/1.0.0...1.1.0

[#146]: https://github.com/cybercog/laravel-love/pull/146
[#127]: https://github.com/cybercog/laravel-love/pull/127
[#121]: https://github.com/cybercog/laravel-love/pull/121
[#118]: https://github.com/cybercog/laravel-love/pull/118
Expand Down
60 changes: 60 additions & 0 deletions src/Reactant/Jobs/DecrementAggregatesJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of Laravel Love.
*
* (c) Anton Komarev <anton@komarev.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Cog\Laravel\Love\Reactant\Jobs;

use Cog\Contracts\Love\Reactant\Models\Reactant as ReactantContract;
use Cog\Contracts\Love\Reaction\Models\Reaction as ReactionContract;
use Cog\Laravel\Love\Reactant\ReactionCounter\Services\ReactionCounterService;
use Cog\Laravel\Love\Reactant\ReactionTotal\Services\ReactionTotalService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

final class DecrementAggregatesJob implements
ShouldQueue
{
use Dispatchable;
use Queueable;

/**
* @var \Cog\Contracts\Love\Reactant\Models\Reactant
*/
private $reactant;

/**
* @var \Cog\Contracts\Love\Reaction\Models\Reaction
*/
private $reaction;

/**
* @param \Cog\Contracts\Love\Reactant\Models\Reactant $reactant
* @param \Cog\Contracts\Love\Reaction\Models\Reaction $reaction
*/
public function __construct(
ReactantContract $reactant,
ReactionContract $reaction
) {
$this->reactant = $reactant;
$this->reaction = $reaction;
}

public function handle(): void
{
(new ReactionCounterService($this->reactant))
->removeReaction($this->reaction);

(new ReactionTotalService($this->reactant))
->removeReaction($this->reaction);
}
}
60 changes: 60 additions & 0 deletions src/Reactant/Jobs/IncrementAggregatesJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of Laravel Love.
*
* (c) Anton Komarev <anton@komarev.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Cog\Laravel\Love\Reactant\Jobs;

use Cog\Contracts\Love\Reactant\Models\Reactant as ReactantContract;
use Cog\Contracts\Love\Reaction\Models\Reaction as ReactionContract;
use Cog\Laravel\Love\Reactant\ReactionCounter\Services\ReactionCounterService;
use Cog\Laravel\Love\Reactant\ReactionTotal\Services\ReactionTotalService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

final class IncrementAggregatesJob implements
ShouldQueue
{
use Dispatchable;
use Queueable;

/**
* @var \Cog\Contracts\Love\Reactant\Models\Reactant
*/
private $reactant;

/**
* @var \Cog\Contracts\Love\Reaction\Models\Reaction
*/
private $reaction;

/**
* @param \Cog\Contracts\Love\Reactant\Models\Reactant $reactant
* @param \Cog\Contracts\Love\Reaction\Models\Reaction $reaction
*/
public function __construct(
ReactantContract $reactant,
ReactionContract $reaction
) {
$this->reactant = $reactant;
$this->reaction = $reaction;
}

public function handle(): void
{
(new ReactionCounterService($this->reactant))
->addReaction($this->reaction);

(new ReactionTotalService($this->reactant))
->addReaction($this->reaction);
}
}
22 changes: 15 additions & 7 deletions src/Reactant/Listeners/DecrementAggregates.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,23 @@

namespace Cog\Laravel\Love\Reactant\Listeners;

use Cog\Laravel\Love\Reactant\ReactionCounter\Services\ReactionCounterService;
use Cog\Laravel\Love\Reactant\ReactionTotal\Services\ReactionTotalService;
use Cog\Laravel\Love\Reactant\Jobs\DecrementAggregatesJob;
use Cog\Laravel\Love\Reaction\Events\ReactionHasBeenRemoved;
use Illuminate\Contracts\Bus\Dispatcher;
use Illuminate\Contracts\Queue\ShouldQueue;

final class DecrementAggregates implements ShouldQueue
{
/**
* @var \Illuminate\Contracts\Bus\Dispatcher
*/
private $dispatcher;

public function __construct(Dispatcher $dispatcher)
{
$this->dispatcher = $dispatcher;
}

/**
* Handle the event.
*
Expand All @@ -31,10 +41,8 @@ public function handle(ReactionHasBeenRemoved $event): void
$reaction = $event->getReaction();
$reactant = $reaction->getReactant();

(new ReactionCounterService($reactant))
->removeReaction($reaction);

(new ReactionTotalService($reactant))
->removeReaction($reaction);
$this->dispatcher->dispatch(
new DecrementAggregatesJob($reactant, $reaction)
);
}
}
22 changes: 15 additions & 7 deletions src/Reactant/Listeners/IncrementAggregates.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,23 @@

namespace Cog\Laravel\Love\Reactant\Listeners;

use Cog\Laravel\Love\Reactant\ReactionCounter\Services\ReactionCounterService;
use Cog\Laravel\Love\Reactant\ReactionTotal\Services\ReactionTotalService;
use Cog\Laravel\Love\Reactant\Jobs\IncrementAggregatesJob;
use Cog\Laravel\Love\Reaction\Events\ReactionHasBeenAdded;
use Illuminate\Contracts\Bus\Dispatcher;
use Illuminate\Contracts\Queue\ShouldQueue;

final class IncrementAggregates implements ShouldQueue
{
/**
* @var \Illuminate\Contracts\Bus\Dispatcher
*/
private $dispatcher;

public function __construct(Dispatcher $dispatcher)
{
$this->dispatcher = $dispatcher;
}

/**
* Handle the event.
*
Expand All @@ -31,10 +41,8 @@ public function handle(ReactionHasBeenAdded $event): void
$reaction = $event->getReaction();
$reactant = $reaction->getReactant();

(new ReactionCounterService($reactant))
->addReaction($reaction);

(new ReactionTotalService($reactant))
->addReaction($reaction);
$this->dispatcher->dispatch(
new IncrementAggregatesJob($reactant, $reaction)
);
}
}

0 comments on commit be15cae

Please sign in to comment.