Skip to content

Commit 03e7f31

Browse files
committed
refactor(ProxyGeneratorInterface): Use class as input, remove dependency on ObjectToObjectMetadata.
1 parent e5f3aaf commit 03e7f31

File tree

4 files changed

+20
-27
lines changed

4 files changed

+20
-27
lines changed

src/Transformer/ObjectToObjectMetadata/Implementation/ObjectToObjectMetadataFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ public function createObjectToObjectMetadata(
333333

334334
try {
335335
$proxySpecification = $this->proxyGenerator
336-
->generateTargetProxy($objectToObjectMetadata);
336+
->generateProxy($targetClass);
337337

338338
// determine if the constructor contains eager properties. if it
339339
// does, then the constructor is eager

src/Transformer/Proxy/Exception/ProxyNotSupportedException.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,20 @@
1414
namespace Rekalogika\Mapper\Transformer\Proxy\Exception;
1515

1616
use Rekalogika\Mapper\Exception\RuntimeException;
17-
use Rekalogika\Mapper\Transformer\ObjectToObjectMetadata\ObjectToObjectMetadata;
1817

1918
class ProxyNotSupportedException extends RuntimeException
2019
{
2120
private string $reason;
2221

23-
public function __construct(ObjectToObjectMetadata $metadata, \Throwable $previous)
22+
/**
23+
* @param class-string $class
24+
*/
25+
public function __construct(string $class, \Throwable $previous)
2426
{
2527
parent::__construct(
2628
sprintf(
27-
'Creating target proxy is not supported for target class "%s" and source class "%s"',
28-
$metadata->getTargetClass(),
29-
$metadata->getSourceClass()
29+
'Creating a proxy for class "%s" is not supported.',
30+
$class
3031
),
3132
previous: $previous
3233
);

src/Transformer/Proxy/Implementation/ProxyGenerator.php

+11-17
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace Rekalogika\Mapper\Transformer\Proxy\Implementation;
1515

16-
use Rekalogika\Mapper\Transformer\ObjectToObjectMetadata\ObjectToObjectMetadata;
1716
use Rekalogika\Mapper\Transformer\Proxy\Exception\ProxyNotSupportedException;
1817
use Rekalogika\Mapper\Transformer\Proxy\ProxyGeneratorInterface;
1918
use Rekalogika\Mapper\Transformer\Proxy\ProxySpecification;
@@ -22,45 +21,40 @@
2221

2322
final class ProxyGenerator implements ProxyGeneratorInterface
2423
{
25-
public function generateTargetProxy(ObjectToObjectMetadata $metadata): ProxySpecification
24+
public function generateProxy(string $class): ProxySpecification
2625
{
27-
$sourceClass = $metadata->getSourceClass();
28-
$targetClass = $metadata->getTargetClass();
29-
3026
/** @var class-string */
31-
$proxyClass = $this->generateProxyClassName($sourceClass, $targetClass);
27+
$proxyClass = $this->generateProxyClassName($class);
3228

3329
try {
34-
$proxyCode = $this->generateProxyCode($sourceClass, $targetClass);
30+
$proxyCode = $this->generateProxyCode($class);
3531
} catch (LogicException $e) {
36-
throw new ProxyNotSupportedException($metadata, $e);
32+
throw new ProxyNotSupportedException($class, $e);
3733
}
3834

3935
return new ProxySpecification($proxyClass, $proxyCode);
4036
}
4137

4238
/**
43-
* @param class-string $sourceClass
44-
* @param class-string $targetClass
39+
* @param class-string $class
4540
* @return string
4641
*/
47-
private function generateProxyClassName(string $sourceClass, string $targetClass): string
42+
private function generateProxyClassName(string $class): string
4843
{
4944
return sprintf(
5045
'Rekalogika\Mapper\Generated\__CG__\%s',
51-
$targetClass
46+
$class
5247
);
5348
}
5449

5550
/**
56-
* @param class-string $sourceClass
57-
* @param class-string $targetClass
51+
* @param class-string $class
5852
* @return string
5953
*/
60-
private function generateProxyCode(string $sourceClass, string $targetClass): string
54+
private function generateProxyCode(string $class): string
6155
{
62-
$proxyClass = $this->generateProxyClassName($sourceClass, $targetClass);
63-
$targetReflection = new \ReflectionClass($targetClass);
56+
$proxyClass = $this->generateProxyClassName($class);
57+
$targetReflection = new \ReflectionClass($class);
6458

6559
// get proxy class name & namespace
6660
$shortName = preg_replace('/.*\\\\/', '', $proxyClass);

src/Transformer/Proxy/ProxyGeneratorInterface.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@
1313

1414
namespace Rekalogika\Mapper\Transformer\Proxy;
1515

16-
use Rekalogika\Mapper\Transformer\ObjectToObjectMetadata\ObjectToObjectMetadata;
1716
use Rekalogika\Mapper\Transformer\Proxy\Exception\ProxyNotSupportedException;
1817

1918
interface ProxyGeneratorInterface
2019
{
2120
/**
21+
* @param class-string $class
2222
* @throws ProxyNotSupportedException
2323
*/
24-
public function generateTargetProxy(
25-
ObjectToObjectMetadata $metadata
26-
): ProxySpecification;
24+
public function generateProxy(string $class): ProxySpecification;
2725
}

0 commit comments

Comments
 (0)