Cześć! Chciałbym przebudować daną metodę na bardziej czytelniejszą, lecz nie wiem jak się za to zabrać. Czy mógłby ktoś mi pomóc w Refactorze?
class InventoryService
{
public function createAction($data = [])
{
$isValid = \GUMP::is_valid($data, [
'placeId' => 'required'
]);
if($isValid === true) {
$inventoryEntity = new Inventory();
$inventoryEntity->setPlaceId($data['placeId']);
$result = $this->entityManager->getRepository('AppBundle:Inventory')->save($inventoryEntity);
if(!$result) {
throw new InvalidArgumentException('Dodawanie wpisu nie powiodło się.');
}
$lastInsertedId = $result->getId();
foreach($data['values'] as $key => $value) {
if($this->columnDoesntExists($key)) {
throw new InventoryServiceException('Kolumna o podanym ID nie istnieje.');
}
$valueEntity = new Value();
$valueEntity->setValue($value);
$valueEntity->setColumnId($key);
$valueEntity->setInventoryId($lastInsertedId);
$this->entityManager->getRepository('AppBundle:Inventory')->save($valueEntity);
}
$licenseInsert = new Value();
$licenseInsert->setValue($data['licenses']);
$licenseInsert->setColumnId($data['licenseId']['column_id']);
$licenseInsert->setInventoryId($lastInsertedId);
$resultOfLicenseInsert = $this->entityManager->getRepository('AppBundle:Inventory')->save($licenseInsert);
if(!$resultOfLicenseInsert) {
throw new InventoryServiceException('Dodawanie wpisu nie powiodło się.');
}
return ['status' => 'success'];
} else {
throw new InventoryServiceException('Placówka nie może być pusta.');
}
}
}