Total Complexity | 9 |
Total Lines | 54 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
17 | final class ObjectInstantiator extends ReflectionClass implements Instantiator |
||
18 | { |
||
19 | /** @throws InstantiationFailure */ |
||
20 | private function __construct(string $class) |
||
21 | { |
||
22 | try { |
||
23 | parent::__construct($class); |
||
24 | } catch (ReflectionException $exception) { |
||
25 | throw NoSuchClass::encountered($exception); |
||
26 | } |
||
27 | if ($this->isAbstract()) { |
||
28 | throw NoConcreteClass::cannotInstantiate($class); |
||
29 | } |
||
30 | if ($this->isInterface()) { |
||
31 | throw NotAClass::cannotInstantiate($class); |
||
32 | } |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * Produces a new instantiator. |
||
37 | * |
||
38 | * @param string $class The class to create the instantiator for. |
||
39 | * @return Instantiator The instantiator for the class. |
||
40 | * @throws InstantiationFailure |
||
41 | */ |
||
42 | public static function forThe(string $class): Instantiator |
||
43 | { |
||
44 | return new ObjectInstantiator($class); |
||
45 | } |
||
46 | |||
47 | /** @inheritdoc */ |
||
48 | public function instance(): object |
||
49 | { |
||
50 | try { |
||
51 | return $this->newInstanceWithoutConstructor(); |
||
52 | } catch (Throwable $exception) { |
||
53 | return $this->newInstanceFromDeserialization(); |
||
54 | } |
||
55 | } |
||
56 | |||
57 | /** @inheritdoc */ |
||
58 | public function class(): string |
||
59 | { |
||
60 | return $this->getName(); |
||
61 | } |
||
62 | |||
63 | private function newInstanceFromDeserialization(): object |
||
71 | } |
||
72 | } |
||
73 |