| Conditions | 41 |
| Paths | > 20000 |
| Total Lines | 223 |
| Code Lines | 147 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 51 | public function saveAction() |
||
| 52 | { |
||
| 53 | $services = $this->serviceLocator; |
||
| 54 | |||
| 55 | /* @var \Zend\Http\PhpEnvironment\Request $request */ |
||
| 56 | $request = $this->getRequest(); |
||
| 57 | |||
| 58 | $params = $this->params(); |
||
| 59 | $p = $params->fromPost(); |
||
| 60 | $user = $services->get('AuthenticationService')->getUser(); |
||
| 61 | $repositories = $services->get('repositories'); |
||
| 62 | /* @var \Jobs\Repository\Job $repositoriesJob */ |
||
| 63 | $repositoriesJob = $repositories->get('Jobs/Job'); |
||
| 64 | $log = $services->get('Core/Log'); |
||
| 65 | |||
| 66 | $result = array('token' => session_id(), 'isSaved' => false, 'message' => '', 'portals' => array()); |
||
| 67 | try { |
||
| 68 | if (isset($user) && !empty($user->login)) { |
||
| 69 | $formElementManager = $services->get('FormElementManager'); |
||
| 70 | /* @var \Jobs\Form\Import $form */ |
||
| 71 | $form = $formElementManager->get('Jobs/Import'); |
||
| 72 | $id = $params->fromPost('id'); // determine Job from Database |
||
| 73 | /* @var \Jobs\Entity\Job $entity */ |
||
| 74 | $entity = null; |
||
| 75 | $createdJob = true; |
||
| 76 | |||
| 77 | if (empty($id)) { |
||
| 78 | $applyId = $params->fromPost('applyId'); |
||
| 79 | if (empty($applyId)) { |
||
| 80 | $entity = $repositoriesJob->create(); |
||
| 81 | } else { |
||
| 82 | $entity = $repositoriesJob->findOneBy(array("applyId" => (string) $applyId)); |
||
| 83 | if (!isset($entity)) { |
||
| 84 | // new Job (the more likely branch) |
||
| 85 | $entity =$repositoriesJob->create(array("applyId" => (string) $applyId)); |
||
| 86 | } else { |
||
| 87 | $createdJob = false; |
||
| 88 | } |
||
| 89 | } |
||
| 90 | } else { |
||
| 91 | $repositoriesJob->find($id); |
||
| 92 | $createdJob = false; |
||
| 93 | } |
||
| 94 | //$services->get('repositories')->get('Jobs/Job')->store($entity); |
||
| 95 | $form->bind($entity); |
||
| 96 | if ($request->isPost()) { |
||
| 97 | $loginSuffix = ''; |
||
| 98 | $event = $this->getEvent(); |
||
| 99 | $loginSuffixResponseCollection = $this->getEventManager()->trigger('login.getSuffix', $event); |
||
| 100 | if (!$loginSuffixResponseCollection->isEmpty()) { |
||
| 101 | $loginSuffix = $loginSuffixResponseCollection->last(); |
||
| 102 | } |
||
| 103 | $params = $request->getPost(); |
||
| 104 | $params->datePublishStart = \Datetime::createFromFormat("Y-m-d", $params->datePublishStart); |
||
| 105 | $result['post'] = $_POST; |
||
| 106 | $form->setData($params); |
||
| 107 | if ($form->isValid()) { |
||
| 108 | |||
| 109 | if (isset($params['templateValues']['description-description'])) { |
||
| 110 | $templateValues = new TemplateValues(); |
||
| 111 | $entity->setTemplateValues($templateValues->setDescription(strip_tags($params['templateValues']['description-description']))); |
||
| 112 | } |
||
| 113 | |||
| 114 | $entity->setStatus($params['status']); |
||
| 115 | /* |
||
| 116 | * Search responsible user via contactEmail |
||
| 117 | */ |
||
| 118 | $users = $repositories->get('Auth/User'); |
||
| 119 | $responsibleUser = $users->findByEmail($params['contactEmail']); |
||
| 120 | |||
| 121 | $entity->setUser($responsibleUser ?: $user); |
||
| 122 | |||
| 123 | $group = $user->getGroup($entity->getCompany()); |
||
| 124 | if ($group) { |
||
| 125 | $entity->getPermissions()->grant($group, PermissionsInterface::PERMISSION_VIEW); |
||
| 126 | } |
||
| 127 | $result['isSaved'] = true; |
||
| 128 | $log->info('Jobs/manage/saveJob [user: ' . $user->login . ']:' . var_export($p, true)); |
||
| 129 | |||
| 130 | if (!empty($params->companyId)) { |
||
| 131 | $companyId = $params->companyId . $loginSuffix; |
||
| 132 | $repOrganization = $repositories->get('Organizations/Organization'); |
||
| 133 | $hydratorManager = $services->get('hydratorManager'); |
||
| 134 | /* @var \Organizations\Entity\Hydrator\OrganizationHydrator $hydrator */ |
||
| 135 | $hydrator = $hydratorManager->get('Hydrator/Organization'); |
||
| 136 | $entityOrganizationFromDB = $repOrganization->findbyRef($companyId); |
||
| 137 | //$permissions = $entityOrganizationFromDB->getPermissions(); |
||
| 138 | $data = array( |
||
| 139 | 'externalId' => $companyId, |
||
| 140 | 'organizationName' => $params->company, |
||
| 141 | 'image' => $params->logoRef, |
||
| 142 | 'user' => $user |
||
| 143 | ); |
||
| 144 | //$permissions->grant($user, PermissionsInterface::PERMISSION_CHANGE); |
||
| 145 | |||
| 146 | $entityOrganization = $hydrator->hydrate($data, $entityOrganizationFromDB); |
||
| 147 | if ($responsibleUser && $user !== $responsibleUser) { |
||
| 148 | /* |
||
| 149 | * We cannot use custom collections yet |
||
| 150 | * @todo if we updated Mongo ODM to >1.0.5, we must move this to |
||
| 151 | * a custom collection class |
||
| 152 | */ |
||
| 153 | $employees = $entityOrganization->getEmployees(); |
||
| 154 | $contained = false; |
||
| 155 | /* |
||
| 156 | * this is o(n) and should propably be optimized when the custom collection is created. |
||
| 157 | * It's not very performant to load the whole user entity, when all we need is the ID. |
||
| 158 | * Maybe store the id as reference in the Employees Entity. |
||
| 159 | */ |
||
| 160 | foreach ($employees as $employee) { |
||
| 161 | if ($employee->getUser()->getId() == $responsibleUser->getId()) { |
||
| 162 | $contained = true; |
||
| 163 | break; |
||
| 164 | } |
||
| 165 | } |
||
| 166 | if (!$contained) { |
||
| 167 | $employees->add(new Employee($responsibleUser)); |
||
| 168 | } |
||
| 169 | } |
||
| 170 | $repositories->store($entityOrganization); |
||
| 171 | $entity->setOrganization($entityOrganization); |
||
| 172 | } else { |
||
| 173 | $result['message'] = ''; |
||
| 174 | } |
||
| 175 | |||
| 176 | if (!empty($params->locations)) { |
||
| 177 | $locations = \Zend\Json\Json::decode($params->locations, \Zend\Json\Json::TYPE_ARRAY); |
||
| 178 | $jobLocations = $entity->getLocations(); |
||
| 179 | $jobLocations->clear(); |
||
| 180 | foreach ($locations as $locData) { |
||
| 181 | $location = new Location(); |
||
| 182 | $coords = array_map(function ($i) { return (float) $i; }, $locData['coordinates']); |
||
| 183 | $location->setCountry($locData['country']) |
||
| 184 | ->setRegion($locData['region']) |
||
| 185 | ->setCity($locData['city']) |
||
| 186 | ->setCoordinates(new Point($coords)); |
||
| 187 | |||
| 188 | $jobLocations->add($location); |
||
| 189 | } |
||
| 190 | } |
||
| 191 | $repositoriesJob->store($entity); |
||
| 192 | $id = $entity->getId(); |
||
| 193 | if (!empty($id)) { |
||
| 194 | $jobEvent = $services->get('Jobs/Event'); |
||
| 195 | $jobEvent->setJobEntity($entity); |
||
| 196 | |||
| 197 | $extra = []; |
||
| 198 | foreach (array('channels', 'positions', 'branches', 'keywords', 'description') as $paramName) { |
||
| 199 | $data = $params->get($paramName); |
||
| 200 | if ($data) { |
||
| 201 | $data = Json::decode($data, Json::TYPE_ARRAY); |
||
| 202 | if ('channels' == $paramName) { |
||
| 203 | foreach (array_keys($data) as $portalName) { |
||
| 204 | $jobEvent->addPortal($portalName); |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | $extra[$paramName] = $data; |
||
| 209 | } |
||
| 210 | } |
||
| 211 | $jobEvent->setParam('extraData', $extra); |
||
| 212 | |||
| 213 | if ($createdJob || true) { |
||
| 214 | /* @var $jobEvents \Zend\EventManager\EventManager */ |
||
| 215 | $jobEvents = $services->get('Jobs/Events'); |
||
| 216 | $jobEvent->setName(JobEvent::EVENT_JOB_ACCEPTED) |
||
| 217 | ->setTarget($this); |
||
| 218 | $responses = $jobEvents->trigger($jobEvent); |
||
| 219 | foreach ($responses as $response) { |
||
| 220 | // responses from the portals |
||
| 221 | // @TODO, put this in some conclusion and meaningful messages |
||
| 222 | if (!empty($response)) { |
||
| 223 | if ($response instanceof JobResponse) { |
||
| 224 | if (!array_key_exists('log', $result)) { |
||
| 225 | $result['log'] = ''; |
||
| 226 | } |
||
| 227 | //$message = $response->getMessage(); |
||
| 228 | //$result['log'] .= $response->getMessage() . PHP_EOL; |
||
| 229 | $status = $response->getStatus(); |
||
| 230 | $portal = $response->getPortal(); |
||
| 231 | if (empty($portal)) { |
||
| 232 | throw new \RuntimeException('Publisher-Events (internal error): There is an unregistered publisher listening'); |
||
| 233 | } |
||
| 234 | switch ($status) { |
||
| 235 | case JobResponse::RESPONSE_FAIL: |
||
| 236 | case JobResponse::RESPONSE_NOTIMPLEMENTED: |
||
| 237 | case JobResponse::RESPONSE_ERROR: |
||
| 238 | $result['isSaved'] = false; |
||
| 239 | break; |
||
| 240 | case JobResponse::RESPONSE_DENIED: |
||
| 241 | case JobResponse::RESPONSE_OK: |
||
| 242 | case JobResponse::RESPONSE_OKANDSTOP: |
||
| 243 | case JobResponse::RESPONSE_DEPRECATED: |
||
| 244 | break; |
||
| 245 | } |
||
| 246 | if (array_key_exists($portal, $result['portals'])) { |
||
| 247 | throw new \RuntimeException('Publisher-Events (internal error): There are two publisher registered for ' . $portal); |
||
| 248 | } |
||
| 249 | $result['portals'][$portal] = $status; |
||
| 250 | } else { |
||
| 251 | throw new \RuntimeException('Publisher-Events (internal error): Response must be from the class Jobs\Listener\Response\JobResponse'); |
||
| 252 | } |
||
| 253 | } else { |
||
| 254 | throw new \RuntimeException('Publisher-Events (internal error): Response must be set'); |
||
| 255 | } |
||
| 256 | } |
||
| 257 | } |
||
| 258 | } |
||
| 259 | } else { |
||
| 260 | $log->info('Jobs/manage/saveJob [error: ' . $form->getMessages() . ']:' . var_export($p, true)); |
||
| 261 | $result['valid Error'] = $form->getMessages(); |
||
| 262 | } |
||
| 263 | } |
||
| 264 | } else { |
||
| 265 | $log->info('Jobs/manage/saveJob [error: session lost]:' . var_export($p, true)); |
||
| 266 | $result['message'] = 'session_id is lost'; |
||
| 267 | } |
||
| 268 | } catch (\Exception $e) { |
||
| 269 | $result['message'] = 'exception occured: ' . $e->getMessage(); |
||
| 270 | } |
||
| 271 | //$services->get('Core/Log')->info('Jobs/manage/saveJob result:' . PHP_EOL . var_export($p, True)); |
||
| 272 | return new JsonModel($result); |
||
| 273 | } |
||
| 274 | } |
||
| 275 |
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.