From 2b0e41e412ebb079b9328444d73288d1ff361f31 Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Fri, 7 Jun 2024 14:34:56 +0200 Subject: [PATCH] Remove deserialization and entity manager merge --- .github/workflows/test-application.yaml | 52 ++++++++++++++++---- .gitignore | 3 ++ Controller/TaskController.php | 63 +++++++------------------ Entity/DoctrineTaskRepository.php | 2 + Resources/config/services.xml | 2 +- composer.json | 2 +- phpstan.neon | 1 - 7 files changed, 66 insertions(+), 59 deletions(-) diff --git a/.github/workflows/test-application.yaml b/.github/workflows/test-application.yaml index 8111476..c11af70 100644 --- a/.github/workflows/test-application.yaml +++ b/.github/workflows/test-application.yaml @@ -20,42 +20,36 @@ jobs: matrix: include: - php-version: '7.2' - lint: false dependency-versions: 'lowest' tools: 'composer:v2' env: SYMFONY_DEPRECATIONS_HELPER: disabled - php-version: '7.4' - lint: true dependency-versions: 'highest' tools: 'composer:v2' env: SYMFONY_DEPRECATIONS_HELPER: weak - php-version: '8.0' - lint: false dependency-versions: 'highest' tools: 'composer:v2' env: SYMFONY_DEPRECATIONS_HELPER: weak - php-version: '8.1' - lint: false dependency-versions: 'highest' tools: 'composer:v2' env: SYMFONY_DEPRECATIONS_HELPER: weak - php-version: '8.2' - lint: false dependency-versions: 'highest' tools: 'composer:v2' env: SYMFONY_DEPRECATIONS_HELPER: weak - php-version: '8.3' - lint: false dependency-versions: 'highest' tools: 'composer:v2' env: @@ -93,10 +87,48 @@ jobs: - name: Bootstrap test environment run: composer bootstrap-test-environment - - name: Lint code - if: ${{ matrix.lint }} - run: composer lint - - name: Execute test cases run: composer test env: ${{ matrix.env }} + + lint: + name: 'PHP Lint' + runs-on: ubuntu-latest + + env: + DATABASE_URL: 'mysql://root:root@127.0.0.1/sulu_automation_test?serverVersion=5.7' + + services: + mysql: + image: mysql:5.7 + env: + MYSQL_ROOT_PASSWORD: root + ports: + - 3306:3306 + options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=5 + + steps: + - name: Checkout project + uses: actions/checkout@v2 + + - name: Install and configure PHP + uses: shivammathur/setup-php@v2 + with: + php-version: 8.3 + extensions: 'mysql, gd' + tools: composer:v2 + + - name: Remove not required tooling + run: composer remove php-cs-fixer/shim "*phpstan*" --dev --no-interaction --no-update + + - name: Install composer dependencies + uses: ramsey/composer-install@v1 + with: + dependency-versions: highest + composer-options: highest + + - name: Bootstrap test environment + run: composer bootstrap-test-environment + + - name: Lint code + run: composer lint diff --git a/.gitignore b/.gitignore index 3005148..0b1e076 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,6 @@ Tests/Application/.env.test.local # php-cs-fixer .php-cs-fixer.cache php-cs-fixer + +.env.local +.env.*.local diff --git a/Controller/TaskController.php b/Controller/TaskController.php index 2a5cac8..b3f00e6 100644 --- a/Controller/TaskController.php +++ b/Controller/TaskController.php @@ -93,7 +93,7 @@ class TaskController extends AbstractRestController implements ClassResourceInte protected $entityManager; /** - * @var SerializerInterface + * @var SerializerInterface|null */ protected $serializer; @@ -112,7 +112,7 @@ public function __construct( RestHelperInterface $doctrineRestHelper, TaskManagerInterface $taskManager, EntityManagerInterface $entityManager, - SerializerInterface $serializer, + ?SerializerInterface $serializer, FieldDescriptorFactoryInterface $fieldDescriptorFactory, AutomationTaskRepositoryInterface $automationTaskRepository ) { @@ -298,27 +298,14 @@ public function getCountAction(Request $request): Response */ public function postAction(Request $request): Response { - $data = \array_merge( - [ - 'scheme' => $request->getScheme(), - 'host' => $request->getHost(), - 'entityId' => $request->get('entityId'), - 'entityClass' => $request->get('entityClass'), - 'locale' => $request->get('locale'), - ], - \array_filter($request->request->all()) - ); - - $context = DeserializationContext::create(); - $context->setGroups(['api']); - - /** @var TaskInterface $task */ - $task = $this->serializer->deserialize( - (string) \json_encode($data), - Task::class, - 'json', - $context - ); + $task = new Task(); + $task->setScheme($request->getScheme()); + $task->setHost($request->getHost()); + $task->setEntityId((string) $request->query->get('entityId')); + $task->setEntityClass((string) $request->query->get('entityClass')); + $task->setLocale($this->getLocale($request)); + $task->setHandlerClass((string) $request->request->get('handlerClass')); + $task->setSchedule(new \DateTime((string) $request->request->get('schedule'))); $this->taskManager->create($task); @@ -332,32 +319,16 @@ public function postAction(Request $request): Response */ public function putAction(string $id, Request $request): Response { - $data = \array_merge( - [ - 'id' => $id, - 'scheme' => $request->getScheme(), - 'host' => $request->getHost(), - 'entityId' => $request->get('entityId'), - 'entityClass' => $request->get('entityClass'), - 'locale' => $request->get('locale'), - ], - \array_filter($request->request->all()) - ); - - $context = DeserializationContext::create(); - $context->setGroups(['api']); - - /** @var TaskInterface $task */ - $task = $this->serializer->deserialize( - (string) \json_encode($data), - Task::class, - 'json', - $context - ); + /** @var Task $task */ + $task = $this->taskManager->findById($id); + $task->setScheme($request->getScheme()); + $task->setHost($request->getHost()); + $task->setLocale($this->getLocale($request)); + $task->setHandlerClass((string) $request->request->get('handlerClass')); + $task->setSchedule(new \DateTime((string) $request->request->get('schedule'))); $task = $this->taskManager->update($task); - $this->entityManager->merge($task); $this->entityManager->flush(); return $this->handleView($this->view($task)); diff --git a/Entity/DoctrineTaskRepository.php b/Entity/DoctrineTaskRepository.php index 93c8383..4f9c25b 100644 --- a/Entity/DoctrineTaskRepository.php +++ b/Entity/DoctrineTaskRepository.php @@ -19,6 +19,8 @@ /** * Task-Repository implementation for doctrine. + * + * @extends EntityRepository */ class DoctrineTaskRepository extends EntityRepository implements TaskRepositoryInterface { diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 341d3e4..77f5cbc 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -21,7 +21,7 @@ - + null diff --git a/composer.json b/composer.json index f0f2643..8bc2e53 100644 --- a/composer.json +++ b/composer.json @@ -23,10 +23,10 @@ "symfony/http-kernel": "^4.3 || ^5.0 || ^6.0" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^2.17 || ^3.0", "handcraftedinthealps/zendsearch": "^2.0", "jackalope/jackalope-doctrine-dbal": "^1.3.0", "jangregor/phpstan-prophecy": "^1.0", + "php-cs-fixer/shim": "^3.0", "phpspec/prophecy": "^1.8", "phpstan/phpstan": "^1.0", "phpstan/phpstan-doctrine": "^1.0", diff --git a/phpstan.neon b/phpstan.neon index 0809386..bd8d083 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -20,4 +20,3 @@ parameters: console_application_loader: Tests/phpstan/console-application.php doctrine: objectManagerLoader: Tests/phpstan/object-manager.php - checkGenericClassInNonGenericObjectType: false