Skip to content

Commit

Permalink
Got model restoration working on sumcache.
Browse files Browse the repository at this point in the history
  • Loading branch information
kirkbushell committed Aug 26, 2023
1 parent bec7a64 commit 4f3f330
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 29 deletions.
18 changes: 18 additions & 0 deletions src/Behaviours/Cacheable.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Eloquence\Behaviours;

use Closure;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
Expand All @@ -10,6 +11,13 @@
*/
trait Cacheable
{
/**
* Allows cacheable to work with implementors and their unique relationship methods.
*
* @return array
*/
abstract private function relationsMethod(): array;

/**
* Helper method for easier use of the implementing classes.
*/
Expand All @@ -18,6 +26,16 @@ public static function for(Model $model): self
return new self($model);
}

/**
* Applies the provided function using the relevant configuration to all configured relations.
*/
public function apply(Closure $function): void
{
foreach ($this->relationsMethod() as $key => $value) {
$function($this->config($key, $value));
}
}

/**
* Updates a table's record based on the query information provided in the $config variable.
*
Expand Down
11 changes: 2 additions & 9 deletions src/Behaviours/CountCache/CountCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,9 @@ class CountCache

private function __construct(private Countable $model) {}

/**
* Applies the provided function to the count cache setup/configuration.
*
* @param \Closure $function
*/
public function apply(\Closure $function)
private function relationsMethod(): array
{
foreach ($this->model->countedBy() as $key => $value) {
$function($this->config($key, $value));
}
return $this->model->countedBy();
}

/**
Expand Down
3 changes: 0 additions & 3 deletions src/Behaviours/CountCache/HasCounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

trait HasCounts
{
/**
* Boot the countable behaviour and setup the appropriate event bindings.
*/
public static function bootHasCounts()
{
static::observe(Observer::class);
Expand Down
4 changes: 0 additions & 4 deletions src/Behaviours/SumCache/HasSums.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
<?php
namespace Eloquence\Behaviours\SumCache;

use Eloquence\Behaviours\Cacheable;

trait HasSums
{
use Cacheable;

public static function bootHasSums()
{
static::observe(Observer::class);
Expand Down
19 changes: 6 additions & 13 deletions src/Behaviours/SumCache/SumCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,29 @@ class SumCache

private function __construct(private Summable $model) {}

/**
* Applies the provided function to the count cache setup/configuration.
*
* @param \Closure $function
*/
public function apply(\Closure $function)
private function relationsMethod(): array
{
foreach ($this->model->summedBy() as $key => $cache) {
$function($this->config($key, $cache));
}
return $this->model->summedBy();
}

/**
* Rebuild the count caches from the database
*/
public function rebuild()
public function rebuild(): void
{
$this->apply(function($config) {
$this->rebuildCacheRecord($config, $this->model, 'SUM', $config['columnToSum']);
});
}

public function increase()
public function increase(): void
{
$this->apply(function(CacheConfig $config) {
$this->updateCacheValue($config->relatedModel($this->model), $config, $this->model->{$config->sourceField});
});
}

public function decrease()
public function decrease(): void
{
$this->apply(function(CacheConfig $config) {
$this->updateCacheValue($config->relatedModel($this->model), $config, -$this->model->{$config->sourceField});
Expand All @@ -52,7 +45,7 @@ public function decrease()
/**
* Update the cache for all operations.
*/
public function update()
public function update(): void
{
$this->apply(function($config) {
$foreignKey = $config->foreignKeyName($this->model);
Expand Down
7 changes: 7 additions & 0 deletions tests/Acceptance/SumCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ function test_whenAnAggregatedModelValueSwitchesContext()
$this->assertEquals(45, $order->fresh()->totalAmount);
}

function test_aggregateValuesAreUpdatedWhenModelsAreRestored()
{
$this->data['item']->delete();

$this->assertEquals(0, $this->data['order']->fresh()->totalAmount);
}

private function setupOrderAndItem()
{
$order = new Order;
Expand Down

0 comments on commit 4f3f330

Please sign in to comment.