| Conditions | 6 |
| Paths | 1 |
| Total Lines | 76 |
| Code Lines | 49 |
| 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 |
||
| 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 | $ip = $request->getPostParameter('ip'); |
||
| 65 | InputValidation::ip4($ip); |
||
| 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 | if (true === $this->users->isDisabled($userId)) { |
||
| 75 | return false; |
||
| 76 | } |
||
| 77 | |||
| 78 | // check if the common_name is disabled |
||
| 79 | if (true === $this->commonNames->isDisabled($commonName)) { |
||
| 80 | return false; |
||
| 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 false; |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | $responseData = $this->connectionLog->connect($profileId, $commonName, $ip, $ip6, $connectedAt); |
||
| 98 | |||
| 99 | return new ApiResponse('connect', $responseData); |
||
| 100 | } |
||
| 101 | ); |
||
| 102 | |||
| 103 | $service->post( |
||
| 104 | '/disconnect', |
||
| 105 | function (Request $request, array $hookData) { |
||
| 106 | Utils::requireUser($hookData, ['vpn-server-node']); |
||
| 107 | |||
| 108 | $profileId = $request->getPostParameter('profile_id'); |
||
| 109 | InputValidation::profileId($profileId); |
||
| 110 | $commonName = $request->getPostParameter('common_name'); |
||
| 111 | InputValidation::commonName($commonName); |
||
| 112 | $ip = $request->getPostParameter('ip'); |
||
| 113 | InputValidation::ip4($ip); |
||
| 114 | $ip6 = $request->getPostParameter('ip6'); |
||
| 115 | InputValidation::ip6($ip6); |
||
| 116 | $connectedAt = $request->getPostParameter('connected_at'); |
||
| 117 | InputValidation::connectedAt($connectedAt); |
||
| 118 | $disconnectedAt = $request->getPostParameter('disconnected_at'); |
||
| 119 | InputValidation::disconnectedAt($disconnectedAt); |
||
| 120 | $bytesTransferred = $request->getPostParameter('bytes_transferred'); |
||
| 121 | InputValidation::bytesTransferred($bytesTransferred); |
||
| 122 | |||
| 123 | $responseData = $this->connectionLog->disconnect($profileId, $commonName, $ip, $ip6, $connectedAt, $disconnectedAt, $bytesTransferred); |
||
| 124 | |||
| 125 | return new ApiResponse('disconnect', $responseData); |
||
| 126 | } |
||
| 127 | ); |
||
| 128 | } |
||
| 129 | |||
| 151 |