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 |
||
38 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
39 | { |
||
40 | $io = new SymfonyStyle($input, $output); |
||
41 | $config = $this->configuration->overrideWith(without_nullish_values($input->getOptions()))->asArray(); |
||
42 | |||
43 | $target = $input->getOption('target') ?? self::TARGET_BOTH; |
||
44 | if (!\in_array($target, self::VALID_TARGETS, true)) { |
||
45 | $io->error(\sprintf('Invalid target `%s`.', $target)); |
||
|
|||
46 | |||
47 | return self::FAILURE; |
||
48 | } |
||
49 | |||
50 | $values = [ |
||
51 | 'publish' => $input->getOption('publish'), |
||
52 | 'publish_exclude' => $input->getOption('publish-exclude'), |
||
53 | 'subscribe' => $input->getOption('subscribe'), |
||
54 | 'subscribe_exclude' => $input->getOption('subscribe-exclude'), |
||
55 | ]; |
||
56 | |||
57 | $claim = []; |
||
58 | |||
59 | if (\in_array($target, [self::TARGET_PUBLISHERS, self::TARGET_BOTH], true)) { |
||
60 | $claim = [ |
||
61 | 'publish' => $values['publish'], |
||
62 | 'publish_exclude' => $values['publish_exclude'], |
||
63 | ]; |
||
64 | } |
||
65 | |||
66 | if (\in_array($target, [self::TARGET_SUBSCRIBERS, self::TARGET_BOTH], true)) { |
||
67 | $claim = \array_merge( |
||
68 | $claim, |
||
69 | [ |
||
70 | 'subscribe' => $values['subscribe'], |
||
71 | 'subscribe_exclude' => $values['subscribe_exclude'], |
||
72 | ] |
||
73 | ); |
||
74 | } |
||
75 | |||
76 | $claim = \array_filter($values, fn(array $claim) => [] !== $claim); |
||
77 | $containsPublishTopics = isset($claim['publish']) || isset($claim['publish_exclude']); |
||
78 | $containsSubscribeTopics = isset($claim['subscribe']) || isset($claim['subscribe_exclude']); |
||
79 | $builder = (new Builder())->withClaim('mercure', $claim); |
||
80 | |||
81 | if (null !== $input->getOption('ttl')) { |
||
82 | $builder = $builder->expiresAt(\time() + (int) $input->getOption('ttl')); |
||
83 | } |
||
84 | |||
85 | $defaultKey = $config[Configuration::JWT_KEY]; |
||
86 | $defaultAlgorithm = $config[Configuration::JWT_ALGORITHM]; |
||
87 | |||
88 | if (isset($config[Configuration::PUBLISHER_JWT_KEY])) { |
||
89 | $publisherKey = $config[Configuration::PUBLISHER_JWT_KEY]; |
||
90 | $publisherAlgorithm = $config[Configuration::PUBLISHER_JWT_ALGORITHM] ?? $config[Configuration::JWT_ALGORITHM]; |
||
91 | } |
||
92 | |||
93 | if (isset($config[Configuration::SUBSCRIBER_JWT_KEY])) { |
||
94 | $subscriberKey = $config[Configuration::SUBSCRIBER_JWT_KEY]; |
||
95 | $subscriberAlgorithm = $config[Configuration::SUBSCRIBER_JWT_ALGORITHM] ?? $config[Configuration::JWT_ALGORITHM]; |
||
96 | } |
||
97 | |||
98 | if (true === $containsPublishTopics && false === $containsSubscribeTopics) { |
||
99 | $target = self::TARGET_PUBLISHERS; |
||
100 | } elseif (false === $containsPublishTopics && true === $containsSubscribeTopics) { |
||
101 | $target = self::TARGET_SUBSCRIBERS; |
||
102 | } |
||
103 | |||
104 | switch ($target) { |
||
105 | case self::TARGET_PUBLISHERS: |
||
106 | $key = $publisherKey ?? $defaultKey; |
||
107 | $algorithm = $publisherAlgorithm ?? $defaultAlgorithm; |
||
108 | break; |
||
109 | case self::TARGET_SUBSCRIBERS: |
||
110 | $key = $subscriberKey ?? $defaultKey; |
||
111 | $algorithm = $subscriberAlgorithm ?? $defaultAlgorithm; |
||
112 | break; |
||
113 | case self::TARGET_BOTH: |
||
114 | default: |
||
115 | $key = $defaultKey; |
||
116 | $algorithm = $defaultAlgorithm; |
||
117 | } |
||
118 | |||
119 | try { |
||
120 | $token = $builder->getToken( |
||
121 | get_signer($algorithm), |
||
122 | new Key($key), |
||
123 | ); |
||
124 | } catch (\Exception $e) { |
||
125 | $io->error('Unable to sign your token.'); |
||
126 | |||
127 | return self::FAILURE; |
||
128 | } |
||
129 | |||
130 | if (false === $input->getOption('raw')) { |
||
131 | $io->success('Here is your token! ⤵️'); |
||
132 | } |
||
133 | $output->writeln((string) $token); |
||
134 | |||
135 | return self::SUCCESS; |
||
136 | } |
||
321 |