| Conditions | 18 |
| Paths | 4609 |
| Total Lines | 98 |
| Code Lines | 63 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 29 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
| 30 | { |
||
| 31 | $io = new SymfonyStyle($input, $output); |
||
| 32 | $config = Configuration::bootstrapFromCLI($input)->asArray(); |
||
| 33 | |||
| 34 | $target = $input->getOption('target') ?? self::TARGET_BOTH; |
||
| 35 | if (!\in_array($target, self::VALID_TARGETS, true)) { |
||
| 36 | $io->error(\sprintf('Invalid target `%s`.', $target)); |
||
|
|
|||
| 37 | |||
| 38 | return self::FAILURE; |
||
| 39 | } |
||
| 40 | |||
| 41 | $values = [ |
||
| 42 | 'publish' => $input->getOption('publish'), |
||
| 43 | 'publish_exclude' => $input->getOption('publish-exclude'), |
||
| 44 | 'subscribe' => $input->getOption('subscribe'), |
||
| 45 | 'subscribe_exclude' => $input->getOption('subscribe-exclude'), |
||
| 46 | ]; |
||
| 47 | |||
| 48 | $claim = []; |
||
| 49 | |||
| 50 | if (\in_array($target, [self::TARGET_PUBLISHERS, self::TARGET_BOTH], true)) { |
||
| 51 | $claim = [ |
||
| 52 | 'publish' => $values['publish'], |
||
| 53 | 'publish_exclude' => $values['publish_exclude'], |
||
| 54 | ]; |
||
| 55 | } |
||
| 56 | |||
| 57 | if (\in_array($target, [self::TARGET_SUBSCRIBERS, self::TARGET_BOTH], true)) { |
||
| 58 | $claim = \array_merge( |
||
| 59 | $claim, |
||
| 60 | [ |
||
| 61 | 'subscribe' => $values['subscribe'], |
||
| 62 | 'subscribe_exclude' => $values['subscribe_exclude'], |
||
| 63 | ] |
||
| 64 | ); |
||
| 65 | } |
||
| 66 | |||
| 67 | $claim = \array_filter($values, fn(array $claim) => [] !== $claim); |
||
| 68 | $containsPublishTopics = isset($claim['publish']) || isset($claim['publish_exclude']); |
||
| 69 | $containsSubscribeTopics = isset($claim['subscribe']) || isset($claim['subscribe_exclude']); |
||
| 70 | $builder = (new Builder())->withClaim('mercure', $claim); |
||
| 71 | |||
| 72 | if (null !== $input->getOption('ttl')) { |
||
| 73 | $builder = $builder->expiresAt(\time() + (int) $input->getOption('ttl')); |
||
| 74 | } |
||
| 75 | |||
| 76 | $defaultKey = $config[Configuration::JWT_KEY]; |
||
| 77 | $defaultAlgorithm = $config[Configuration::JWT_ALGORITHM]; |
||
| 78 | |||
| 79 | if (isset($config[Configuration::PUBLISHER_JWT_KEY])) { |
||
| 80 | $publisherKey = $config[Configuration::PUBLISHER_JWT_KEY]; |
||
| 81 | $publisherAlgorithm = $config[Configuration::PUBLISHER_JWT_ALGORITHM] ?? $config[Configuration::JWT_ALGORITHM]; |
||
| 82 | } |
||
| 83 | |||
| 84 | if (isset($config[Configuration::SUBSCRIBER_JWT_KEY])) { |
||
| 85 | $subscriberKey = $config[Configuration::SUBSCRIBER_JWT_KEY]; |
||
| 86 | $subscriberAlgorithm = $config[Configuration::SUBSCRIBER_JWT_ALGORITHM] ?? $config[Configuration::JWT_ALGORITHM]; |
||
| 87 | } |
||
| 88 | |||
| 89 | if (true === $containsPublishTopics && false === $containsSubscribeTopics) { |
||
| 90 | $target = self::TARGET_PUBLISHERS; |
||
| 91 | } elseif (false === $containsPublishTopics && true === $containsSubscribeTopics) { |
||
| 92 | $target = self::TARGET_SUBSCRIBERS; |
||
| 93 | } |
||
| 94 | |||
| 95 | switch ($target) { |
||
| 96 | case self::TARGET_PUBLISHERS: |
||
| 97 | $key = $publisherKey ?? $defaultKey; |
||
| 98 | $algorithm = $publisherAlgorithm ?? $defaultAlgorithm; |
||
| 99 | break; |
||
| 100 | case self::TARGET_SUBSCRIBERS: |
||
| 101 | $key = $subscriberKey ?? $defaultKey; |
||
| 102 | $algorithm = $subscriberAlgorithm ?? $defaultAlgorithm; |
||
| 103 | break; |
||
| 104 | case self::TARGET_BOTH: |
||
| 105 | default: |
||
| 106 | $key = $defaultKey; |
||
| 107 | $algorithm = $defaultAlgorithm; |
||
| 108 | } |
||
| 109 | |||
| 110 | try { |
||
| 111 | $token = $builder->getToken( |
||
| 112 | get_signer($algorithm), |
||
| 113 | new Key($key), |
||
| 114 | ); |
||
| 115 | } catch (\Exception $e) { |
||
| 116 | $io->error('Unable to sign your token.'); |
||
| 117 | |||
| 118 | return self::FAILURE; |
||
| 119 | } |
||
| 120 | |||
| 121 | if (false === $input->getOption('raw')) { |
||
| 122 | $io->success('Here is your token! ⤵️'); |
||
| 123 | } |
||
| 124 | $output->writeln((string) $token); |
||
| 125 | |||
| 126 | return self::SUCCESS; |
||
| 127 | } |
||
| 312 |