Skip to content

Commit

Permalink
feat(ObjectToObjectMetadata): Store the reason if the object cannot…
Browse files Browse the repository at this point in the history
… use proxy.
  • Loading branch information
priyadi committed Feb 8, 2024
1 parent 7fe4824 commit b337c8d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## 0.7.3

* test: Add tests for read-only targets.
* feat(`ObjectToObjectMetadata`): Store the reason if the object cannot use
proxy.

## 0.7.2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,8 @@ public function createObjectToObjectMetadata(
$objectToObjectMetadata = $objectToObjectMetadata
->withTargetProxy($proxySpecification, $skippedProperties);
} catch (ProxyNotSupportedException $e) {
// do nothing
$objectToObjectMetadata = $objectToObjectMetadata
->withReasonCannotUseProxy($e->getReason());
}

return $objectToObjectMetadata;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public function __construct(
private ?string $targetProxyClass = null,
private ?string $targetProxyCode = null,
private array $targetProxySkippedProperties = [],
private ?string $cannotUseProxyReason = null,
) {
$constructorPropertyMappings = [];
$lazyPropertyMappings = [];
Expand Down Expand Up @@ -116,7 +117,29 @@ public function withTargetProxy(
$this->targetReadOnly,
$proxySpecification->getClass(),
$proxySpecification->getCode(),
$targetProxySkippedProperties
$targetProxySkippedProperties,
cannotUseProxyReason: null
);
}

public function withReasonCannotUseProxy(
string $reason
): self {
return new self(
$this->sourceClass,
$this->targetClass,
$this->providedTargetClass,
$this->allPropertyMappings,
$this->instantiable,
$this->cloneable,
$this->initializableTargetPropertiesNotInSource,
$this->sourceModifiedTime,
$this->targetModifiedTime,
$this->targetReadOnly,
null,
null,
[],
cannotUseProxyReason: $reason,
);
}

Expand Down Expand Up @@ -259,4 +282,9 @@ public function isTargetReadOnly(): bool
{
return $this->targetReadOnly;
}

public function getCannotUseProxyReason(): ?string
{
return $this->cannotUseProxyReason;
}
}
11 changes: 10 additions & 1 deletion src/Transformer/Proxy/Exception/ProxyNotSupportedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

class ProxyNotSupportedException extends RuntimeException
{
public function __construct(ObjectToObjectMetadata $metadata, \Throwable $previous = null)
private string $reason;

public function __construct(ObjectToObjectMetadata $metadata, \Throwable $previous)
{
parent::__construct(
sprintf(
Expand All @@ -28,5 +30,12 @@ public function __construct(ObjectToObjectMetadata $metadata, \Throwable $previo
),
previous: $previous
);

$this->reason = $previous->getMessage();
}

public function getReason(): string
{
return $this->reason;
}
}

0 comments on commit b337c8d

Please sign in to comment.