diff --git a/src/Bundle/ChillPersonBundle/Audit/SubjectConverter/AccompanyingPeriodWorkSubjectConverter.php b/src/Bundle/ChillPersonBundle/Audit/SubjectConverter/AccompanyingPeriodWorkSubjectConverter.php new file mode 100644 index 000000000..5e4250faf --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Audit/SubjectConverter/AccompanyingPeriodWorkSubjectConverter.php @@ -0,0 +1,52 @@ + + */ +class AccompanyingPeriodWorkSubjectConverter implements SubjectConverterInterface, SubjectConverterManagerAwareInterface +{ + use SubjectConverterManagerAwareTrait; + + public function convert(mixed $subject): Subject|array + { + $data = [new Subject('accompanying_period_work', ['id' => $subject->getId()])]; + + foreach ($subject->getPersons() as $person) { + $personSubject = $this->subjectConverterManager->getSubjectsForEntity($person); + if ($personSubject instanceof Subject) { + $data[] = $personSubject; + } else { + array_push($data, ...$personSubject); + } + } + + return $data; + } + + public function supportsConvert(mixed $subject): bool + { + return $subject instanceof AccompanyingPeriodWork; + } + + public static function getDefaultPriority(): int + { + return 10; + } +} diff --git a/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseWorkController.php b/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseWorkController.php index 29362ba83..a2c9dae82 100644 --- a/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseWorkController.php +++ b/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseWorkController.php @@ -11,6 +11,8 @@ declare(strict_types=1); namespace Chill\PersonBundle\Controller; +use Chill\MainBundle\Audit\AuditEvent; +use Chill\MainBundle\Entity\AuditTrail; use Chill\MainBundle\Entity\User; use Chill\MainBundle\Pagination\PaginatorFactory; use Chill\MainBundle\Templating\Listing\FilterOrderHelper; @@ -21,6 +23,7 @@ use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWork; use Chill\PersonBundle\Entity\SocialWork\SocialAction; use Chill\PersonBundle\Repository\AccompanyingPeriod\AccompanyingPeriodWorkRepository; use Chill\PersonBundle\Security\Authorization\AccompanyingPeriodWorkVoter; +use Psr\EventDispatcher\EventDispatcherInterface; use Psr\Log\LoggerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Form\Extension\Core\Type\SubmitType; @@ -29,6 +32,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Serializer\SerializerInterface; +use Symfony\Component\Translation\TranslatableMessage; use Symfony\Contracts\Translation\TranslatorInterface; final class AccompanyingCourseWorkController extends AbstractController @@ -42,6 +46,7 @@ final class AccompanyingCourseWorkController extends AbstractController private readonly TranslatableStringHelperInterface $translatableStringHelper, private readonly FilterOrderHelperFactoryInterface $filterOrderHelperFactory, private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry, + private readonly EventDispatcherInterface $eventDispatcher, ) {} #[Route(path: '{_locale}/person/accompanying-period/{id}/work/new', name: 'chill_person_accompanying_period_work_new', methods: ['GET'])] @@ -154,6 +159,15 @@ final class AccompanyingCourseWorkController extends AbstractController $paginator->getCurrentPageFirstItemNumber() ); + $this->eventDispatcher->dispatch( + new AuditEvent( + AuditTrail::AUDIT_VIEW, + [$period], + new TranslatableMessage('accompanying_period.audit.show_list_work'), + ['action' => 'show_list_work'] + ) + ); + return $this->render('@ChillPerson/AccompanyingCourseWork/index.html.twig', [ 'accompanyingCourse' => $period, 'works' => $works, @@ -171,6 +185,13 @@ final class AccompanyingCourseWorkController extends AbstractController $this->denyAccessUnlessGranted(AccompanyingPeriodWorkVoter::SEE, $work); + $this->eventDispatcher->dispatch( + new AuditEvent( + AuditTrail::AUDIT_VIEW, + [$work], + ) + ); + return $this->render('@ChillPerson/AccompanyingCourseWork/show.html.twig', [ 'accompanyingCourse' => $work->getAccompanyingPeriod(), 'work' => $work, diff --git a/src/Bundle/ChillPersonBundle/Tests/Audit/SubjectConverter/AccompanyingPeriodWorkSubjectConverterTest.php b/src/Bundle/ChillPersonBundle/Tests/Audit/SubjectConverter/AccompanyingPeriodWorkSubjectConverterTest.php new file mode 100644 index 000000000..d8b554833 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Tests/Audit/SubjectConverter/AccompanyingPeriodWorkSubjectConverterTest.php @@ -0,0 +1,83 @@ +converter = new AccompanyingPeriodWorkSubjectConverter(); + } + + public function testSupportsConvert(): void + { + $this->assertTrue($this->converter->supportsConvert($this->prophesize(AccompanyingPeriodWork::class)->reveal())); + $this->assertFalse($this->converter->supportsConvert(new \stdClass())); + } + + public function testConvert(): void + { + $accompanyingPeriodWork = $this->prophesize(AccompanyingPeriodWork::class); + $accompanyingPeriodWork->getId()->willReturn(456); + + $person1 = $this->prophesize(Person::class); + $person2 = $this->prophesize(Person::class); + + $accompanyingPeriodWork->getPersons()->willReturn(new ArrayCollection([ + $person1->reveal(), + $person2->reveal(), + ])); + + $subjectConverterManager = $this->prophesize(SubjectConverterManagerInterface::class); + + $personSubject1 = new Subject('person', ['id' => 1]); + $personSubject2 = new Subject('person', ['id' => 2]); + + $subjectConverterManager->getSubjectsForEntity($person1->reveal())->willReturn($personSubject1); + $subjectConverterManager->getSubjectsForEntity($person2->reveal())->willReturn([$personSubject2]); + + $this->converter->setSubjectConverterManager($subjectConverterManager->reveal()); + + $result = $this->converter->convert($accompanyingPeriodWork->reveal()); + + $this->assertCount(3, $result); + $this->assertInstanceOf(Subject::class, $result[0]); + $this->assertSame('accompanying_period_work', $result[0]->type); + $this->assertSame(['id' => 456], $result[0]->identifiers); + + $this->assertSame($personSubject1, $result[1]); + $this->assertSame($personSubject2, $result[2]); + } + + public function testGetDefaultPriority(): void + { + $this->assertSame(10, AccompanyingPeriodWorkSubjectConverter::getDefaultPriority()); + } +}