Skip to content

Commit

Permalink
minor: introduce PerssitenceManager::isPersisted() (#754)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil authored Dec 24, 2024
1 parent 9810cbb commit 5f99506
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 18 deletions.
9 changes: 7 additions & 2 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,14 @@ public function isPersistenceAvailable(): bool
return (bool) $this->persistence;
}

public function assertPersistanceEnabled(): void
public function isPersistenceEnabled(): bool
{
if (!$this->isPersistenceAvailable() || !$this->persistence()->isEnabled()) {
return $this->isPersistenceAvailable() && $this->persistence()->isEnabled();
}

public function assertPersistenceEnabled(): void
{
if (!$this->isPersistenceEnabled()) {
throw new PersistenceDisabled('Cannot get repository when persist is disabled.');
}
}
Expand Down
13 changes: 3 additions & 10 deletions src/Persistence/IsProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,10 @@ public function _initializeLazyObject(): void

private function isPersisted(): bool
{
try {
$this->_refresh();

return true;
} catch (RefreshObjectFailed $e) {
if ($e->objectWasDeleted()) {
return false;
}
$this->initializeLazyObject();
$object = $this->lazyObjectState->realInstance;

throw $e;
}
return Configuration::instance()->persistence()->isPersisted($object);
}

private function _autoRefresh(): void
Expand Down
12 changes: 12 additions & 0 deletions src/Persistence/PersistenceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,18 @@ public function refresh(object &$object, bool $force = false): object
return $object;
}

public function isPersisted(object $object): bool
{
if ($object instanceof Proxy) {
$object = unproxy($object);
}

$om = $this->strategyFor($object::class)->objectManagerFor($object::class);
$id = $om->getClassMetadata($object::class)->getIdentifierValues($object);

return $id && $om->find($object::class, $id) !== null;
}

/**
* @template T of object
*
Expand Down
5 changes: 3 additions & 2 deletions src/Persistence/PersistentObjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public static function all(): array
*/
public static function repository(): ObjectRepository
{
Configuration::instance()->assertPersistanceEnabled();
Configuration::instance()->assertPersistenceEnabled();

return new RepositoryDecorator(static::class()); // @phpstan-ignore return.type
}
Expand Down Expand Up @@ -279,11 +279,12 @@ protected function normalizeParameter(string $field, mixed $value): mixed

// we create now the object to prevent "non-nullable" property errors,
// but we'll need to remove it once the current object is created

$inversedObject = unproxy($value->create());
$this->tempAfterPersist[] = static function(object $object) use ($value, $inverseField, $pm, $inversedObject) {
// we cannot use the already created $inversedObject:
// because we must also remove its potential newly created owner (here: "$oldObj")
// but a cascade:["persist"] would remove too many things
// but a cascade:["remove"] would remove too many things
$value->create([$inverseField => $object]);
$pm->refresh($object);
$oldObj = get($inversedObject, $inverseField);
Expand Down
2 changes: 1 addition & 1 deletion src/Persistence/PersistentProxyObjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ final public static function all(): array
*/
final public static function repository(): ObjectRepository
{
Configuration::instance()->assertPersistanceEnabled();
Configuration::instance()->assertPersistenceEnabled();

return new ProxyRepositoryDecorator(static::class()); // @phpstan-ignore argument.type, return.type
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public function one_to_one_owning(): void
#[Test]
#[DataProvider('provideCascadeRelationshipsCombinations')]
#[UsingRelationships(Address::class, ['contact'])]
#[UsingRelationships(Contact::class, ['address'])]
#[UsingRelationships(Contact::class, ['address', 'category'])]
public function inversed_one_to_one(): void
{
$address = static::addressFactory()->create(['contact' => static::contactFactory()]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,11 @@ public function can_remove_and_assert_not_persisted(): void

/** @test */
#[Test]
public function cannot_use_assert_persisted_when_entity_has_changes(): void
public function can_use_assert_persisted_when_entity_has_changes(): void
{
$contact = static::contactFactory()->create();
$contact->setName('foo');

$this->expectException(RefreshObjectFailed::class);
$contact->_assertPersisted();
}

Expand Down

0 comments on commit 5f99506

Please sign in to comment.