Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
msalakhov committed Apr 12, 2022
1 parent 1422c27 commit c32ebf6
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 185 deletions.
4 changes: 1 addition & 3 deletions app/src/Controller/Admin/UserCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@

class UserCrudController extends AbstractCrudController
{
public function __construct(private UserRepository $userRepository)
{
}
public function __construct(private UserRepository $userRepository) {}

public static function getEntityFqcn(): string
{
Expand Down
181 changes: 23 additions & 158 deletions app/src/Controller/ClientController.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php

declare(strict_types=1);

namespace App\Controller;

use App\Entity\Attachments;
use App\Form\InsuranceAttachmentsFormType;
use App\ImageOptimizer;
use App\Entity\Client;
use App\Entity\ClientInsurance;
use App\Form\AttachmentsFormType;
use App\Entity\InsuranceAttachments;
use App\Repository\ClientRepository;
use App\Form\CreateClientFormType;
use App\Form\CreateClientInsuranceFormType;
Expand All @@ -26,27 +26,10 @@
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

#[Route('/client')]
class ClientController extends AbstractController
{
#[Route('/', name: 'homepage')]
public function index(UserInterface $user, ClientRepository $clientRepository)
{
if (in_array('ADMIN', $user->getRoles())) {
$clients = $clientRepository->findAll();
return $this->render('admin/index.html.twig', [
'title' => 'Admin section',
'clients' => $clients,
]);
} else {
$clients = $clientRepository->findBy(['user' => $user->getId()]);
return $this->render('client/index.html.twig', [
'title' => 'Your clients',
'clients' => $clients,
]);
}
}

#[Route('/client/create')]
#[Route('/create')]
public function create(UserInterface $user, Request $request, ManagerRegistry $managerRegistry): Response
{
$client = new Client();
Expand All @@ -60,7 +43,7 @@ public function create(UserInterface $user, Request $request, ManagerRegistry $m

try {
$photo->move(
$this->getParameter('photoDirAbs'),
$this->getParameter('photoDirAbs'),
$fileName
);
} catch (FileException $e) {
Expand All @@ -72,6 +55,7 @@ public function create(UserInterface $user, Request $request, ManagerRegistry $m

$client->setPhoto($fileName);
}

$client->setUser($user);

$entityManager = $managerRegistry->getManager();
Expand All @@ -88,8 +72,8 @@ public function create(UserInterface $user, Request $request, ManagerRegistry $m
]);
}

#[Route('/client/delete/{id}', methods:['DELETE'])]
public function delete($id, UserInterface $user, ClientRepository $clientRepository, ManagerRegistry $managerRegistry)
#[Route('/delete/{id}', methods: ['DELETE'])]
public function delete(int $id, UserInterface $user, ClientRepository $clientRepository, ManagerRegistry $managerRegistry)
{
$client = $clientRepository->find($id);

Expand All @@ -107,8 +91,8 @@ public function delete($id, UserInterface $user, ClientRepository $clientReposit
$response->send();
}

#[Route('/client/edit/{id}')]
public function edit(Request $request, $id, UserInterface $user, ClientRepository $clientRepository, ManagerRegistry $managerRegistry): Response
#[Route('/edit/{id}')]
public function edit(Request $request, int $id, UserInterface $user, ClientRepository $clientRepository, ManagerRegistry $managerRegistry): Response
{
$client = $clientRepository->find($id);

Expand All @@ -122,7 +106,7 @@ public function edit(Request $request, $id, UserInterface $user, ClientRepositor
$photo = new File($this->getParameter('photoDirAbs') . '/' . $client->getPhoto());
$fileName = $photo->getFilename();

$client->setPhoto($photo);
$client->setPhoto((string)$photo);
}

$form = $this->createForm(CreateClientFormType::class, $client);
Expand All @@ -135,7 +119,7 @@ public function edit(Request $request, $id, UserInterface $user, ClientRepositor

try {
$photo->move(
$this->getParameter('photoDirAbs'),
$this->getParameter('photoDirAbs'),
$fileName
);
} catch (FileException $e) {
Expand All @@ -144,8 +128,6 @@ public function edit(Request $request, $id, UserInterface $user, ClientRepositor

$imageOptimazer = new ImageOptimizer();
$imageOptimazer->resize($this->getParameter('photoDirAbs') . '/' . $fileName);


}

$client->setPhoto($fileName);
Expand All @@ -163,8 +145,8 @@ public function edit(Request $request, $id, UserInterface $user, ClientRepositor
]);
}

#[Route('/client/{id}', name: 'insuranceList')]
public function insuranceObjects($id, UserInterface $user, ClientRepository $clientRepository, ClientInsuranceRepository $clientInsuranceRepository, InsuranceAttachmentsRepository $insuranceAttachmentsRepository, AttachmentsRepository $attachmentsRepository)
#[Route('/{id}', name: 'insuranceList')]
public function insuranceObjects(int $id, UserInterface $user, ClientRepository $clientRepository, ClientInsuranceRepository $clientInsuranceRepository, InsuranceAttachmentsRepository $insuranceAttachmentsRepository, AttachmentsRepository $attachmentsRepository)
{
$client = $clientRepository->find($id);

Expand Down Expand Up @@ -212,10 +194,11 @@ public function insuranceObjects($id, UserInterface $user, ClientRepository $cli
]);
}

#[Route('/client/{id}/add-insurance', name: 'add-insurance')]
public function addInsurance($id, ClientRepository $clientRepository, UserInterface $user, Request $request, ManagerRegistry $managerRegistry)
#[Route('/{id}/add-insurance', name: 'add-insurance')]
public function addInsurance(int $id, ClientRepository $clientRepository, UserInterface $user, Request $request, ManagerRegistry $managerRegistry)
{
$client = $clientRepository->find($id);

if ($client->getUser()->getId() != $user->getId()) {
if (!in_array('ADMIN', $user->getRoles())) {
throw new AccessDeniedException();
Expand Down Expand Up @@ -244,131 +227,12 @@ public function addInsurance($id, ClientRepository $clientRepository, UserInterf
]);
}

#[Route('/client/insurance/delete/{id}', name: 'delete-ins', methods:['DELETE'])]
public function deleteInsurance($id, ClientRepository $clientRepository, UserInterface $user, ClientInsuranceRepository $clientInsuranceRepository, ManagerRegistry $managerRegistry)
{
$clientInsurance = $clientInsuranceRepository->find($id);

$client = $clientRepository->find($clientInsurance->getClientId());
if ($client->getUser()->getId() != $user->getId()) {
if (!in_array('ADMIN', $user->getRoles())) {
throw new AccessDeniedException();
}
}

$entityManager = $managerRegistry->getManager();

$entityManager->remove($clientInsurance);
$entityManager->flush();

$response = new Response();
$response->send();
}

#[Route('/client/insurance/edit/{id}', name: 'edit-ins')]
public function editInsurance(UserInterface $user, Request $request, $id, ClientRepository $clientRepository, ClientInsuranceRepository $clientInsuranceRepository, ManagerRegistry $managerRegistry): Response
{
$insuranse = $clientInsuranceRepository->find($id);

$client = $clientRepository->find($insuranse->getClientId());
if ($client->getUser()->getId() != $user->getId()) {
if (!in_array('ADMIN', $user->getRoles())) {
throw new AccessDeniedException();
}
}

$form = $this->createForm(CreateClientInsuranceFormType::class, $insuranse);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $managerRegistry->getManager();
$entityManager->flush();

return $this->redirectToRoute('insuranceList', ['id' => $insuranse->getClientId()]);
}

return $this->render('client/editInsurance.html.twig', [
'controller_name' => 'ClientController',
'addInsuranceForm' => $form->createView(),
'clientId' => $insuranse->getClientId()
]);
}

#[Route('/client/{id}/insurance/{insId}/upload-file', name: 'insurance-upload-file')]
public function uploadIns(Request $request, $id, $insId, ClientRepository $clientRepository, UserInterface $user, ManagerRegistry $managerRegistry): Response
{
$client = $clientRepository->find($id);
if ($client->getUser()->getId() != $user->getId()) {
if (!in_array('ADMIN', $user->getRoles())) {
throw new AccessDeniedException();
}
}

$attachment = new InsuranceAttachments();
$form = $this->createForm(InsuranceAttachmentsFormType::class, $attachment);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
/** @var UploadedFile $file */
if ($file = $form->get('path')->getData()) {
$fileName = base64_encode($file->getClientOriginalName());
$encodedFileName = bin2hex(random_bytes(6)) . '.' . $file->guessExtension();

try {
$file->move(
$this->getParameter('photoDirAbs'),
$encodedFileName
);
} catch (FileException $e) {
//unable to upload
}

$attachment->setPath($encodedFileName);
$attachment->setInsuranceId($insId);
$attachment->setName($fileName);
}

$entityManager = $managerRegistry->getManager();
$entityManager->persist($attachment);
$entityManager->flush();

return $this->redirectToRoute('insuranceList', ['id' => $id]);
}

return $this->render('client/insurance-upload-file.html.twig', [
'controller_name' => 'ClientController',
'attachmentForm' => $form->createView(),
'title' => 'Attach insurance file'
]);
}

#[Route('/client/insurance/delete-attachment/{attachmentId}', name: 'delete-ins-attachment', methods:['DELETE'])]
public function deleteInsAttachment($attachmentId, ClientRepository $clientRepository, UserInterface $user, InsuranceAttachmentsRepository $insuranceAttachmentsRepository, ClientInsuranceRepository $clientInsuranceRepository, ManagerRegistry $managerRegistry)
{
$attachment = $insuranceAttachmentsRepository->find($attachmentId);
$insurance = $clientInsuranceRepository->find($attachment->getInsuranceId());

$client = $clientRepository->find($insurance->getClientId());
if ($client->getUser()->getId() != $user->getId()) {
if (!in_array('ADMIN', $user->getRoles())) {
throw new AccessDeniedException();
}
}

$entityManager = $managerRegistry->getManager();
$entityManager->remove($attachment);
$entityManager->flush();

$response = new Response();
$response->send();
}

#[Route('/client/delete-attachment/{id}', name: 'delete-attachment', methods:['DELETE'])]
public function deleteAttachment($id, ClientRepository $clientRepository, UserInterface $user, AttachmentsRepository $attachmentsRepository, ManagerRegistry $managerRegistry)
#[Route('/delete-attachment/{id}', name: 'delete-attachment', methods: ['DELETE'])]
public function deleteAttachment(int $id, ClientRepository $clientRepository, UserInterface $user, AttachmentsRepository $attachmentsRepository, ManagerRegistry $managerRegistry)
{
$attachment = $attachmentsRepository->find($id);

$client = $clientRepository->find($attachment->getUserId());

if ($client->getUser()->getId() != $user->getId()) {
if (!in_array('ADMIN', $user->getRoles())) {
throw new AccessDeniedException();
Expand All @@ -383,10 +247,11 @@ public function deleteAttachment($id, ClientRepository $clientRepository, UserIn
$response->send();
}

#[Route('/client/{id}/upload-file', name: 'upload-file')]
public function upload(Request $request, $id, ClientRepository $clientRepository, UserInterface $user, ManagerRegistry $managerRegistry): Response
#[Route('/{id}/upload-file', name: 'upload-file')]
public function upload(Request $request, int $id, ClientRepository $clientRepository, UserInterface $user, ManagerRegistry $managerRegistry): Response
{
$client = $clientRepository->find($id);

if ($client->getUser()->getId() != $user->getId()) {
if (!in_array('ADMIN', $user->getRoles())) {
throw new AccessDeniedException();
Expand Down
Loading

0 comments on commit c32ebf6

Please sign in to comment.