Skip to content

Commit

Permalink
feat: add softDelete for new models
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoFerrazfs committed Aug 17, 2023
1 parent ec9ab6a commit 5f60870
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 185 deletions.
75 changes: 17 additions & 58 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php
namespace Mongolid\Query;

use DateTime;
use InvalidArgumentException;
use MongoDB\BSON\ObjectId;
use MongoDB\BSON\UTCDateTime;
use Mongolid\Connection\Connection;
use Mongolid\Container\Container;
use Mongolid\Cursor\CacheableCursor;
Expand All @@ -13,6 +15,7 @@
use Mongolid\Model\Exception\ModelNotFoundException;
use Mongolid\Model\ModelInterface;
use Mongolid\Util\ObjectIdUtils;
use Mongolid\Util\QueryBuilder;

/**
* This class will abstract how a Model is persisted and retrieved
Expand Down Expand Up @@ -171,6 +174,10 @@ public function update(ModelInterface $model, array $options = []): bool
*/
public function delete(ModelInterface $model, array $options = []): bool
{
if (($model->enabledSoftDeletes ?? false) && !($model->forceDelete ?? false)) {
return $this->executeSoftDelete($model, $options);
}

if (false === $this->fireEvent('deleting', $model, true)) {
return false;
}
Expand Down Expand Up @@ -207,7 +214,7 @@ public function where(ModelInterface $model, $query = [], array $projection = []
$model->getCollection(),
'find',
[
$this->prepareValueQuery($query),
QueryBuilder::resolveQuery($query, $model),
[
'projection' => $this->prepareProjection($projection),
'eagerLoads' => $model->with ?? [],
Expand Down Expand Up @@ -247,7 +254,7 @@ public function first(ModelInterface $model, $query = [], array $projection = []
}

return $model->getCollection()->findOne(
$this->prepareValueQuery($query),
QueryBuilder::resolveQuery($query, $model),
['projection' => $this->prepareProjection($projection)],
);
}
Expand All @@ -274,62 +281,6 @@ public function firstOrFail(ModelInterface $model, $query = [], array $projectio
throw (new ModelNotFoundException())->setModel(get_class($model));
}

/**
* Transforms a value that is not an array into an MongoDB query (array).
* This method will take care of converting a single value into a query for
* an _id, including when a objectId is passed as a string.
*
* @param mixed $value the _id of the model
*
* @return array Query for the given _id
*/
protected function prepareValueQuery($value): array
{
if (!is_array($value)) {
$value = ['_id' => $value];
}

if (isset($value['_id']) &&
is_string($value['_id']) &&
ObjectIdUtils::isObjectId($value['_id'])
) {
$value['_id'] = new ObjectId($value['_id']);
}

if (isset($value['_id']) &&
is_array($value['_id'])
) {
$value['_id'] = $this->prepareArrayFieldOfQuery($value['_id']);
}

return $value;
}

/**
* Prepares an embedded array of an query. It will convert string ObjectIds
* in operators into actual objects.
*
* @param array $value array that will be treated
*
* @return array prepared array
*/
protected function prepareArrayFieldOfQuery(array $value): array
{
foreach (['$in', '$nin'] as $operator) {
if (isset($value[$operator]) &&
is_array($value[$operator])
) {
foreach ($value[$operator] as $index => $id) {
if (ObjectIdUtils::isObjectId($id)) {
$value[$operator][$index] = new ObjectId($id);
}
}
}
}

return $value;
}

/**
* Triggers an event. May return if that event had success.
*
Expand Down Expand Up @@ -476,4 +427,12 @@ private function getUpdateData($model, array $data): array

return $changes;
}

private function executeSoftDelete(ModelInterface $entity, $options): bool
{
$deletedAtCoullum = QueryBuilder::getDeletedAtColumn($entity);
$entity->$deletedAtCoullum = new UTCDateTime(new DateTime('now'));

return $this->update($entity, $options);
}
}
2 changes: 1 addition & 1 deletion tests/Integration/EagerLoadingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use MongoDB\BSON\ObjectId;
use Mongolid\Container\Container;
use Mongolid\Tests\Stubs\Price;
use Mongolid\Tests\Stubs\Product;
use Mongolid\Tests\Stubs\Legacy\Product;
use Mongolid\Tests\Stubs\ReferencedUser;
use Mongolid\Tests\Stubs\Shop;
use Mongolid\Tests\Stubs\Sku;
Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/PersisteModelWithSoftDeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use MongoDB\BSON\ObjectId;
use MongoDB\BSON\UTCDateTime;
use Mongolid\Model\ModelInterface;
use Mongolid\Tests\Stubs\Product;
use Mongolid\Tests\Stubs\ProductWithSoftDelete;
use Mongolid\Tests\Stubs\Legacy\ProductWithSoftDelete;
use Mongolid\Tests\Stubs\Legacy\Product;

final class PersisteModelWithSoftDeleteTest extends IntegrationTestCase
{
Expand Down
5 changes: 4 additions & 1 deletion tests/Stubs/Product.php → tests/Stubs/Legacy/Product.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php
namespace Mongolid\Tests\Stubs;
namespace Mongolid\Tests\Stubs\Legacy;

use Mongolid\LegacyRecord;
use Mongolid\Model\Relations\ReferencesOne;
use Mongolid\Tests\Stubs\Price;
use Mongolid\Tests\Stubs\Shop;
use Mongolid\Tests\Stubs\Sku;

class Product extends LegacyRecord
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Mongolid\Tests\Stubs;
namespace Mongolid\Tests\Stubs\Legacy;

use Mongolid\Model\SoftDeletesTrait;

Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/DataMapper/DataMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Mongolid\Event\EventTriggerService;
use Mongolid\Model\ModelInterface;
use Mongolid\Schema\Schema;
use Mongolid\Tests\Stubs\Product;
use Mongolid\Tests\Stubs\Legacy\Product;
use stdClass;
use Mongolid\TestCase;

Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Model/HasLegacyRelationTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Mongolid\TestCase;
use Mongolid\Tests\Stubs\EmbeddedUser;
use Mongolid\Tests\Stubs\Price;
use Mongolid\Tests\Stubs\Product;
use Mongolid\Tests\Stubs\Legacy\Product;
use Mongolid\Tests\Stubs\ReferencedUser;
use Mongolid\Util\CacheComponent;
use Mongolid\Util\CacheComponentInterface;
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/Model/SoftDeletesTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
namespace Mongolid\Model;

use DateTime;
use Mockery as m;
use MongoDB\BSON\UTCDateTime;
use Mongolid\Cursor\CursorInterface;
use Mongolid\DataMapper\DataMapper;
use Mongolid\Schema\DynamicSchema;
use Mongolid\TestCase;
use Mongolid\Tests\Stubs\Product;
use Mockery as m;
use Mongolid\Tests\Stubs\ProductWithSoftDelete;
use Mongolid\Tests\Stubs\Legacy\ProductWithSoftDelete;
use Mongolid\Tests\Stubs\Legacy\Product;

class SoftDeletesTraitTest extends TestCase
{
Expand Down
Loading

0 comments on commit 5f60870

Please sign in to comment.