1010
1111namespace CleverAge \RestProcessBundle \Task ;
1212
13+ use CleverAge \RestProcessBundle \Exception \MissingClientException ;
1314use CleverAge \RestProcessBundle \Registry \ClientRegistry ;
1415use CleverAge \ProcessBundle \Configuration \TaskConfiguration ;
1516use CleverAge \ProcessBundle \Model \AbstractConfigurableTask ;
1617use CleverAge \ProcessBundle \Model \ProcessState ;
1718use Psr \Log \LoggerInterface ;
19+ use Symfony \Component \OptionsResolver \Exception \AccessException ;
20+ use Symfony \Component \OptionsResolver \Exception \ExceptionInterface ;
21+ use Symfony \Component \OptionsResolver \Exception \UndefinedOptionsException ;
1822use Symfony \Component \OptionsResolver \OptionsResolver ;
1923
2024/**
2428 */
2529class RequestTask extends AbstractConfigurableTask
2630{
27-
2831 /** @var LoggerInterface */
2932 protected $ logger ;
3033
3134 /** @var ClientRegistry */
3235 protected $ registry ;
3336
3437 /**
35- * RequestTask constructor.
36- *
3738 * @param LoggerInterface $logger
39+ * @param ClientRegistry $registry
3840 */
3941 public function __construct (LoggerInterface $ logger , ClientRegistry $ registry )
4042 {
@@ -46,32 +48,20 @@ public function __construct(LoggerInterface $logger, ClientRegistry $registry)
4648 * {@inheritdoc}
4749 * @param ProcessState $state
4850 *
49- * @throws \CleverAge\RestProcessBundle\Exception\ MissingClientException
50- * @throws \Symfony\Component\OptionsResolver\Exception\ ExceptionInterface
51+ * @throws MissingClientException
52+ * @throws ExceptionInterface
5153 */
52- public function execute (ProcessState $ state )
54+ public function execute (ProcessState $ state ): void
5355 {
5456 $ options = $ this ->getOptions ($ state );
5557
56- $ client = $ this ->registry ->getClient ($ options ['client ' ]);
57-
58- $ requestOptions = [
59- 'method ' => $ options ['method ' ],
60- 'url ' => $ options ['url ' ],
61- 'headers ' => $ options ['headers ' ],
62- 'url_parameters ' => $ options ['url_parameters ' ],
63- 'query_parameters ' => $ options ['query_parameters ' ],
64- 'sends ' => $ options ['sends ' ],
65- 'expects ' => $ options ['expects ' ],
66- ];
58+ $ requestOptions = $ this ->getRequestOptions ($ state );
6759
68- $ input = $ state ->getInput () ?: [];
69- $ requestOptions = array_merge ($ requestOptions , $ input );
7060 $ this ->logger ->debug (
71- "Sending request {$ options ['method ' ]} to ' {$ options ['url ' ]}' " ,
61+ "Sending request {$ requestOptions ['method ' ]} to ' {$ requestOptions ['url ' ]}' " ,
7262 ['requestOptions ' => $ requestOptions ]
7363 );
74- $ result = $ client ->call ($ requestOptions );
64+ $ result = $ this -> registry -> getClient ( $ options [ ' client ' ]) ->call ($ requestOptions );
7565 if ($ options ['log_response ' ]) {
7666 $ this ->logger ->debug (
7767 "Response received from ' {$ options ['url ' ]}' " ,
@@ -108,12 +98,12 @@ public function execute(ProcessState $state)
10898 }
10999
110100 /**
111- * @param \Symfony\Component\OptionsResolver\ OptionsResolver $resolver
101+ * @param OptionsResolver $resolver
112102 *
113- * @throws \Symfony\Component\OptionsResolver\Exception\ UndefinedOptionsException
114- * @throws \Symfony\Component\OptionsResolver\Exception\ AccessException
103+ * @throws UndefinedOptionsException
104+ * @throws AccessException
115105 */
116- protected function configureOptions (OptionsResolver $ resolver )
106+ protected function configureOptions (OptionsResolver $ resolver ): void
117107 {
118108 $ resolver ->setRequired (
119109 [
@@ -140,4 +130,30 @@ protected function configureOptions(OptionsResolver $resolver)
140130 $ resolver ->setAllowedTypes ('valid_response_code ' , ['array ' ]);
141131 $ resolver ->setAllowedTypes ('log_response ' , ['bool ' ]);
142132 }
133+
134+ /**
135+ * @param ProcessState $state
136+ *
137+ * @throws ExceptionInterface
138+ *
139+ * @return array
140+ */
141+ protected function getRequestOptions (ProcessState $ state ): array
142+ {
143+ $ options = $ this ->getOptions ($ state );
144+
145+ $ requestOptions = [
146+ 'method ' => $ options ['method ' ],
147+ 'url ' => $ options ['url ' ],
148+ 'headers ' => $ options ['headers ' ],
149+ 'url_parameters ' => $ options ['url_parameters ' ],
150+ 'query_parameters ' => $ options ['query_parameters ' ],
151+ 'sends ' => $ options ['sends ' ],
152+ 'expects ' => $ options ['expects ' ],
153+ ];
154+
155+ $ input = $ state ->getInput () ?: [];
156+
157+ return array_merge ($ requestOptions , $ input );
158+ }
143159}
0 commit comments