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