| Conditions | 4 | 
| Paths | 8 | 
| Total Lines | 103 | 
| Code Lines | 64 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 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 | ||
| 47 | public function sendCode(array $state): ResponseInterface | ||
| 48 |     { | ||
| 49 | Assert::keyExists($state, 'cmdotcom:productToken', 'Missing required REST API key for the cm.com service.'); | ||
| 50 | Assert::keyExists($state, 'cmdotcom:recipient'); | ||
| 51 | Assert::keyExists($state, 'cmdotcom:originator'); | ||
| 52 | Assert::keyExists($state, 'cmdotcom:codeLength'); | ||
| 53 | Assert::keyExists($state, 'cmdotcom:validFor'); | ||
| 54 | Assert::keyExists($state, 'cmdotcom:allowPush'); | ||
| 55 | |||
| 56 | // Validate product token | ||
| 57 | $productToken = $state['cmdotcom:productToken']; | ||
| 58 | Assert::notNull( | ||
| 59 | $productToken, | ||
| 60 | 'Missing required REST API key for the cm.com service.', | ||
| 61 | ); | ||
| 62 | Assert::uuid($productToken); | ||
| 63 | |||
| 64 | // Validate appKey | ||
| 65 | $allowPush = $state['cmdotcom:allowPush']; | ||
| 66 | $appKey = null; | ||
| 67 |         if ($allowPush === true) { | ||
| 68 | $appKey = $state['cmdotcom:appKey']; | ||
| 69 | Assert::notNull( | ||
| 70 | $appKey, | ||
| 71 | 'Missing required appKey for use with push notification.', | ||
| 72 | ); | ||
| 73 | Assert::uuid($appKey); | ||
| 74 | } | ||
| 75 | |||
| 76 | // Validate originator | ||
| 77 | $originator = $state['cmdotcom:originator']; | ||
| 78 |         if (preg_match('/[0-9]+/', $originator)) { | ||
| 79 | Assert::maxLength( | ||
| 80 | $originator, | ||
| 81 | 16, | ||
| 82 | 'A numeric originator must represent a phonenumber and can contain a maximum of 16 digits.', | ||
| 83 | ); | ||
| 84 |         } else { | ||
| 85 | // TODO: figure out what characters are allowed and write a regex. | ||
| 86 | // So far 'A-Z', 'a-z', '0-9', ' ' and '-' are known to be accepted | ||
| 87 |             //Assert::alnum(str_replace(' ', '', $originator)); | ||
| 88 | Assert::lengthBetween( | ||
| 89 | $originator, | ||
| 90 | 3, | ||
| 91 | 11, | ||
| 92 | 'An alphanumeric originator can contain a minimum of 2 and a maximum of 11 characters.', | ||
| 93 | ); | ||
| 94 | } | ||
| 95 | |||
| 96 | // Validate OTP length | ||
| 97 | $codeLength = $state['cmdotcom:codeLength']; | ||
| 98 | Assert::range($codeLength, 4, 10); | ||
| 99 | |||
| 100 | // Validate recipient | ||
| 101 | $recipient = $state['cmdotcom:recipient']; | ||
| 102 | Assert::numeric($recipient); | ||
| 103 | Assert::maxLength( | ||
| 104 | $recipient, | ||
| 105 | 16, | ||
| 106 | 'A recipient must represent a phonenumber and can contain a maximum of 16 digits.', | ||
| 107 | ); | ||
| 108 | |||
| 109 | // Validate validFor | ||
| 110 | $validFor = $state['cmdotcom:validFor']; | ||
| 111 | Assert::range($validFor, 10, 3600); | ||
| 112 | |||
| 113 | // Translate text-message | ||
| 114 | // Initializating a Localization is a dirty hack to make translateSingularGettext work. | ||
| 115 | new Template($this->config, 'cmdotcom:message.twig'); | ||
| 116 | $message = Translate::translateSingularGettext( | ||
| 117 |             '{code} | ||
| 118 | Enter this verification code when asked during the authentication process.', | ||
| 119 | ); | ||
| 120 | |||
| 121 | $options = [ | ||
| 122 | 'base_uri' => self::API_BASE, | ||
| 123 | //'debug' => true, | ||
| 124 | 'headers' => [ | ||
| 125 | 'Content-Type' => 'application/json', | ||
| 126 | self::HEADER => $productToken, | ||
| 127 | ], | ||
| 128 | 'http_errors' => false, | ||
| 129 | 'timeout' => 3.0, | ||
| 130 | ]; | ||
| 131 | |||
| 132 | $client = new GuzzleClient($options); | ||
| 133 | $json = [ | ||
| 134 | 'recipient' => $recipient, | ||
| 135 | 'sender' => $originator, | ||
| 136 | 'length' => $codeLength, | ||
| 137 | 'expiry' => $validFor, | ||
| 138 | 'message' => $message, | ||
| 139 | ]; | ||
| 140 | |||
| 141 |         if ($allowPush === true) { | ||
| 142 | $json += ['allowPush' => $allowPush, 'appKey' => $appKey]; | ||
| 143 | } | ||
| 144 | |||
| 145 | return $client->request( | ||
| 146 | 'POST', | ||
| 147 | '/v1.0/otp/generate', | ||
| 148 | [ | ||
| 149 | 'json' => $json, | ||
| 150 | ], | ||
| 210 |