| Conditions | 8 |
| Paths | 1 |
| Total Lines | 82 |
| Code Lines | 51 |
| Lines | 12 |
| Ratio | 14.63 % |
| 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 |
||
| 53 | public function init(Service $service) |
||
| 54 | { |
||
| 55 | $service->post( |
||
| 56 | '/connect', |
||
| 57 | function (Request $request, array $hookData) { |
||
| 58 | Utils::requireUser($hookData, ['vpn-server-node']); |
||
| 59 | |||
| 60 | $profileId = $request->getPostParameter('profile_id'); |
||
| 61 | InputValidation::profileId($profileId); |
||
| 62 | $commonName = $request->getPostParameter('common_name'); |
||
| 63 | InputValidation::commonName($commonName); |
||
| 64 | $ip4 = $request->getPostParameter('ip4'); |
||
| 65 | InputValidation::ip4($ip4); |
||
| 66 | $ip6 = $request->getPostParameter('ip6'); |
||
| 67 | InputValidation::ip6($ip6); |
||
| 68 | $connectedAt = $request->getPostParameter('connected_at'); |
||
| 69 | InputValidation::connectedAt($connectedAt); |
||
| 70 | |||
| 71 | $userId = self::getUserId($commonName); |
||
| 72 | |||
| 73 | // check if user is disabled |
||
| 74 | View Code Duplication | if (true === $this->users->isDisabled($userId)) { |
|
|
|
|||
| 75 | return new ApiResponse('connect', ['ok' => false, 'error' => sprintf('user "%s" disabled', $userId)]); |
||
| 76 | } |
||
| 77 | |||
| 78 | // check if the common_name is disabled |
||
| 79 | View Code Duplication | if (true === $this->commonNames->isDisabled($commonName)) { |
|
| 80 | return new ApiResponse('connect', ['ok' => false, 'error' => sprintf('common_name "%s" disabled', $commonName)]); |
||
| 81 | } |
||
| 82 | |||
| 83 | // if the ACL is enabled, verify that the user is allowed to |
||
| 84 | // connect |
||
| 85 | $profileConfig = new ProfileConfig($this->config->v('vpnProfiles', $profileId)); |
||
| 86 | if ($profileConfig->v('enableAcl')) { |
||
| 87 | $userGroups = []; |
||
| 88 | foreach ($this->groupProviders as $groupProvider) { |
||
| 89 | $userGroups = array_merge($userGroups, $groupProvider->getGroups($userId)); |
||
| 90 | } |
||
| 91 | |||
| 92 | if (false === self::isMember($userGroups, $profileConfig->v('aclGroupList'))) { |
||
| 93 | return new ApiResponse('connect', ['ok' => false, 'error' => sprintf('user "%s" not in ACL', $userId)]); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | View Code Duplication | if (false == $this->connectionLog->connect($profileId, $commonName, $ip4, $ip6, $connectedAt)) { |
|
| 98 | return new ApiResponse('connect', ['ok' => false, 'error' => 'unable to write connect event to log, dropping client']); |
||
| 99 | } |
||
| 100 | |||
| 101 | return new ApiResponse('connect', ['ok' => true]); |
||
| 102 | } |
||
| 103 | ); |
||
| 104 | |||
| 105 | $service->post( |
||
| 106 | '/disconnect', |
||
| 107 | function (Request $request, array $hookData) { |
||
| 108 | Utils::requireUser($hookData, ['vpn-server-node']); |
||
| 109 | |||
| 110 | $profileId = $request->getPostParameter('profile_id'); |
||
| 111 | InputValidation::profileId($profileId); |
||
| 112 | $commonName = $request->getPostParameter('common_name'); |
||
| 113 | InputValidation::commonName($commonName); |
||
| 114 | $ip4 = $request->getPostParameter('ip4'); |
||
| 115 | InputValidation::ip4($ip4); |
||
| 116 | $ip6 = $request->getPostParameter('ip6'); |
||
| 117 | InputValidation::ip6($ip6); |
||
| 118 | $connectedAt = $request->getPostParameter('connected_at'); |
||
| 119 | InputValidation::connectedAt($connectedAt); |
||
| 120 | |||
| 121 | $disconnectedAt = $request->getPostParameter('disconnected_at'); |
||
| 122 | InputValidation::disconnectedAt($disconnectedAt); |
||
| 123 | |||
| 124 | $bytesTransferred = $request->getPostParameter('bytes_transferred'); |
||
| 125 | InputValidation::bytesTransferred($bytesTransferred); |
||
| 126 | |||
| 127 | View Code Duplication | if (false === $this->connectionLog->disconnect($profileId, $commonName, $ip4, $ip6, $connectedAt, $disconnectedAt, $bytesTransferred)) { |
|
| 128 | return new ApiResponse('disconnect', ['ok' => false, 'error' => 'unable to write disconnect event to log, nothing we can do']); |
||
| 129 | } |
||
| 130 | |||
| 131 | return new ApiResponse('disconnect', ['ok' => true]); |
||
| 132 | } |
||
| 133 | ); |
||
| 134 | } |
||
| 135 | |||
| 157 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.