1212use Dot \AnnotatedServices \Annotation \Inject ;
1313use Dot \AnnotatedServices \Exception \InvalidArgumentException ;
1414use Dot \AnnotatedServices \Exception \RuntimeException ;
15+ use Psr \Container \ContainerExceptionInterface ;
1516use Psr \Container \ContainerInterface ;
17+ use Psr \Container \NotFoundExceptionInterface ;
18+ use ReflectionClass ;
19+ use ReflectionException ;
20+ use ReflectionMethod ;
1621
1722/**
1823 * Class AnnotatedServiceFactory
@@ -23,7 +28,10 @@ class AnnotatedServiceFactory extends AbstractAnnotatedFactory
2328 /**
2429 * @param ContainerInterface $container
2530 * @param $requestedName
26- * @return null
31+ * @return mixed
32+ * @throws ContainerExceptionInterface
33+ * @throws NotFoundExceptionInterface
34+ * @throws ReflectionException
2735 */
2836 public function __invoke (ContainerInterface $ container , $ requestedName )
2937 {
@@ -34,33 +42,31 @@ public function __invoke(ContainerInterface $container, $requestedName)
3442 * @param ContainerInterface $container
3543 * @param $requestedName
3644 * @return mixed
45+ * @throws ContainerExceptionInterface
46+ * @throws NotFoundExceptionInterface
47+ * @throws ReflectionException
3748 */
3849 public function createObject (ContainerInterface $ container , $ requestedName )
3950 {
4051 if (!class_exists ($ requestedName )) {
41- throw new RuntimeException (sprintf (
42- 'Annotated factories can only be used with services that are identified by their FQCN. ' .
43- 'Provided "%s" service name is not a valid class. ' ,
44- $ requestedName
45- ));
52+ throw RuntimeException::classNotFound ($ requestedName );
4653 }
4754
4855 $ service = null ;
4956
5057 $ annotationReader = $ this ->createAnnotationReader ($ container );
51- $ refClass = new \ ReflectionClass ($ requestedName );
58+ $ refClass = new ReflectionClass ($ requestedName );
5259 $ constructor = $ refClass ->getConstructor ();
5360 if ($ constructor === null ) {
5461 $ service = new $ requestedName ();
5562 } else {
5663 $ inject = $ annotationReader ->getMethodAnnotation ($ constructor , Inject::class);
5764 if ($ inject === null && $ constructor ->getNumberOfRequiredParameters () > 0 ) {
58- throw new RuntimeException (sprintf (
59- 'You need to use the "%s" annotation in "%s" constructor so that the "%s" can create it. ' ,
65+ throw RuntimeException::annotationNotFound (
6066 Inject::class,
6167 $ requestedName ,
6268 static ::class
63- )) ;
69+ );
6470 }
6571
6672 $ services = [];
@@ -71,7 +77,7 @@ public function createObject(ContainerInterface $container, $requestedName)
7177 $ service = new $ requestedName (...$ services );
7278 }
7379
74- $ methods = $ refClass ->getMethods (\ ReflectionMethod::IS_PUBLIC );
80+ $ methods = $ refClass ->getMethods (ReflectionMethod::IS_PUBLIC );
7581 foreach ($ methods as $ method ) {
7682 $ inject = $ annotationReader ->getMethodAnnotation ($ method , Inject::class);
7783 if ($ inject ) {
@@ -87,6 +93,8 @@ public function createObject(ContainerInterface $container, $requestedName)
8793 * @param ContainerInterface $container
8894 * @param Inject $inject
8995 * @return array
96+ * @throws ContainerExceptionInterface
97+ * @throws NotFoundExceptionInterface
9098 */
9199 protected function getServicesToInject (ContainerInterface $ container , Inject $ inject ): array
92100 {
@@ -106,10 +114,7 @@ protected function getServicesToInject(ContainerInterface $container, Inject $in
106114 } elseif (class_exists ($ serviceKey )) {
107115 $ service = new $ serviceKey ();
108116 } else {
109- throw new RuntimeException (sprintf (
110- 'Defined injectable service "%s" could not be found in container or as a class. ' ,
111- $ serviceKey
112- ));
117+ throw RuntimeException::classNotFound ($ serviceKey );
113118 }
114119
115120 $ services [] = empty ($ parts ) ? $ service : $ this ->readKeysFromArray ($ parts , $ service );
@@ -126,7 +131,7 @@ protected function getServicesToInject(ContainerInterface $container, Inject $in
126131 protected function readKeysFromArray (array $ keys , $ array )
127132 {
128133 $ key = array_shift ($ keys );
129- // When one of the provided keys is not found, thorw an exception
134+ // When one of the provided keys is not found, throw an exception
130135 if (!isset ($ array [$ key ])) {
131136 throw new InvalidArgumentException (sprintf (
132137 'The key "%s" provided in the dotted notation could not be found in the array service ' ,
0 commit comments