From 83475dc324c8106cd049abcbc5f38220f949cffb Mon Sep 17 00:00:00 2001 From: Fabiola BELET Date: Wed, 12 Aug 2020 10:52:02 +0200 Subject: [PATCH] Fixs --- features/admin_create.feature | 10 ++-- src/Command/UpdateBooksCommand.php | 20 +++++--- .../Admin/AdminAuthorController.php | 4 +- src/Form/AccountType.php | 6 +-- ...ooks.php => CanSaveBooksSpecification.php} | 20 +------- src/UseCases/CreateBookUseCase.php | 47 +++++++++++++++++++ 6 files changed, 71 insertions(+), 36 deletions(-) rename src/Specifications/{SaveBooks.php => CanSaveBooksSpecification.php} (60%) create mode 100644 src/UseCases/CreateBookUseCase.php diff --git a/features/admin_create.feature b/features/admin_create.feature index 0836381..1bac34b 100644 --- a/features/admin_create.feature +++ b/features/admin_create.feature @@ -28,10 +28,10 @@ Feature: Given I am on "/admin/users" And I should see "Users handler" When I press "Create" - And I fill "user_email" with "toto@gmail.com" - And I fill "user_firstName" with "Toto" - And I fill "user_lastName" with "Titi" - And I fill "user_password" with "titutoti" - And I fill "user_passwordConfirm" with "titutoti" + And I fill "account_email" with "toto@gmail.com" + And I fill "account_firstName" with "Toto" + And I fill "account_lastName" with "Titi" + And I fill "account_password" with "titutoti" + And I fill "account_passwordConfirm" with "titutoti" And I press "submit" Then I should be on "/admin/users" diff --git a/src/Command/UpdateBooksCommand.php b/src/Command/UpdateBooksCommand.php index 8280372..ad62f8f 100644 --- a/src/Command/UpdateBooksCommand.php +++ b/src/Command/UpdateBooksCommand.php @@ -6,9 +6,9 @@ use App\Entity\Author; use App\Events\Events; -use App\Service\Books\SaveBook; use App\Service\BookUploader\BookUploaderInterface; -use App\Specifications\SaveBooks; +use App\Specifications\CanSaveBooksSpecification; +use App\UseCases\CreateBookUseCase; use DateTime; use Doctrine\ORM\EntityManagerInterface; use Psr\Log\LoggerInterface; @@ -25,7 +25,7 @@ class UpdateBooksCommand extends Command protected static $defaultName = 'app:update-books'; private $em; - private $saveBook; + private $canSaveBooksSpecification; /** * @var BookUploaderInterface */ @@ -38,15 +38,20 @@ class UpdateBooksCommand extends Command * @var LoggerInterface */ private $logger; + /** + * @var CreateBookUseCase + */ + private $createBookUseCase; - public function __construct(BookUploaderInterface $bookUploader, EntityManagerInterface $em, SaveBooks $saveBook,EventDispatcherInterface $eventDispatcher, LoggerInterface $logger) + public function __construct(BookUploaderInterface $bookUploader, EntityManagerInterface $em, CanSaveBooksSpecification $canSaveBooksSpecification,EventDispatcherInterface $eventDispatcher, LoggerInterface $logger, CreateBookUseCase $createBookUseCase) { $this->bookUploader = $bookUploader; $this->em = $em; - $this->saveBook = $saveBook; + $this->canSaveBooksSpecification = $canSaveBooksSpecification; parent::__construct(); $this->eventDispatcher = $eventDispatcher; $this->logger = $logger; + $this->createBookUseCase = $createBookUseCase; } protected function configure() @@ -71,10 +76,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int $output->writeln(count($books).' books (not saved)'); // 3/ Save books in database -// $savedBooks = $this->saveBook->save($books); foreach ($books as $bookToSave) { - $this->saveBook->isSatisfiedBy($bookToSave); + if ($this->canSaveBooksSpecification->isSatisfiedBy($bookToSave)) { + $this->createBookUseCase->create($bookToSave); + } } // 4/ Send user subscribers diff --git a/src/Controller/Admin/AdminAuthorController.php b/src/Controller/Admin/AdminAuthorController.php index 1abdfff..5c6a522 100644 --- a/src/Controller/Admin/AdminAuthorController.php +++ b/src/Controller/Admin/AdminAuthorController.php @@ -41,7 +41,7 @@ public function index(Request $request, PaginatorInterface $paginator): Response } /** - * @Route("/admin/author/create", name="admin_create_author") + * @Route("/admin/author/create", name="admin_create_author", methods={"GET","POST"}) */ public function create(Request $request): Response { @@ -64,7 +64,7 @@ public function create(Request $request): Response } /** - * @Route("/admin/author/{id}/edit", name="admin_edit_author") + * @Route("/admin/author/{id}/edit", name="admin_edit_author", methods={"GET","PUT"}) */ public function edit(Author $author, Request $request): Response { diff --git a/src/Form/AccountType.php b/src/Form/AccountType.php index 7e4671e..5b75aa8 100644 --- a/src/Form/AccountType.php +++ b/src/Form/AccountType.php @@ -22,7 +22,7 @@ public function buildForm(FormBuilderInterface $builder, array $options) ->add('password', PasswordType::class) ->add('passwordConfirm', PasswordType::class) ; - if ($options['validation_groups'][0] == 'admin_edit') + if (in_array('admin_edit', $options['validation_groups'] )) { $builder ->remove('email') @@ -30,13 +30,13 @@ public function buildForm(FormBuilderInterface $builder, array $options) ->remove('password') ->remove('passwordConfirm'); } - if ($options['validation_groups'][0] == 'user_edit') + if (in_array('user_edit', $options['validation_groups'] )) { $builder ->remove('password') ->remove('passwordConfirm'); } - if ($options['validation_groups'][0] == 'registration') + if (in_array('registration', $options['validation_groups'] )) { $builder ->remove('subscriptions'); diff --git a/src/Specifications/SaveBooks.php b/src/Specifications/CanSaveBooksSpecification.php similarity index 60% rename from src/Specifications/SaveBooks.php rename to src/Specifications/CanSaveBooksSpecification.php index 2a43141..e3c883b 100644 --- a/src/Specifications/SaveBooks.php +++ b/src/Specifications/CanSaveBooksSpecification.php @@ -6,10 +6,9 @@ use App\Entity\Author; use App\Entity\Book; -use DateTime; use Doctrine\ORM\EntityManagerInterface; -class SaveBooks implements SpecificationInterface +class CanSaveBooksSpecification implements SpecificationInterface { private $em; @@ -40,24 +39,7 @@ public function isSatisfiedBy($bookToSave): bool return false; } - $this->createEntity($bookToSave, $book); - return true; } - private function createEntity(array $bookToSave, Book $book) - { - $book - ->setVolumeId($bookToSave["id"]) - ->setTitle($bookToSave["volumeInfo"]["title"]) - ->setDescription($bookToSave["volumeInfo"]["description"]) - ->setImage($bookToSave["volumeInfo"]["imageLinks"]["thumbnail"]); - - if (key_exists("publishedDate",$bookToSave["volumeInfo"])){ - $book->setPublishedDate(new DateTime($bookToSave["volumeInfo"]["publishedDate"])); - } - - $this->em->persist($book); - $this->em->flush(); - } } \ No newline at end of file diff --git a/src/UseCases/CreateBookUseCase.php b/src/UseCases/CreateBookUseCase.php new file mode 100644 index 0000000..3ab9625 --- /dev/null +++ b/src/UseCases/CreateBookUseCase.php @@ -0,0 +1,47 @@ +em = $em; + } + + public function create(array $bookToSave) + { + $book = new Book(); + + $book + ->setVolumeId($bookToSave["id"]) + ->setTitle($bookToSave["volumeInfo"]["title"]) + ->setDescription($bookToSave["volumeInfo"]["description"]) + ->setImage($bookToSave["volumeInfo"]["imageLinks"]["thumbnail"]); + + foreach ($bookToSave["volumeInfo"]["authors"] as $author) { + $foundAuthor = $this->em->getRepository(Author::class)->findOneBy(["name" => $author]); + if ($foundAuthor !== null) { + $book->setAuthors($foundAuthor); + continue; + } + } + + if (key_exists("publishedDate",$bookToSave["volumeInfo"])){ + $book->setPublishedDate(new DateTime($bookToSave["volumeInfo"]["publishedDate"])); + } + $this->em->persist($book); + $this->em->flush(); + } + +} \ No newline at end of file