<?php
declare(strict_types=1);
namespace App\Feature\Management\Model\Employee\Upsert;
use App\Feature\Management\Entity\ManagementEmployee;
use App\Feature\Management\Exception\ManagementEmployeeAlreadyExistsException;
use App\Feature\Management\Model\Employee\ManagementEmployeeRequest;
use App\Feature\Management\Repository\ManagementEmployeeRepository;
use App\Feature\Management\Repository\ManagementRepository;
use App\Feature\User\Repository\UserRepository;
class CreateManagementEmployeeUpsert extends AbstractManagementEmployeeUpsert
{
public function __construct(
private readonly ManagementEmployeeRepository $employeeRepository,
private readonly ManagementEmployeeUpsert $upsert,
private readonly ManagementRepository $managementRepository,
private readonly UserRepository $userRepository,
)
{
}
public function fill(ManagementEmployee $employee, ManagementEmployeeRequest $request): ManagementEmployee
{
$management = $this->managementRepository->getManagement($request->getManagementId());
$user = $this->userRepository->getUser($request->getUserId());
$employee->setManagement($management);
$employee->setUser($user);
$this->checkEmployeeIsExist($employee);
$this->upsert->isUserHired($employee, $request);
return $this->upsert->fill($employee, $request);
}
public function checkEmployeeIsExist(ManagementEmployee $employee): void
{
if ($this->employeeRepository->findByManagementAndUser($employee->getManagement(), $employee->getUser())) {
throw new ManagementEmployeeAlreadyExistsException();
}
}
}
<?php
declare(strict_types=1);
namespace App\Feature\Management\Model\Employee\Upsert;
use App\Feature\Management\Entity\ManagementEmployee;
use App\Feature\Management\Exception\ManagementEmployeeAlreadyExistsException;
use App\Feature\Management\Model\Employee\ManagementEmployeeRequest;
use App\Feature\Management\Repository\ManagementEmployeeRepository;
use App\Feature\Management\Repository\ManagementRepository;
use App\Feature\User\Repository\UserRepository;
class UpdateManagementEmployeeUpsert extends AbstractManagementEmployeeUpsert
{
public function __construct(
private readonly ManagementEmployeeRepository $employeeRepository,
private readonly ManagementEmployeeUpsert $upsert,
private readonly ManagementRepository $managementRepository,
private readonly UserRepository $userRepository,
)
{
}
public function fill(ManagementEmployee $employee, ManagementEmployeeRequest $request): ManagementEmployee
{
$management = $this->managementRepository->getManagement($request->getManagementId());
$user = $this->userRepository->getUser($request->getUserId());
$employee->setManagement($management);
if ($this->upsert->isUserNotChanged($employee, $request)) {
return $this->upsert->fill($employee, $request);
}
$employee->setUser($user);
$this->isEmployeeExist($employee, $request);
$this->upsert->isUserHired($employee, $request);
return $this->upsert->fill($employee, $request);
}
public function isEmployeeExist(ManagementEmployee $employee, ManagementEmployeeRequest $request): void
{
if ($this->employeeRepository->findByManagementAndUser($employee->getManagement(), $employee->getUser())
) {
throw new ManagementEmployeeAlreadyExistsException();
}
}
}
/**
* @param array|null $ownersId
* @return $this
*/
public function filterOwners(?array $ownersId): self
{
$criteria = Criteria::create()
->where(Criteria::expr()->in('id', $ownersId));
$filteredEntries = $this->owners->matching($criteria);
return $this->setOwners($filteredEntries);
}
/**
* @return $this
*/
public function clearOwners(): self
{
$this->getOwners()->clear();
return $this;
}
/**
* @param RoomOwner $owner
* @return $this
*/
public function addOwner(RoomOwner $owner): self
{
$roomOwnerExist = $this->getOwners()->exists(function ($key, RoomOwner $element) use ($owner) {
return ($element->getOwner()->getId() === $owner->getOwner()->getId());
});
if (!$roomOwnerExist) {
$this->owners[] = $owner;
$owner->setRoom($this);
}
return $this;
}
/**
* @param array|null $ownersId
* @param Room $room
* @return void
*/
public function addRoomOwners(?array $ownersId, Room $room): void
{
if ($ownersId) {
$room->filterOwners($ownersId);
foreach ($ownersId as $ownerId) {
$room->addOwner((new RoomOwner())
->setTitle('Test Owner')
->setOwner($this->ownerRepository->getById($ownerId)));
}
} else {
$room->clearOwners();
}
}
/config/services.yaml
services:
app_controller:
namespace: App\
resource: '../src/*/Controller'
tags: ['controller.service_arguments']
config/routes/annotations.yaml
domain_controllers:
resource: '../../src/*/Controller/'
type: annotation
public function fillData(Dislocation $dislocation): DislocationDownloadingData
{
$has = $dislocation->getVars();
if ($has) {
foreach ($has as $name => $value) {
$this->$name = $value;
}
}
return $this;
}
/**
* @ORM\Entity
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="type", type="string", length=3)
*/
/**
* @ORM\Entity
*/
class Dislocation
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @ORM\Column(type="float", nullable=true)
*/
protected $timeInRun;
public function getTimeInRun(): ?float
{
return $this->timeInRun;
}
}
class DislocationDownloadingData extends Dislocation
{
public function getTimeInRun(): ?float
{
return round($this->timeInRun, 0);
}
}
Реализовал через фабрику