Skip to content

Commit

Permalink
refact: add PR suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoFerrazfs committed Aug 24, 2023
1 parent 7727096 commit 4bde2ad
Show file tree
Hide file tree
Showing 15 changed files with 219 additions and 191 deletions.
10 changes: 7 additions & 3 deletions src/DataMapper/DataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace Mongolid\DataMapper;

use DateTime;
use InvalidArgumentException;
use MongoDB\BSON\UTCDateTime;
use MongoDB\Collection;
use Mongolid\Container\Container;
use Mongolid\Cursor\CursorInterface;
Expand All @@ -27,6 +25,8 @@
*/
class DataMapper implements HasSchemaInterface
{
public bool $withTrashed = false;

/**
* Name of the schema class to be used.
*
Expand Down Expand Up @@ -261,12 +261,16 @@ public function where(

$model = new $this->schema->entityClass;

$query = $this->withTrashed ?
QueryBuilder::prepareValueQuery($query) :
QueryBuilder::resolveQuery($query, $model);

return new $cursorClass(
$this->schema,
$this->getCollection(),
'find',
[
QueryBuilder::resolveQuery($query, $model),
$query,
[
'projection' => $this->prepareProjection($projection),
'eagerLoads' => $model->with ?? [],
Expand Down
4 changes: 2 additions & 2 deletions src/LegacyRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function update(): bool
*/
public function delete(): bool
{
if (($this->enabledSoftDeletes ?? false) && !($this->forceDelete ?? false)) {
if ($this->isSoftDeleteEnabled ?? false) {
return $this->executeSoftDelete();
}

Expand Down Expand Up @@ -362,7 +362,7 @@ protected function execute(string $action)
*
* @return mixed
*/
private static function getDataMapperInstance()
protected static function getDataMapperInstance()
{
$instance = Container::make(get_called_class());

Expand Down
4 changes: 2 additions & 2 deletions src/Model/AbstractModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public static function firstOrNew($id)
/**
* Returns a valid instance from Ioc.
*/
private static function getBuilderInstance(): Builder
protected static function getBuilderInstance(): Builder
{
$instance = new static();

Expand Down Expand Up @@ -176,7 +176,7 @@ public function update(): bool
*/
public function delete(): bool
{
if (($this->enabledSoftDeletes ?? false) && !($this->forceDelete ?? false)) {
if ($this->isSoftDeleteEnabled ?? false) {
return $this->executeSoftDelete();
}

Expand Down
84 changes: 84 additions & 0 deletions src/Model/SoftDeleteTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Mongolid\Model;

use DateTime;
use MongoDB\BSON\UTCDateTime;
use Mongolid\Cursor\CursorInterface;
use Mongolid\Util\QueryBuilder;

trait SoftDeleteTrait
{
public bool $isSoftDeleteEnabled = true;

public function isTrashed(): bool
{
return !is_null($this->{QueryBuilder::getDeletedAtColumn($this)});
}

public function restore(): bool
{
$collumn = QueryBuilder::getDeletedAtColumn($this);

if (!$this->isTrashed()) {
return false;
}

unset($this->{$collumn});

return $this->update();
}

public function forceDelete(): bool
{
return $this->execute('delete');
}

public function executeSoftDelete(): bool
{
$deletedAtColumn = QueryBuilder::getDeletedAtColumn($this);
$this->$deletedAtColumn = new UTCDateTime(new DateTime('now'));

return $this->update();
}

public static function withTrashed(
mixed $query = [],
array $projection = [],
bool $useCache = false
): CursorInterface {
return self::performSearch($query, $projection, $useCache);
}

private static function whereWithMapper(mixed $query, array $projection, bool $useCache)
{
$mapper = self::getDataMapperInstance();

$mapper->withTrashed = true;

return $mapper->where($query, $projection, $useCache);
}

private static function whereWithBuilder(mixed $query, array $projection, bool $useCache)
{
$mapper = self::getBuilderInstance();

$mapper->withTrashed = true;

return $mapper->where(
new static(),
$query,
$projection,
$useCache
);
}

private static function performSearch(mixed $query, array $projection, bool $useCache)
{
$isLegacy = method_exists(self::class, 'getDataMapperInstance');

return $isLegacy ?
self::whereWithMapper($query, $projection, $useCache) :
self::whereWithBuilder($query, $projection, $useCache);
}
}
69 changes: 0 additions & 69 deletions src/Model/SoftDeletesTrait.php

This file was deleted.

11 changes: 7 additions & 4 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
<?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 @@ -23,6 +20,8 @@
*/
class Builder
{
public bool $withTrashed = false;

/**
* Connection that is going to be used to interact with the database.
*
Expand Down Expand Up @@ -206,11 +205,15 @@ public function where(ModelInterface $model, $query = [], array $projection = []
{
$cursor = $useCache ? CacheableCursor::class : Cursor::class;

$query = $this->withTrashed ?
QueryBuilder::prepareValueQuery($query) :
QueryBuilder::resolveQuery($query, $model);

return new $cursor(
$model->getCollection(),
'find',
[
QueryBuilder::resolveQuery($query, $model),
$query,
[
'projection' => $this->prepareProjection($projection),
'eagerLoads' => $model->with ?? [],
Expand Down
24 changes: 11 additions & 13 deletions src/Util/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class QueryBuilder
{
public static function resolveQuery(int|array|string $query, ModelInterface $model): array
public static function resolveQuery(mixed $query, ModelInterface $model): array
{
$query = self::prepareValueQuery($query);

Expand All @@ -28,7 +28,7 @@ public static function getDeletedAtColumn(ModelInterface $model): string
* 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.
*/
public static function prepareValueQuery(int|array|string $value): array
public static function prepareValueQuery(mixed $value): array
{
if (!is_array($value)) {
$value = ['_id' => $value];
Expand All @@ -54,20 +54,18 @@ public static function prepareValueQuery(int|array|string $value): array

private static function addSoftDeleteFilterIfRequired(array $query, ModelInterface $model): array
{
$field = self::getDeletedAtColumn($model);
if ($model->isSoftDeleteEnabled) {
$field = self::getDeletedAtColumn($model);

if (isset($query['withTrashed'])) {
unset($query['withTrashed']);

return $query;
return array_merge(
$query,
[
$field => ['$exists' => false],
]
);
}

return array_merge(
$query,
[
$field => ['$exists' => false],
]
);
return $query;
}

/**
Expand Down
Loading

0 comments on commit 4bde2ad

Please sign in to comment.