1212use Keboola \PermissionChecker \BranchType ;
1313use Keboola \StorageApi \ClientException as StorageClientException ;
1414use Keboola \StorageApi \Components ;
15- use Keboola \StorageApi \ DevBranches ;
15+ use Keboola \StorageApiBranch \ ClientWrapper ;
1616use Keboola \StorageApiBranch \Factory \ClientOptions ;
1717use Keboola \StorageApiBranch \Factory \StorageClientPlainFactory ;
1818use Symfony \Component \Config \Definition \Exception \InvalidConfigurationException ;
@@ -28,14 +28,15 @@ class JobRuntimeResolver
2828
2929 private const PAY_AS_YOU_GO_FEATURE = 'pay-as-you-go ' ;
3030
31- private StorageClientPlainFactory $ storageClientFactory ;
31+ private ClientWrapper $ clientWrapper ;
32+ private Components $ componentsApiClient ;
3233 private ?array $ configuration ;
3334 private array $ componentData ;
3435 private array $ jobData ;
3536
36- public function __construct (StorageClientPlainFactory $ storageClientFactory )
37- {
38- $ this -> storageClientFactory = $ storageClientFactory ;
37+ public function __construct (
38+ private readonly StorageClientPlainFactory $ storageClientFactory ,
39+ ) {
3940 }
4041
4142 public function resolveJobData (array $ jobData , array $ tokenInfo ): array
@@ -44,13 +45,19 @@ public function resolveJobData(array $jobData, array $tokenInfo): array
4445 $ this ->jobData = $ jobData ;
4546
4647 try {
47- $ this ->componentData = $ this ->getComponentsApiClient (null )
48- ->getComponent ($ jobData ['componentId ' ]);
48+ $ this ->clientWrapper = $ this ->storageClientFactory ->createClientWrapper (new ClientOptions (
49+ token: $ jobData ['#tokenString ' ],
50+ branchId: ((string ) $ jobData ['branchId ' ]) ?: null ,
51+ ));
52+
53+ $ this ->componentsApiClient = new Components ($ this ->clientWrapper ->getBranchClient ());
54+ $ this ->componentData = $ this ->componentsApiClient ->getComponent ($ jobData ['componentId ' ]);
55+
4956 $ jobData ['tag ' ] = $ this ->resolveTag ($ jobData );
5057 $ variableValues = $ this ->resolveVariables ();
5158 $ jobData ['parallelism ' ] = $ this ->resolveParallelism ($ jobData );
5259 $ jobData ['executor ' ] = $ this ->resolveExecutor ($ jobData )->value ;
53- $ jobData[ ' branchType ' ] = $ this ->resolveBranchType ($ jobData )-> value ;
60+ $ jobData = $ this ->resolveBranchType ($ jobData );
5461
5562 // set type after resolving parallelism
5663 $ jobData ['type ' ] = $ this ->resolveJobType ($ jobData )->value ;
@@ -230,10 +237,7 @@ private function getConfiguration(): array
230237 $ this ->jobData ['configId ' ] !== null &&
231238 $ this ->jobData ['configId ' ] !== ''
232239 ) {
233- $ componentsApi = $ this ->getComponentsApiClient (
234- !empty ($ this ->jobData ['branchId ' ]) ? (string ) $ this ->jobData ['branchId ' ] : null ,
235- );
236- $ this ->configuration = $ componentsApi ->getConfiguration (
240+ $ this ->configuration = $ this ->componentsApiClient ->getConfiguration (
237241 $ this ->jobData ['componentId ' ],
238242 $ this ->jobData ['configId ' ],
239243 );
@@ -257,17 +261,6 @@ private function getConfiguration(): array
257261 return $ this ->configuration ;
258262 }
259263
260- private function getComponentsApiClient (?string $ branchId ): Components
261- {
262- return new Components (
263- $ this ->storageClientFactory ->createClientWrapper (new ClientOptions (
264- null ,
265- $ this ->jobData ['#tokenString ' ],
266- $ branchId ,
267- ))->getBranchClient (),
268- );
269- }
270-
271264 private function resolveIsForceRunMode (): bool
272265 {
273266 return isset ($ this ->jobData ['mode ' ]) && $ this ->jobData ['mode ' ] === JobInterface::MODE_FORCE_RUN ;
@@ -304,21 +297,13 @@ private function resolveExecutor(array $jobData): Executor
304297 return Executor::from ($ value );
305298 }
306299
307- private function getBranchesApiClient ( ): DevBranches
300+ public function resolveBranchType ( array $ jobData ): array
308301 {
309- return new DevBranches (
310- $ this ->storageClientFactory ->createClientWrapper (new ClientOptions (
311- token: $ this ->jobData ['#tokenString ' ],
312- ))->getBasicClient (),
313- );
314- }
302+ $ branchType = $ this ->clientWrapper ->isDefaultBranch () ? BranchType::DEFAULT : BranchType::DEV ;
315303
316- public function resolveBranchType (array $ jobData ): BranchType
317- {
318- if ($ jobData ['branchId ' ] === 'default ' || $ jobData ['branchId ' ] === null ) {
319- return BranchType::DEFAULT ;
320- }
321- $ branch = $ this ->getBranchesApiClient ()->getBranch ((int ) $ jobData ['branchId ' ]);
322- return $ branch ['isDefault ' ] ? BranchType::DEFAULT : BranchType::DEV ;
304+ $ jobData ['branchType ' ] = $ branchType ->value ;
305+ $ jobData ['branchId ' ] = $ this ->clientWrapper ->getBranchId ();
306+
307+ return $ jobData ;
323308 }
324309}
0 commit comments