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