<?php
declare(strict_types=1);
namespace App\Feature\User\Service;
use App\Application\Model\Response\IdResponse;
use App\Feature\User\Entity\User;
use App\Feature\User\Model\UserRequest;
use App\Feature\User\Repository\UserRepository;
readonly class UserService
{
public function __construct(private UserRepository $repository)
{
}
public function createUser(UserRequest $request): IdResponse
{
$user = new User();
$this->upsertUser($user, $request);
return new IdResponse($user->getId());
}
public function updateUser(int $id, UserRequest $request): void
{
$this->upsertUser($this->repository->getById($id), $request);
}
public function upsertUser(User $user, UserRequest $request): void
{
$user->setName($request->getName());
$user->setActive($request->getActive());
// for update request only
if ($this->isUserNotChanged($user, $request)) {
// ... some logic
}
// for create request only
if ($this->isUserPhoneExists($user)) {
// ... some logic
}
$this->repository->saveAndCommit($user);
}
}
public function upsertUser(User $user, UserRequest $request, bool $isUpdateRequest = false): void
{
$user->setName($request->getName());
$user->setActive($request->getActive());
if ($isUpdateRequest) {
// for update request only
if ($this->isUserNotChanged($user, $request)) {
// ... some logic
}
} else {
// for create request only
if ($this->isUserPhoneExists($user)) {
// ... some logic
}
}
$this->repository->saveAndCommit($user);
}
public function upsertUserForCreateRequest(User $user, UserRequest $request): void
{
$user->setName($request->getName());
$user->setActive($request->getActive());
// for create request only
if ($this->isUserPhoneExists($user)) {
// ... some logic
}
$this->repository->saveAndCommit($user);
}
public function upsertUserForUpdateRequest(User $user, UserRequest $request): void
{
$user->setName($request->getName());
$user->setActive($request->getActive());
// for update request only
if ($this->isUserNotChanged($user, $request)) {
// ... some logic
}
$this->repository->saveAndCommit($user);
}
<?php
declare(strict_types=1);
namespace App\Feature\Management\Service;
use App\Application\Model\Response\IdResponse;
use App\Feature\Management\Entity\ManagementEmployee;
use App\Feature\Management\Mapper\ManagementEmployeeMapper;
use App\Feature\Management\Mapper\ManagementMapper;
use App\Feature\Management\Model\Employee\ManagementEmployeeDetails;
use App\Feature\Management\Model\Employee\ManagementEmployeeRequest;
use App\Feature\Management\Repository\ManagementEmployeeRepository;
use App\Feature\User\Mapper\UserMapper;
readonly class ManagementEmployeeService
{
public function __construct(
private ManagementEmployeeRepository $employeeRepository,
private ManagementEmployeeUpsertService $upsertService
)
{
}
public function getEmployee(int $id): ManagementEmployeeDetails
{
$employee = $this->employeeRepository->getById($id);
return ManagementEmployeeMapper::mapEmployeeDetails($employee)
->setUser(UserMapper::mapUser($employee->getUser()))
->setManagement(ManagementMapper::mapManagement($employee->getManagement()));
}
public function deleteEmployee(int $id): void
{
$employee = $this->employeeRepository->getById($id);
$this->employeeRepository->removeAndCommit($employee);
}
public function createEmployee(ManagementEmployeeRequest $request): IdResponse
{
$employee = $this->upsertService->createUpsert((new ManagementEmployee()), $request);
$this->employeeRepository->saveAndCommit($employee);
return new IdResponse($employee->getId());
}
public function updateEmployee(int $id, ManagementEmployeeRequest $request): void
{
$employee = $this->upsertService->updateUpsert($this->employeeRepository->getById($id), $request);
$this->employeeRepository->saveAndCommit($employee);
}
}
<?php
declare(strict_types=1);
namespace App\Feature\Management\Service;
use App\Feature\Management\Entity\ManagementEmployee;
use App\Feature\Management\Model\Employee\ManagementEmployeeRequest;
use App\Feature\Management\Model\Employee\Upsert\AbstractManagementEmployeeUpsert;
use App\Feature\Management\Model\Employee\Upsert\ManagementEmployeeUpsertManager;
readonly class ManagementEmployeeUpsertService
{
public function __construct(
private ManagementEmployeeUpsertManager $upsertManager)
{
}
public function createUpsert(ManagementEmployee $employee, ManagementEmployeeRequest $request): ManagementEmployee
{
return $this->upsert($this->upsertManager->getCreateUpsert(), $employee, $request);
}
public function updateUpsert(ManagementEmployee $employee, ManagementEmployeeRequest $request): ManagementEmployee
{
return $this->upsert($this->upsertManager->getUpdateUpsert(), $employee, $request);
}
public function upsert(AbstractManagementEmployeeUpsert $upsert, ManagementEmployee $employee, ManagementEmployeeRequest $request): ManagementEmployee
{
return $upsert->fill($employee, $request);
}
}
<?php
declare(strict_types=1);
namespace App\Feature\Management\Model\Employee\Upsert;
readonly class ManagementEmployeeUpsertManager
{
public function __construct(
private CreateManagementEmployeeUpsert $createUpsert,
private UpdateManagementEmployeeUpsert $updateUpsert
)
{
}
/**
* @return CreateManagementEmployeeUpsert
*/
public function getCreateUpsert(): CreateManagementEmployeeUpsert
{
return $this->createUpsert;
}
/**
* @return UpdateManagementEmployeeUpsert
*/
public function getUpdateUpsert(): UpdateManagementEmployeeUpsert
{
return $this->updateUpsert;
}
}
<?php
declare(strict_types=1);
namespace App\Feature\Management\Model\Employee\Upsert;
use App\Feature\Management\Entity\ManagementEmployee;
use App\Feature\Management\Model\Employee\ManagementEmployeeRequest;
use App\Feature\Management\Repository\ManagementEmployeeRepository;
use App\Feature\User\Exception\UserIsAlreadyHiredException;
readonly class ManagementEmployeeUpsert
{
public function __construct(private ManagementEmployeeRepository $employeeRepository)
{
}
public function isUserNotChanged(ManagementEmployee $employee, ManagementEmployeeRequest $request): bool
{
return ($employee->getUser() && $employee->getUser()->getId() === $request->getUserId());
}
public function isUserHired(ManagementEmployee $employee, ManagementEmployeeRequest $request): void
{
if ($this->employeeRepository->findByUser($employee->getUser())) {
throw new UserIsAlreadyHiredException();
}
}
public function fill(ManagementEmployee $employee, ManagementEmployeeRequest $request): ManagementEmployee
{
$employee->setFired($request->isFired());
if ($request->getPosition() !== null) {
$employee->setPosition($request->getPosition());
}
if ($request->getParentId() !== null) {
$employee->setParentId($request->getParentId());
}
if ($request->getDescription() !== null) {
$employee->setDescription($request->getDescription());
}
if ($request->getData() !== null) {
$employee->setData($request->getData());
}
return $employee;
}
}
<?php
declare(strict_types=1);
namespace App\Feature\Management\Model\Employee\Upsert;
use App\Feature\Management\Entity\ManagementEmployee;
use App\Feature\Management\Model\Employee\ManagementEmployeeRequest;
abstract class AbstractManagementEmployeeUpsert
{
abstract public function fill(ManagementEmployee $employee, ManagementEmployeeRequest $request): ManagementEmployee;
}