From acd47a647e9a94740e9666d1a121060999ebaad5 Mon Sep 17 00:00:00 2001 From: Mikkel Ricky Date: Thu, 27 Feb 2025 14:55:13 +0100 Subject: [PATCH 1/8] =?UTF-8?q?Added=20=E2=80=9CRegistration=20deadline?= =?UTF-8?q?=E2=80=9D=20to=20date?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 3 + itk_pretix.install | 72 +++++++++++++++++++ src/NodeHelper.php | 2 +- .../FieldFormatter/PretixDateFormatter.php | 1 + src/Plugin/Field/FieldType/PretixDate.php | 16 +++++ .../Field/FieldWidget/PretixDateWidget.php | 32 ++++++++- src/Pretix/EventHelper.php | 2 +- 7 files changed, 124 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 208fdcc..55125d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ before starting to add changes. ## [Unreleased] +* [PR-69](https://github.com/itk-dev/itk_pretix/pull/69) + Added “Registration deadline” to date + ## 1.2.2 - Allow nullabale pretix client diff --git a/itk_pretix.install b/itk_pretix.install index cc8e20d..ecf3f3c 100644 --- a/itk_pretix.install +++ b/itk_pretix.install @@ -5,6 +5,8 @@ * Installation file for the itk_pretix module. */ +use Drupal\Core\Entity\Sql\SqlContentEntityStorage; + /** * Implements hook_schema(). */ @@ -93,3 +95,73 @@ function itk_pretix_schema() { ], ]; } + +/** + * Implements hook_update_N(). + */ +function itk_pretix_update_10101(&$sandbox) { + _itk_pretix_field_type_add_properties( + 'pretix_date', + [ + 'registration_deadline_value' => [ + 'description' => 'The registration deadline value.', + 'type' => 'varchar', + 'length' => 20, + ], + ] + ); +} + +/** + * Lifted from https://drupal.stackexchange.com/a/295485. + */ +function _itk_pretix_field_type_add_properties(string $field_type, array $columns): void { + $database_schema = \Drupal::database()->schema(); + + $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager(); + + // First, get all fields of the provided field type. + $field_map = \Drupal::service('entity_field.manager') + ->getFieldMapByFieldType($field_type); + + // Process per entity type (i.e., node, paragraph) all fields. + foreach ($field_map as $entity_type_id => $fields) { + $entity_type_storage = \Drupal::entityTypeManager() + ->getStorage($entity_type_id); + + // Database schema can only be updated, if the values are stored in the + // database, of course. + if ($entity_type_storage instanceof SqlContentEntityStorage) { + foreach (array_keys($fields) as $field_name) { + $field_storage_definition = $entity_definition_update_manager->getFieldStorageDefinition($field_name, + $entity_type_id); + + // Get the table and column names, according to the field definition. + $table_mapping = $entity_type_storage->getTableMapping([ + $field_name => $field_storage_definition, + ]); + $table_names = $table_mapping->getDedicatedTableNames(); + $table_columns = $table_mapping->getColumnNames($field_name); + + // Check if, for the property, the corresponding table exists in the + // database. If not, create the table column (according to the schema + // provided by the field definition). + foreach ($table_names as $table_name) { + $table_exists = $database_schema->tableExists($table_name); + foreach ($columns as $property => $spec) { + $field_exists = $database_schema->fieldExists($table_name, + $table_columns[$property]); + + if ($table_exists && !$field_exists) { + $database_schema->addField($table_name, $table_columns[$property], + $spec); + } + } + } + + // Reflect any changes made to the field storage definition. + $entity_definition_update_manager->updateFieldStorageDefinition($field_storage_definition); + } + } + } +} diff --git a/src/NodeHelper.php b/src/NodeHelper.php index b7830f4..2ae41c7 100644 --- a/src/NodeHelper.php +++ b/src/NodeHelper.php @@ -226,7 +226,7 @@ private function getPretixDates(NodeInterface $node): ?FieldItemListInterface { if (NULL !== $items) { foreach ($items as $item) { - foreach (['time_from', 'time_to'] as $key) { + foreach (['registration_deadline', 'time_from', 'time_to'] as $key) { if (isset($item->{$key}) && is_string($item->{$key})) { $item->{$key} = new DrupalDateTime($item->{$key}); } diff --git a/src/Plugin/Field/FieldFormatter/PretixDateFormatter.php b/src/Plugin/Field/FieldFormatter/PretixDateFormatter.php index b90494c..89e6814 100644 --- a/src/Plugin/Field/FieldFormatter/PretixDateFormatter.php +++ b/src/Plugin/Field/FieldFormatter/PretixDateFormatter.php @@ -83,6 +83,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { 'entity' => $items->getEntity(), 'location' => $item->location, 'address' => $item->address, + 'registration_deadline' => $item->registration_deadline, 'time_from' => $item->time_from, 'time_to' => $item->time_to, 'spots' => $item->spots, diff --git a/src/Plugin/Field/FieldType/PretixDate.php b/src/Plugin/Field/FieldType/PretixDate.php index 9199743..7a68206 100644 --- a/src/Plugin/Field/FieldType/PretixDate.php +++ b/src/Plugin/Field/FieldType/PretixDate.php @@ -24,6 +24,7 @@ * @property string uuid * @property string location * @property string address + * @property DateTimeComputed registration_deadline * @property DateTimeComputed time_from * @property DateTimeComputed time_to * @property int spots @@ -45,6 +46,16 @@ public static function propertyDefinitions(FieldStorageDefinitionInterface $fiel ->setLabel(new TranslatableMarkup('Address')) ->setRequired(TRUE); + $properties['registration_deadline_value'] = DataDefinition::create('datetime_iso8601') + ->setLabel(t('Registration deadline value')) + ->setRequired(TRUE); + $properties['registration_deadline'] = DataDefinition::create('any') + ->setLabel(t('Computed registration deadline')) + ->setDescription(t('The computed registration deadline DateTime object.')) + ->setComputed(TRUE) + ->setClass(DateTimeComputed::class) + ->setSetting('date source', 'registration_deadline_value'); + $properties['time_from_value'] = DataDefinition::create('datetime_iso8601') ->setLabel(t('Time from value')) ->setRequired(TRUE); @@ -91,6 +102,11 @@ public static function schema(FieldStorageDefinitionInterface $field_definition) 'type' => 'varchar', 'length' => 255, ], + 'registration_deadline_value' => [ + 'description' => 'The registration deadline value.', + 'type' => 'varchar', + 'length' => 20, + ], 'time_from_value' => [ 'description' => 'The time from value.', 'type' => 'varchar', diff --git a/src/Plugin/Field/FieldWidget/PretixDateWidget.php b/src/Plugin/Field/FieldWidget/PretixDateWidget.php index cd1f8ec..35f3e1e 100644 --- a/src/Plugin/Field/FieldWidget/PretixDateWidget.php +++ b/src/Plugin/Field/FieldWidget/PretixDateWidget.php @@ -98,6 +98,21 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen '#required' => $element['#required'], ]; + $element['registration_deadline_value'] = [ + '#title' => t('Registration deadline'), + '#type' => 'datetime', + '#default_value' => NULL, + '#date_increment' => 1, + '#date_timezone' => date_default_timezone_get(), + '#required' => $element['#required'], + ]; + + if ($items[$delta]->registration_deadline) { + /** @var \Drupal\Core\Datetime\DrupalDateTime $registration_deadline */ + $registration_deadline = $items[$delta]->registration_deadline; + $element['registration_deadline_value']['#default_value'] = $this->createDefaultValue($registration_deadline, $element['registration_deadline_value']['#date_timezone']); + } + $element['time_from_value'] = [ '#title' => t('Start time'), '#type' => 'datetime', @@ -225,6 +240,14 @@ public function massageFormValues(array $values, array $form, FormStateInterface $user_timezone = new \DateTimeZone(date_default_timezone_get()); foreach ($values as &$item) { + if (!empty($item['registration_deadline_value']) && $item['registration_deadline_value'] instanceof DrupalDateTime) { + /** @var \Drupal\Core\Datetime\DrupalDateTime $time_from */ + $registration_deadline = $item['registration_deadline_value']; + + // Adjust the date for storage. + $item['registration_deadline_value'] = $registration_deadline->setTimezone($storage_timezone)->format($storage_format); + } + if (!empty($item['time_from_value']) && $item['time_from_value'] instanceof DrupalDateTime) { /** @var \Drupal\Core\Datetime\DrupalDateTime $time_from */ $time_from = $item['time_from_value']; @@ -341,12 +364,17 @@ public function errorElement( * The complete form structure. */ public function validate(array &$element, FormStateInterface $form_state, array &$complete_form) { - $time_from = $element['time_from_value']['#value']['object']; - $time_to = $element['time_to_value']['#value']['object']; + $time_from = $element['time_from_value']['#value']['object'] ?? NULL; + $time_to = $element['time_to_value']['#value']['object'] ?? NULL; if ($time_from instanceof DrupalDateTime && $time_to instanceof DrupalDateTime && $time_to < $time_from) { $form_state->setError($element['time_to_value'], $this->t('The end time cannot be before the start time')); } + + $registration_deadline = $element['registration_deadline_value']['#value']['object'] ?? NULL; + if ($registration_deadline instanceof DrupalDateTime && $time_from instanceof DrupalDateTime && $time_from < $registration_deadline) { + $form_state->setError($element['registration_deadline_value'], $this->t('The registration deadline must be before the start time')); + } } /** diff --git a/src/Pretix/EventHelper.php b/src/Pretix/EventHelper.php index a68575e..f30ca6c 100644 --- a/src/Pretix/EventHelper.php +++ b/src/Pretix/EventHelper.php @@ -290,7 +290,7 @@ private function synchronizePretixSubEvent(PretixDate $item, Event $event, NodeI 'active' => TRUE, 'is_public' => TRUE, 'date_admission' => NULL, - 'presale_end' => NULL, + 'presale_end' => $this->formatDate($item->registration_deadline), 'seating_plan' => NULL, 'seat_category_mapping' => (object) [], ]); From 7dbf791a69a73df5ed0e44aba3e3d58f91d6f851 Mon Sep 17 00:00:00 2001 From: Mikkel Ricky Date: Thu, 27 Feb 2025 23:14:32 +0100 Subject: [PATCH 2/8] Updated proxy classes --- scripts/.env | 2 + scripts/base | 98 +++++++++++++++++++ scripts/compose.yaml | 20 ++++ scripts/generate-proxy-classes | 20 ++++ .../Exporter/CheckInListExporter.php | 20 +--- .../Exporter/CheckInListPdfExporter.php | 20 +--- src/ProxyClass/Exporter/Manager.php | 10 +- src/ProxyClass/Exporter/OrderListExporter.php | 20 +--- 8 files changed, 151 insertions(+), 59 deletions(-) create mode 100644 scripts/.env create mode 100644 scripts/base create mode 100644 scripts/compose.yaml create mode 100755 scripts/generate-proxy-classes diff --git a/scripts/.env b/scripts/.env new file mode 100644 index 0000000..34c4496 --- /dev/null +++ b/scripts/.env @@ -0,0 +1,2 @@ +COMPOSE_PROJECT_NAME=drupal-module +MODULE_NAME=itk_pretix diff --git a/scripts/base b/scripts/base new file mode 100644 index 0000000..7ac4abd --- /dev/null +++ b/scripts/base @@ -0,0 +1,98 @@ +#!/usr/bin/env bash +set -o errexit -o errtrace -o noclobber -o nounset -o pipefail +IFS=$'\n\t' + +execute_name=execute + +usage() { + (cat >&2 </dev/null); then + (cat >&2 <&2 <lazyLoadItself()->__wakeup(); } - /** - * {@inheritdoc} - */ - public function setLinkGenerator(\Drupal\Core\Utility\LinkGeneratorInterface $generator) - { - return $this->lazyLoadItself()->setLinkGenerator($generator); - } - /** * {@inheritdoc} */ @@ -235,14 +227,6 @@ public function setStringTranslation(\Drupal\Core\StringTranslation\TranslationI return $this->lazyLoadItself()->setStringTranslation($translation); } - /** - * {@inheritdoc} - */ - public function setUrlGenerator(\Drupal\Core\Routing\UrlGeneratorInterface $generator) - { - return $this->lazyLoadItself()->setUrlGenerator($generator); - } - } } diff --git a/src/ProxyClass/Exporter/CheckInListPdfExporter.php b/src/ProxyClass/Exporter/CheckInListPdfExporter.php index 0c17707..755aa6a 100644 --- a/src/ProxyClass/Exporter/CheckInListPdfExporter.php +++ b/src/ProxyClass/Exporter/CheckInListPdfExporter.php @@ -1,8 +1,8 @@ lazyLoadItself()->__wakeup(); } - /** - * {@inheritdoc} - */ - public function setLinkGenerator(\Drupal\Core\Utility\LinkGeneratorInterface $generator) - { - return $this->lazyLoadItself()->setLinkGenerator($generator); - } - /** * {@inheritdoc} */ @@ -235,14 +227,6 @@ public function setStringTranslation(\Drupal\Core\StringTranslation\TranslationI return $this->lazyLoadItself()->setStringTranslation($translation); } - /** - * {@inheritdoc} - */ - public function setUrlGenerator(\Drupal\Core\Routing\UrlGeneratorInterface $generator) - { - return $this->lazyLoadItself()->setUrlGenerator($generator); - } - } } diff --git a/src/ProxyClass/Exporter/Manager.php b/src/ProxyClass/Exporter/Manager.php index 2b45cce..5ae48a8 100644 --- a/src/ProxyClass/Exporter/Manager.php +++ b/src/ProxyClass/Exporter/Manager.php @@ -1,8 +1,8 @@ lazyLoadItself()->getEventExporters($ids); } @@ -86,7 +86,7 @@ public function getEventExporters(array $ids = NULL) /** * {@inheritdoc} */ - public function getEventExporter($id) + public function getEventExporter(string $id) { return $this->lazyLoadItself()->getEventExporter($id); } @@ -102,7 +102,7 @@ public function saveExporterResult(\Drupal\node\NodeInterface $node, \GuzzleHttp /** * {@inheritdoc} */ - public function fileDownload($uri) + public function fileDownload(string $uri) { return $this->lazyLoadItself()->fileDownload($uri); } diff --git a/src/ProxyClass/Exporter/OrderListExporter.php b/src/ProxyClass/Exporter/OrderListExporter.php index c09dc3b..dfe38f1 100644 --- a/src/ProxyClass/Exporter/OrderListExporter.php +++ b/src/ProxyClass/Exporter/OrderListExporter.php @@ -1,8 +1,8 @@ lazyLoadItself()->__wakeup(); } - /** - * {@inheritdoc} - */ - public function setLinkGenerator(\Drupal\Core\Utility\LinkGeneratorInterface $generator) - { - return $this->lazyLoadItself()->setLinkGenerator($generator); - } - /** * {@inheritdoc} */ @@ -235,14 +227,6 @@ public function setStringTranslation(\Drupal\Core\StringTranslation\TranslationI return $this->lazyLoadItself()->setStringTranslation($translation); } - /** - * {@inheritdoc} - */ - public function setUrlGenerator(\Drupal\Core\Routing\UrlGeneratorInterface $generator) - { - return $this->lazyLoadItself()->setUrlGenerator($generator); - } - } } From 90c23fb8e031a5288ccfb08ab757c24207406e9e Mon Sep 17 00:00:00 2001 From: Mikkel Ricky Date: Thu, 27 Feb 2025 23:32:03 +0100 Subject: [PATCH 3/8] Fixed use of deprecated constant --- scripts/code-analysis | 38 ++++++++------------------------------ src/Exporter/Manager.php | 5 +++-- 2 files changed, 11 insertions(+), 32 deletions(-) diff --git a/scripts/code-analysis b/scripts/code-analysis index a370d73..d0bb172 100755 --- a/scripts/code-analysis +++ b/scripts/code-analysis @@ -1,35 +1,13 @@ #!/usr/bin/env bash -script_dir=$(pwd) -module_name=$(basename "$script_dir") -drupal_dir=vendor/drupal-module-code-analysis -# Relative to $drupal_dir -module_path=web/modules/contrib/$module_name +set -o errexit -o errtrace -o noclobber -o nounset -o pipefail +IFS=$'\n\t' -cd "$script_dir" || exit +script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +cd "$script_dir" -drupal_composer() { - composer --working-dir="$drupal_dir" --no-interaction "$@" +execute() { + composer require --dev symfony/phpunit-bridge + shell vendor/bin/phpstan --configuration="$module_path/phpstan.neon" --memory-limit=256M } -# Create new Drupal 9 project -if [ ! -f "$drupal_dir/composer.json" ]; then - composer --no-interaction create-project drupal/recommended-project:^10 "$drupal_dir" -fi -# Copy our code into the modules folder -mkdir -p "$drupal_dir/$module_path" -# https://stackoverflow.com/a/15373763 -rsync --archive --compress . --filter=':- .gitignore' --exclude "$drupal_dir" --exclude .git "$drupal_dir/$module_path" - -drupal_composer config minimum-stability dev - -# Allow ALL plugins -# https://getcomposer.org/doc/06-config.md#allow-plugins -drupal_composer config --no-plugins allow-plugins true - -drupal_composer require wikimedia/composer-merge-plugin -drupal_composer config extra.merge-plugin.include "$module_path/composer.json" -# https://www.drupal.org/project/drupal/issues/3220043#comment-14845434 -drupal_composer require --dev symfony/phpunit-bridge - -# Run PHPStan -(cd "$drupal_dir" && vendor/bin/phpstan --configuration="$module_path/phpstan.neon" --memory-limit=256M) +source "$script_dir/base" diff --git a/src/Exporter/Manager.php b/src/Exporter/Manager.php index 9a03c73..67eccb4 100644 --- a/src/Exporter/Manager.php +++ b/src/Exporter/Manager.php @@ -2,6 +2,7 @@ namespace Drupal\itk_pretix\Exporter; +use Drupal\Core\File\FileExists; use Drupal\Core\File\FileSystem; use Drupal\Core\File\FileUrlGenerator; use Drupal\Core\Session\AccountInterface; @@ -81,8 +82,8 @@ public function saveExporterResult(NodeInterface $node, Response $response) { $directory = dirname($url); $this->fileSystem->prepareDirectory($directory, FileSystem::CREATE_DIRECTORY); $filePath = $this->fileSystem->realpath($url); - $this->fileSystem->saveData((string) $response->getBody(), $filePath, FileSystem::EXISTS_REPLACE); - $this->fileSystem->saveData(json_encode($response->getHeaders()), $filePath . '.headers', FileSystem::EXISTS_REPLACE); + $this->fileSystem->saveData((string) $response->getBody(), $filePath, FileExists::Replace); + $this->fileSystem->saveData(json_encode($response->getHeaders()), $filePath . '.headers', FileExists::Replace); return $this->fileUrlGenerator->generateAbsoluteString($url); } From 89305bfa48afab59b1538a9607a09e3d00615fa5 Mon Sep 17 00:00:00 2001 From: Mikkel Ricky Date: Thu, 27 Feb 2025 23:37:15 +0100 Subject: [PATCH 4/8] Applied coding standards --- README.md | 17 +++-- compose.yaml | 15 ++++ composer.json | 74 ++++++++++--------- phpcs.xml.dist | 5 +- src/Exporter/Manager.php | 4 +- src/Exporter/ManagerInterface.php | 2 +- src/NodeHelper.php | 2 +- .../Field/FieldType/PretixEventSettings.php | 8 +- .../Field/FieldWidget/PretixDateWidget.php | 10 +-- .../FieldWidget/PretixEventSettingsWidget.php | 12 +-- src/Pretix/AbstractHelper.php | 6 +- src/Pretix/EventHelper.php | 2 +- 12 files changed, 91 insertions(+), 66 deletions(-) create mode 100644 compose.yaml diff --git a/README.md b/README.md index 9e78fcd..8dbd63d 100644 --- a/README.md +++ b/README.md @@ -49,19 +49,19 @@ The available exporters for a node can be run from First, install tools and requirements: ```sh -yarn install +docker compose run --rm node yarn install ``` Build for development: ```sh -yarn encore dev --watch +docker compose run --rm node yarn encore dev --watch ``` Build for production: ```sh -yarn encore production +docker compose run --rm node yarn encore production ``` ## Coding standards @@ -71,14 +71,15 @@ The code must follw the [Drupal Coding Standards](https://www.drupal.org/docs/de Check the coding standards by running ```sh -composer install -composer coding-standards-check +docker compose run --rm phpfpm composer install +docker compose run --rm phpfpm composer coding-standards-check ``` Apply the coding standards by running ```sh -composer coding-standards-apply +docker compose run --rm phpfpm composer normalize +docker compose run --rm phpfpm composer coding-standards-apply ``` ### Assets @@ -86,11 +87,11 @@ composer coding-standards-apply Check the coding standards in assets by running ```sh -yarn coding-standards-check +docker compose run --rm node yarn coding-standards-check ``` Apply the coding standards by running ```sh -yarn coding-standards-apply +docker compose run --rm node yarn coding-standards-apply ``` diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..6e55e8d --- /dev/null +++ b/compose.yaml @@ -0,0 +1,15 @@ +services: + phpfpm: + image: itkdev/php8.3-fpm:latest + profiles: + - dev + volumes: + - .:/app + + node: + image: node:22 + profiles: + - dev + working_dir: /app + volumes: + - .:/app diff --git a/composer.json b/composer.json index 662f218..0a2df0c 100644 --- a/composer.json +++ b/composer.json @@ -1,52 +1,58 @@ { "name": "itk-dev/itk_pretix", - "type": "drupal-module", "description": "A module using pretix API to generate events from field values", - "keywords": ["Drupal"], "license": "MIT", - "minimum-stability": "dev", + "type": "drupal-module", + "keywords": [ + "Drupal" + ], "require": { - "php": "^8.1", - "nicoeg/dawa": "^1.0", - "itk-dev/pretix-api-client-php": "^1.1" + "php": "^8.3", + "itk-dev/pretix-api-client-php": "^1.1", + "nicoeg/dawa": "^1.0" }, "require-dev": { - "drupal/coder": "^8.3.1", - "dealerdirect/phpcodesniffer-composer-installer": "^1.0", - "mglaman/phpstan-drupal": "^1.1", - "phpstan/extension-installer": "^1.3", - "phpstan/phpstan-deprecation-rules": "^1.1" + "dealerdirect/phpcodesniffer-composer-installer": "^0.7.1", + "drupal/coder": "^8.3", + "ergebnis/composer-normalize": "^2.45", + "mglaman/phpstan-drupal": "^1.3", + "palantirnet/drupal-rector": "^0.20.3", + "phpstan/extension-installer": "^1.4", + "phpstan/phpstan": "^1.12", + "phpstan/phpstan-deprecation-rules": "^1.2" + }, + "repositories": { + "drupal": { + "type": "composer", + "url": "https://packages.drupal.org/8" + } + }, + "minimum-stability": "dev", + "config": { + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true, + "ergebnis/composer-normalize": true, + "phpstan/extension-installer": true + } }, "scripts": { - "coding-standards-check/phpcs": [ - "vendor/bin/phpcs --standard=phpcs.xml.dist" - ], - "coding-standards-check": [ - "@coding-standards-check/phpcs" + "code-analysis": [ + "@code-analysis/phpstan" ], - "coding-standards-apply/phpcs": [ - "vendor/bin/phpcbf --standard=phpcs.xml.dist" + "code-analysis/phpstan": [ + "phpstan analyse --configuration=phpstan.neon" ], "coding-standards-apply": [ "@coding-standards-apply/phpcs" ], - "code-analysis/phpstan": [ - "phpstan analyse --configuration=phpstan.neon" + "coding-standards-apply/phpcs": [ + "vendor/bin/phpcbf --standard=phpcs.xml.dist" ], - "code-analysis": [ - "@code-analysis/phpstan" + "coding-standards-check": [ + "@coding-standards-check/phpcs" + ], + "coding-standards-check/phpcs": [ + "vendor/bin/phpcs --standard=phpcs.xml.dist" ] - }, - "repositories": { - "drupal": { - "type": "composer", - "url": "https://packages.drupal.org/8" - } - }, - "config": { - "allow-plugins": { - "dealerdirect/phpcodesniffer-composer-installer": true, - "phpstan/extension-installer": true - } } } diff --git a/phpcs.xml.dist b/phpcs.xml.dist index a6ac5bd..081d381 100755 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -4,13 +4,16 @@ . + rector.php + src/ProxyClass/ vendor/ + node_modules/ - + diff --git a/src/Exporter/Manager.php b/src/Exporter/Manager.php index 67eccb4..5a4349a 100644 --- a/src/Exporter/Manager.php +++ b/src/Exporter/Manager.php @@ -41,7 +41,7 @@ public function __construct( private readonly FileSystem $fileSystem, private readonly AccessCheck $accessCheck, private readonly AccountInterface $currentUser, - private readonly FileUrlGenerator $fileUrlGenerator + private readonly FileUrlGenerator $fileUrlGenerator, ) {} /** @@ -57,7 +57,7 @@ public function addEventExporter(ExporterInterface $exporter, $priority = 0) { /** * Get event exporters. */ - public function getEventExporters(array $ids = NULL) { + public function getEventExporters(?array $ids = NULL) { return array_filter($this->eventExporters, static function (ExporterInterface $exporter) use ($ids) { return NULL === $ids || in_array($exporter->getId(), $ids, TRUE); }); diff --git a/src/Exporter/ManagerInterface.php b/src/Exporter/ManagerInterface.php index e61e22f..750bfd6 100644 --- a/src/Exporter/ManagerInterface.php +++ b/src/Exporter/ManagerInterface.php @@ -24,7 +24,7 @@ public function addEventExporter(ExporterInterface $exporter, $priority = 0); * @return array|ExporterInterface[] * The exporters. */ - public function getEventExporters(array $ids = NULL); + public function getEventExporters(?array $ids = NULL); /** * Get event exporter. diff --git a/src/NodeHelper.php b/src/NodeHelper.php index 2ae41c7..5f84063 100644 --- a/src/NodeHelper.php +++ b/src/NodeHelper.php @@ -40,7 +40,7 @@ public function __construct( private readonly EventHelper $eventHelper, private readonly EntityTypeManagerInterface $entityTypeManager, private readonly ModuleHandlerInterface $moduleHandler, - MessengerInterface $messenger + MessengerInterface $messenger, ) { $this->setMessenger($messenger); } diff --git a/src/Plugin/Field/FieldType/PretixEventSettings.php b/src/Plugin/Field/FieldType/PretixEventSettings.php index 5676ccc..64b5134 100644 --- a/src/Plugin/Field/FieldType/PretixEventSettings.php +++ b/src/Plugin/Field/FieldType/PretixEventSettings.php @@ -26,8 +26,8 @@ class PretixEventSettings extends FieldItemBase { * {@inheritdoc} */ public static function propertyDefinitions( - FieldStorageDefinitionInterface $field_definition - ) { + FieldStorageDefinitionInterface $field_definition, + ) { $properties['template_event'] = DataDefinition::create('string') ->setLabel(new TranslatableMarkup('Template event')) ->setRequired(TRUE); @@ -42,8 +42,8 @@ public static function propertyDefinitions( * {@inheritdoc} */ public static function schema( - FieldStorageDefinitionInterface $field_definition - ) { + FieldStorageDefinitionInterface $field_definition, + ) { return [ 'columns' => [ 'template_event' => [ diff --git a/src/Plugin/Field/FieldWidget/PretixDateWidget.php b/src/Plugin/Field/FieldWidget/PretixDateWidget.php index 35f3e1e..416c43c 100644 --- a/src/Plugin/Field/FieldWidget/PretixDateWidget.php +++ b/src/Plugin/Field/FieldWidget/PretixDateWidget.php @@ -340,11 +340,11 @@ private function roundedTime($seconds) { * {@inheritdoc} */ public function errorElement( - array $element, - ConstraintViolationInterface $error, - array $form, - FormStateInterface $form_state - ) { + array $element, + ConstraintViolationInterface $error, + array $form, + FormStateInterface $form_state, + ) { $propertyPath = preg_replace('/^\d+\./', '', $error->getPropertyPath()); return $element[$propertyPath] ?? $element; } diff --git a/src/Plugin/Field/FieldWidget/PretixEventSettingsWidget.php b/src/Plugin/Field/FieldWidget/PretixEventSettingsWidget.php index c570b26..f8e5860 100644 --- a/src/Plugin/Field/FieldWidget/PretixEventSettingsWidget.php +++ b/src/Plugin/Field/FieldWidget/PretixEventSettingsWidget.php @@ -66,12 +66,12 @@ public static function create(ContainerInterface $container, array $configuratio * {@inheritdoc} */ public function formElement( - FieldItemListInterface $items, - $delta, - array $element, - array &$form, - FormStateInterface $form_state - ) { + FieldItemListInterface $items, + $delta, + array $element, + array &$form, + FormStateInterface $form_state, + ) { /** @var \Drupal\node\Entity\Node $node */ $node = $items->getParent()->getEntity(); $helper = $this->nodeHelper; diff --git a/src/Pretix/AbstractHelper.php b/src/Pretix/AbstractHelper.php index c7f79d0..9cdef3d 100644 --- a/src/Pretix/AbstractHelper.php +++ b/src/Pretix/AbstractHelper.php @@ -43,7 +43,7 @@ abstract class AbstractHelper { public function __construct( private readonly Connection $database, private readonly ConfigFactoryInterface $configFactory, - private readonly LoggerChannelFactoryInterface $loggerFactory + private readonly LoggerChannelFactoryInterface $loggerFactory, ) { } @@ -94,7 +94,7 @@ public function getPretixClient(NodeInterface $node): Client { * @return array * The configuration. */ - public function getPretixConfiguration(NodeInterface $node = NULL): array { + public function getPretixConfiguration(?NodeInterface $node = NULL): array { $config = $this->configFactory->get('itk_pretix.pretixconfig'); // @todo Handle node, e.g. to get user specific configuration. @@ -379,7 +379,7 @@ public function getPretixEventUrl($node, $path = ''): ?string { /** * Handle a pretix api client exception. */ - protected function clientException(string $message, \Exception $clientException = NULL): SynchronizeException { + protected function clientException(string $message, ?\Exception $clientException = NULL): SynchronizeException { // @todo Log the exception. if (NULL === $clientException) { $this->loggerFactory->get('itk_pretix')->error($message); diff --git a/src/Pretix/EventHelper.php b/src/Pretix/EventHelper.php index f30ca6c..d8ed420 100644 --- a/src/Pretix/EventHelper.php +++ b/src/Pretix/EventHelper.php @@ -57,7 +57,7 @@ public function __construct( ConfigFactoryInterface $configFactory, LoggerChannelFactoryInterface $loggerFactory, OrderHelper $orderHelper, - ModuleHandlerInterface $moduleHandler + ModuleHandlerInterface $moduleHandler, ) { parent::__construct($database, $configFactory, $loggerFactory); $this->orderHelper = $orderHelper; From d9d093d191ab9637495e5b6ef96082da96d928bf Mon Sep 17 00:00:00 2001 From: Mikkel Ricky Date: Thu, 27 Feb 2025 23:46:48 +0100 Subject: [PATCH 5/8] Refactored code --- README.md | 19 ++------ rector.php | 24 ++++++++++ scripts/rector | 13 ++++++ .../PretixEventExportersController.php | 11 ++--- src/Controller/PretixWebhookController.php | 11 ++--- src/Exporter/AbstractExporter.php | 15 +++--- src/Exporter/CheckInListExporter.php | 2 + src/Exporter/CheckInListPdfExporter.php | 2 + src/Exporter/Manager.php | 13 ++++-- src/Exporter/OrderListExporter.php | 1 + src/Form/PretixConfigForm.php | 46 +++++++------------ src/NodeHelper.php | 2 +- .../FieldFormatter/PretixDateFormatter.php | 25 +++++----- src/Plugin/Field/FieldType/PretixDate.php | 6 ++- .../Field/FieldType/PretixEventSettings.php | 3 ++ .../Field/FieldWidget/PretixDateWidget.php | 26 +++++++---- .../FieldWidget/PretixEventSettingsWidget.php | 28 +++++------ .../PretixDateConstraintValidator.php | 1 + src/Pretix/EventHelper.php | 38 ++++----------- src/Pretix/OrderHelper.php | 4 +- tests/src/Functional/LoadTest.php | 1 + 21 files changed, 154 insertions(+), 137 deletions(-) create mode 100644 rector.php create mode 100755 scripts/rector diff --git a/README.md b/README.md index 8dbd63d..e68e3d1 100644 --- a/README.md +++ b/README.md @@ -68,30 +68,21 @@ docker compose run --rm node yarn encore production The code must follw the [Drupal Coding Standards](https://www.drupal.org/docs/develop/standards) -Check the coding standards by running +Apply and check the coding standards by running ```sh docker compose run --rm phpfpm composer install -docker compose run --rm phpfpm composer coding-standards-check -``` - -Apply the coding standards by running - -```sh docker compose run --rm phpfpm composer normalize docker compose run --rm phpfpm composer coding-standards-apply +docker compose run --rm phpfpm composer coding-standards-check ``` ### Assets -Check the coding standards in assets by running - -```sh -docker compose run --rm node yarn coding-standards-check -``` - -Apply the coding standards by running +Apply and check the coding standards in assets by running ```sh +docker compose run --rm node yarn install docker compose run --rm node yarn coding-standards-apply +docker compose run --rm node yarn coding-standards-check ``` diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..61dcf01 --- /dev/null +++ b/rector.php @@ -0,0 +1,24 @@ +withPaths([ + __DIR__ . '/src', + __DIR__ . '/tests', + ]) + ->withSkip([ + __DIR__ . '/src/ProxyClass', + ]) + ->withSets([ + Drupal10SetList::DRUPAL_10, + ]) + ->withPhpSets(php83: TRUE) + ->withTypeCoverageLevel(0); diff --git a/scripts/rector b/scripts/rector new file mode 100755 index 0000000..658d933 --- /dev/null +++ b/scripts/rector @@ -0,0 +1,13 @@ +#!/usr/bin/env bash +set -o errexit -o errtrace -o noclobber -o nounset -o pipefail +IFS=$'\n\t' + +script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +cd "$script_dir" + +execute() { + composer require --dev palantirnet/drupal-rector + shell vendor/bin/rector --config="$module_path/rector.php" +} + +source "$script_dir/base" diff --git a/src/Controller/PretixEventExportersController.php b/src/Controller/PretixEventExportersController.php index 37b3686..e4a937a 100644 --- a/src/Controller/PretixEventExportersController.php +++ b/src/Controller/PretixEventExportersController.php @@ -42,6 +42,7 @@ class PretixEventExportersController extends ControllerBase { /** * {@inheritdoc} */ + #[\Override] public static function create(ContainerInterface $container) { $instance = parent::create($container); $instance->eventHelper = $container->get('itk_pretix.event_helper'); @@ -71,12 +72,10 @@ public function index(Request $request, NodeInterface $node) { $enabled = $config->get('event_exporters_enabled'); $exporters = $this->exporterManager->getEventExporters($enabled); - $exporterForms = array_map(function (AbstractExporter $exporter) use ($node) { - return [ - 'name' => $exporter->getName(), - 'form' => $this->buildForm($node, $exporter), - ]; - }, $exporters); + $exporterForms = array_map(fn(AbstractExporter $exporter) => [ + 'name' => $exporter->getName(), + 'form' => $this->buildForm($node, $exporter), + ], $exporters); return [ '#theme' => 'itk_pretix_event_exporters', diff --git a/src/Controller/PretixWebhookController.php b/src/Controller/PretixWebhookController.php index 02d2ad4..9255e97 100644 --- a/src/Controller/PretixWebhookController.php +++ b/src/Controller/PretixWebhookController.php @@ -50,6 +50,7 @@ class PretixWebhookController extends ControllerBase { /** * {@inheritdoc} */ + #[\Override] public static function create(ContainerInterface $container) { $instance = parent::create($container); $instance->orderHelper = $container->get('itk_pretix.order_helper'); @@ -80,12 +81,10 @@ public function main(Request $request) { } $action = $payload['action'] ?? NULL; - switch ($action) { - case OrderHelper::PRETIX_EVENT_ORDER_PAID: - case OrderHelper::PRETIX_EVENT_ORDER_CANCELED: - $this->handleOrderUpdated($payload, $action); - break; - } + match ($action) { + OrderHelper::PRETIX_EVENT_ORDER_PAID, OrderHelper::PRETIX_EVENT_ORDER_CANCELED => $this->handleOrderUpdated($payload, $action), + default => new JsonResponse($payload), + }; return new JsonResponse($payload); } diff --git a/src/Exporter/AbstractExporter.php b/src/Exporter/AbstractExporter.php index 8108322..55a7435 100644 --- a/src/Exporter/AbstractExporter.php +++ b/src/Exporter/AbstractExporter.php @@ -44,6 +44,7 @@ abstract class AbstractExporter extends FormBase implements ExporterInterface { /** * {@inheritdoc} */ + #[\Override] public function getId() { if (NULL === static::$id) { throw new \RuntimeException(sprintf('Property id not defined in class %s', static::class)); @@ -54,6 +55,7 @@ public function getId() { /** * {@inheritdoc} */ + #[\Override] public function getName() { if (NULL === static::$name) { throw new \RuntimeException(sprintf('Property name not defined in class %s', static::class)); @@ -64,6 +66,7 @@ public function getName() { /** * {@inheritdoc} */ + #[\Override] public function getFormId() { return 'itk_pretix_exporter_' . $this->getId(); } @@ -71,6 +74,7 @@ public function getFormId() { /** * {@inheritdoc} */ + #[\Override] public function submitForm(array &$form, FormStateInterface $form_state) { throw new \RuntimeException(); } @@ -78,6 +82,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { /** * Process input parameters. */ + #[\Override] public function processInputParameters(array $parameters) { unset($parameters['op'], $parameters['form_build_id'], $parameters['form_token'], $parameters['form_id']); @@ -87,6 +92,7 @@ public function processInputParameters(array $parameters) { /** * Get pretix client. */ + #[\Override] public function setPretixClient(Client $client) { $this->client = $client; @@ -96,6 +102,7 @@ public function setPretixClient(Client $client) { /** * Set event info. */ + #[\Override] public function setEventInfo(array $eventInfo) { $this->eventInfo = $eventInfo; @@ -164,9 +171,7 @@ private function getCheckInLists() { if (!isset(self::$checkInLists[$event])) { $defaultName = 'exporter check-in list (do not delete)'; - $isDefaultList = static function (CheckInList $list) use ($defaultName) { - return $defaultName === $list->getName(); - }; + $isDefaultList = static fn(CheckInList $list) => $defaultName === $list->getName(); $checkInLists = $this->client->getCheckInLists($event); $defaultList = $checkInLists->filter($isDefaultList)->first(); if (!$defaultList) { @@ -179,9 +184,7 @@ private function getCheckInLists() { $checkInLists->add($defaultList); } - [$head, $tail] = $checkInLists->partition(static function ($key, $list) use ($isDefaultList) { - return $isDefaultList($list); - }); + [$head, $tail] = $checkInLists->partition(static fn($key, $list) => $isDefaultList($list)); // Make sure that the default list is first in collection. self::$checkInLists[$event] = new ArrayCollection( diff --git a/src/Exporter/CheckInListExporter.php b/src/Exporter/CheckInListExporter.php index d1d35b7..5bfd75f 100644 --- a/src/Exporter/CheckInListExporter.php +++ b/src/Exporter/CheckInListExporter.php @@ -21,6 +21,7 @@ class CheckInListExporter extends AbstractExporter { /** * {@inheritdoc} */ + #[\Override] public function buildForm(array $form, FormStateInterface $form_state) { $formats = [ 'xlsx' => $this->t('Excel (.xlsx)'), @@ -48,6 +49,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { /** * {@inheritdoc} */ + #[\Override] public function processInputParameters(array $parameters) { $parameters = parent::processInputParameters($parameters); // Make sure that questions is set. diff --git a/src/Exporter/CheckInListPdfExporter.php b/src/Exporter/CheckInListPdfExporter.php index 46310f8..c1443c2 100644 --- a/src/Exporter/CheckInListPdfExporter.php +++ b/src/Exporter/CheckInListPdfExporter.php @@ -21,6 +21,7 @@ class CheckInListPdfExporter extends CheckInListExporter { /** * {@inheritdoc} */ + #[\Override] public function buildForm(array $form, FormStateInterface $form_state) { $form = parent::buildForm($form, $form_state); unset($form['_format']); @@ -31,6 +32,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { /** * {@inheritdoc} */ + #[\Override] public function processInputParameters(array $parameters) { $parameters = parent::processInputParameters($parameters); // Make sure that questions is set. diff --git a/src/Exporter/Manager.php b/src/Exporter/Manager.php index 5a4349a..e4fc54b 100644 --- a/src/Exporter/Manager.php +++ b/src/Exporter/Manager.php @@ -16,7 +16,7 @@ * Exporter manager. */ class Manager implements ManagerInterface { - private const EXPORTER_RESULT_BASE_URL = 'private://itk_pretix/exporters'; + private const string EXPORTER_RESULT_BASE_URL = 'private://itk_pretix/exporters'; use StringTranslationTrait; @@ -47,6 +47,7 @@ public function __construct( /** * Add an event exporter. */ + #[\Override] public function addEventExporter(ExporterInterface $exporter, $priority = 0) { $this->eventExporters[$exporter->getId()] = $exporter; $this->eventExporterForms[$exporter->getFormId()] = $exporter; @@ -57,15 +58,15 @@ public function addEventExporter(ExporterInterface $exporter, $priority = 0) { /** * Get event exporters. */ + #[\Override] public function getEventExporters(?array $ids = NULL) { - return array_filter($this->eventExporters, static function (ExporterInterface $exporter) use ($ids) { - return NULL === $ids || in_array($exporter->getId(), $ids, TRUE); - }); + return array_filter($this->eventExporters, static fn(ExporterInterface $exporter) => NULL === $ids || in_array($exporter->getId(), $ids, TRUE)); } /** * Get event exporter. */ + #[\Override] public function getEventExporter(string $id) { return $this->eventExporters[$id] ?? NULL; } @@ -73,13 +74,14 @@ public function getEventExporter(string $id) { /** * Save exporter result to local file system. */ + #[\Override] public function saveExporterResult(NodeInterface $node, Response $response) { $header = $response->getHeaderLine('content-disposition'); if (preg_match('/filename="(?[^"]+)"/', $header, $matches)) { $filename = $matches['filename']; $url = $this->getExporterResultFileUrl($node, $filename); - $directory = dirname($url); + $directory = dirname((string) $url); $this->fileSystem->prepareDirectory($directory, FileSystem::CREATE_DIRECTORY); $filePath = $this->fileSystem->realpath($url); $this->fileSystem->saveData((string) $response->getBody(), $filePath, FileExists::Replace); @@ -97,6 +99,7 @@ public function saveExporterResult(NodeInterface $node, Response $response) { * @param string $uri * The file uri. */ + #[\Override] public function fileDownload(string $uri) { $info = $this->getExporterResultFileUrlInfo($uri); if (isset($info['nid'])) { diff --git a/src/Exporter/OrderListExporter.php b/src/Exporter/OrderListExporter.php index b79375a..1184572 100644 --- a/src/Exporter/OrderListExporter.php +++ b/src/Exporter/OrderListExporter.php @@ -21,6 +21,7 @@ class OrderListExporter extends AbstractExporter { /** * {@inheritdoc} */ + #[\Override] public function buildForm(array $form, FormStateInterface $form_state) { $formats = [ 'xlsx' => 'xlsx', diff --git a/src/Form/PretixConfigForm.php b/src/Form/PretixConfigForm.php index a227028..ae413f8 100644 --- a/src/Form/PretixConfigForm.php +++ b/src/Form/PretixConfigForm.php @@ -15,40 +15,23 @@ * A configuration form for the pretix module. */ final class PretixConfigForm extends ConfigFormBase { - /** - * The event helper. - * - * @var \Drupal\itk_pretix\Pretix\EventHelper - */ - private $eventHelper; - - /** - * The order helper. - * - * @var \Drupal\itk_pretix\Pretix\OrderHelper - */ - private $orderHelper; - - /** - * The exporter manager. - * - * @var ExporterManager - */ - private $exporterManager; /** * {@inheritDoc} */ - public function __construct(ConfigFactoryInterface $config_factory, EventHelper $eventHelper, OrderHelper $orderHelper, ExporterManagerInterface $exporterManager) { + public function __construct( + ConfigFactoryInterface $config_factory, + private readonly EventHelper $eventHelper, + private readonly OrderHelper $orderHelper, + private readonly ExporterManagerInterface $exporterManager, + ) { parent::__construct($config_factory); - $this->eventHelper = $eventHelper; - $this->orderHelper = $orderHelper; - $this->exporterManager = $exporterManager; } /** * {@inheritdoc} */ + #[\Override] public static function create(ContainerInterface $container) { return new static( $container->get('config.factory'), @@ -61,6 +44,7 @@ public static function create(ContainerInterface $container) { /** * {@inheritdoc} */ + #[\Override] protected function getEditableConfigNames() { return [ 'itk_pretix.pretixconfig', @@ -70,6 +54,7 @@ protected function getEditableConfigNames() { /** * {@inheritdoc} */ + #[\Override] public function getFormId() { return 'pretix_config_form'; } @@ -77,6 +62,7 @@ public function getFormId() { /** * {@inheritdoc} */ + #[\Override] public function buildForm(array $form, FormStateInterface $form_state) { $config = $this->config('itk_pretix.pretixconfig'); @@ -149,6 +135,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { /** * {@inheritdoc} */ + #[\Override] public function submitForm(array &$form, FormStateInterface $form_state) { parent::submitForm($form, $form_state); @@ -165,6 +152,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { /** * {@inheritdoc} */ + #[\Override] public function validateForm(array &$form, FormStateInterface $form_state) { parent::validateForm($form, $form_state); @@ -175,7 +163,7 @@ public function validateForm(array &$form, FormStateInterface $form_state) { 'api_token' => $form_state->getValue('api_token'), ]); } - catch (\Exception $exception) { + catch (\Exception) { $form_state->setErrorByName('pretix_url', $this->t('Cannot create pretix api client')); return; } @@ -183,19 +171,19 @@ public function validateForm(array &$form, FormStateInterface $form_state) { try { $client->getOrganizers(); } - catch (\Exception $exception) { + catch (\Exception) { $form_state->setErrorByName('pretix_url', $this->t('Cannot connect to pretix api')); return; } - $templateEventSlugs = array_unique(array_filter(array_map('trim', explode(PHP_EOL, $form_state->getValue('template_event_slugs'))))); + $templateEventSlugs = array_unique(array_filter(array_map('trim', explode(PHP_EOL, (string) $form_state->getValue('template_event_slugs'))))); $templateEvents = []; foreach ($templateEventSlugs as $eventSlug) { try { $event = $client->getEvent($eventSlug); $templateEvents[$event->getSlug()] = $event; } - catch (\Exception $exception) { + catch (\Exception) { } } @@ -231,7 +219,7 @@ public function validateForm(array &$form, FormStateInterface $form_state) { $this->orderHelper->ensureWebhook($client); $this->messenger->addStatus($this->t('pretix webhook created')); } - catch (\Exception $exception) { + catch (\Exception) { $form_state->setErrorByName('pretix_url', $this->t('Cannot create webhook in pretix')); return; } diff --git a/src/NodeHelper.php b/src/NodeHelper.php index 5f84063..194c270 100644 --- a/src/NodeHelper.php +++ b/src/NodeHelper.php @@ -117,7 +117,7 @@ public function getTemplateEvents(NodeInterface $node): ArrayCollection|Collecti $event = $client->getEvent($slug); $events[] = $event; } - catch (\Exception $exception) { + catch (\Exception) { } } diff --git a/src/Plugin/Field/FieldFormatter/PretixDateFormatter.php b/src/Plugin/Field/FieldFormatter/PretixDateFormatter.php index 89e6814..7ed0642 100644 --- a/src/Plugin/Field/FieldFormatter/PretixDateFormatter.php +++ b/src/Plugin/Field/FieldFormatter/PretixDateFormatter.php @@ -22,24 +22,26 @@ */ final class PretixDateFormatter extends FormatterBase { - /** - * Event helper. - * - * @var \Drupal\itk_pretix\Pretix\EventHelper - */ - private EventHelper $eventHelper; - /** * Class constructor. */ - public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EventHelper $eventHelper) { + public function __construct( + $plugin_id, + $plugin_definition, + FieldDefinitionInterface $field_definition, + array $settings, + $label, + $view_mode, + array $third_party_settings, + private readonly EventHelper $eventHelper, + ) { parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings); - $this->eventHelper = $eventHelper; } /** * {@inheritdoc} */ + #[\Override] public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static( $plugin_id, @@ -56,6 +58,7 @@ public static function create(ContainerInterface $container, array $configuratio /** * {@inheritdoc} */ + #[\Override] public function viewElements(FieldItemListInterface $items, $langcode) { $elements = []; @@ -67,9 +70,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { if (NULL !== $sortField) { // Sort ascending. - usort($dates, static function (PretixDate $a, PretixDate $b) use ($sortField) { - return $a->{$sortField} <=> $b->{$sortField}; - }); + usort($dates, static fn(PretixDate $a, PretixDate $b) => $a->{$sortField} <=> $b->{$sortField}); // Reverse if requested. if (0 === strcasecmp('desc', $sortDirection)) { $dates = array_reverse($dates); diff --git a/src/Plugin/Field/FieldType/PretixDate.php b/src/Plugin/Field/FieldType/PretixDate.php index 7a68206..ed448f8 100644 --- a/src/Plugin/Field/FieldType/PretixDate.php +++ b/src/Plugin/Field/FieldType/PretixDate.php @@ -35,6 +35,7 @@ class PretixDate extends FieldItemBase { /** * {@inheritdoc} */ + #[\Override] public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) { $properties['uuid'] = DataDefinition::create('string') ->setLabel(t('UUID')) @@ -87,6 +88,7 @@ public static function propertyDefinitions(FieldStorageDefinitionInterface $fiel /** * {@inheritdoc} */ + #[\Override] public static function schema(FieldStorageDefinitionInterface $field_definition) { $schema = [ 'columns' => [ @@ -140,6 +142,7 @@ public static function schema(FieldStorageDefinitionInterface $field_definition) /** * {@inheritdoc} */ + #[\Override] public function isEmpty() { $location = $this->get('location')->getValue(); $timeFrom = $this->get('time_from_value')->getValue(); @@ -150,6 +153,7 @@ public function isEmpty() { /** * {@inheritdoc} */ + #[\Override] public function preSave() { if (empty($this->get('uuid')->getValue())) { $this->get('uuid')->setValue(\Drupal::service('uuid')->generate()); @@ -163,7 +167,7 @@ public function preSave() { $this->addData(['coordinates' => $results[0]->adgangspunkt->koordinater]); } } - catch (\Exception $exception) { + catch (\Exception) { } } } diff --git a/src/Plugin/Field/FieldType/PretixEventSettings.php b/src/Plugin/Field/FieldType/PretixEventSettings.php index 64b5134..e233fc6 100644 --- a/src/Plugin/Field/FieldType/PretixEventSettings.php +++ b/src/Plugin/Field/FieldType/PretixEventSettings.php @@ -25,6 +25,7 @@ class PretixEventSettings extends FieldItemBase { /** * {@inheritdoc} */ + #[\Override] public static function propertyDefinitions( FieldStorageDefinitionInterface $field_definition, ) { @@ -41,6 +42,7 @@ public static function propertyDefinitions( /** * {@inheritdoc} */ + #[\Override] public static function schema( FieldStorageDefinitionInterface $field_definition, ) { @@ -61,6 +63,7 @@ public static function schema( /** * {@inheritdoc} */ + #[\Override] public function isEmpty() { return FALSE; } diff --git a/src/Plugin/Field/FieldWidget/PretixDateWidget.php b/src/Plugin/Field/FieldWidget/PretixDateWidget.php index 416c43c..23b9296 100644 --- a/src/Plugin/Field/FieldWidget/PretixDateWidget.php +++ b/src/Plugin/Field/FieldWidget/PretixDateWidget.php @@ -28,24 +28,24 @@ */ final class PretixDateWidget extends WidgetBase { - /** - * The event helper. - * - * @var \Drupal\itk_pretix\Pretix\EventHelper - */ - private EventHelper $eventHelper; - /** * Date widget constructor. */ - public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, EventHelper $eventHelper) { + public function __construct( + $plugin_id, + $plugin_definition, + FieldDefinitionInterface $field_definition, + array $settings, + array $third_party_settings, + private readonly EventHelper $eventHelper, + ) { parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings); - $this->eventHelper = $eventHelper; } /** * {@inheritdoc} */ + #[\Override] public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static( $plugin_id, @@ -60,12 +60,13 @@ public static function create(ContainerInterface $container, array $configuratio /** * {@inheritdoc} */ + #[\Override] public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { $eventHelper = $this->eventHelper; /** @var \Drupal\itk_pretix\Plugin\Field\FieldType\PretixDate $item */ $item = $items[$delta]; - $element['#element_validate'][] = [$this, 'validate']; + $element['#element_validate'][] = $this->validate(...); $element['#attributes']['class'][] = 'pretix-date-widget'; if ($this->hideEndDate()) { $element['#attributes']['class'][] = 'hide-end-date'; @@ -231,6 +232,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen /** * {@inheritdoc} */ + #[\Override] public function massageFormValues(array $values, array $form, FormStateInterface $form_state) { // The widget form element type has transformed the value to a // DrupalDateTime object at this point. We need to convert it back to the @@ -271,6 +273,7 @@ public function massageFormValues(array $values, array $form, FormStateInterface /** * {@inheritdoc} */ + #[\Override] public static function defaultSettings() { return [ 'hide_end_date' => FALSE, @@ -282,6 +285,7 @@ public static function defaultSettings() { /** * {@inheritdoc} */ + #[\Override] public function settingsForm(array $form, FormStateInterface $form_state) { $element['hide_end_date'] = [ '#title' => $this->t('Hide end date'), @@ -310,6 +314,7 @@ public function settingsForm(array $form, FormStateInterface $form_state) { /** * {@inheritdoc} */ + #[\Override] public function settingsSummary() { if ($this->hideEndDate()) { $summary[] = $this->t('Hide end date'); @@ -339,6 +344,7 @@ private function roundedTime($seconds) { /** * {@inheritdoc} */ + #[\Override] public function errorElement( array $element, ConstraintViolationInterface $error, diff --git a/src/Plugin/Field/FieldWidget/PretixEventSettingsWidget.php b/src/Plugin/Field/FieldWidget/PretixEventSettingsWidget.php index f8e5860..757084b 100644 --- a/src/Plugin/Field/FieldWidget/PretixEventSettingsWidget.php +++ b/src/Plugin/Field/FieldWidget/PretixEventSettingsWidget.php @@ -24,32 +24,25 @@ */ final class PretixEventSettingsWidget extends WidgetBase { - /** - * The event helper. - * - * @var \Drupal\itk_pretix\Pretix\EventHelper - */ - private EventHelper $eventHelper; - - /** - * Thee node helper. - * - * @var \Drupal\itk_pretix\NodeHelper - */ - private NodeHelper $nodeHelper; - /** * Date widget constructor. */ - public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, EventHelper $eventHelper, NodeHelper $nodeHelper) { + public function __construct( + $plugin_id, + $plugin_definition, + FieldDefinitionInterface $field_definition, + array $settings, + array $third_party_settings, + private readonly EventHelper $eventHelper, + private readonly NodeHelper $nodeHelper, + ) { parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings); - $this->eventHelper = $eventHelper; - $this->nodeHelper = $nodeHelper; } /** * {@inheritdoc} */ + #[\Override] public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static( $plugin_id, @@ -65,6 +58,7 @@ public static function create(ContainerInterface $container, array $configuratio /** * {@inheritdoc} */ + #[\Override] public function formElement( FieldItemListInterface $items, $delta, diff --git a/src/Plugin/Validation/Constraint/PretixDateConstraintValidator.php b/src/Plugin/Validation/Constraint/PretixDateConstraintValidator.php index 5c7ae82..fd222e6 100644 --- a/src/Plugin/Validation/Constraint/PretixDateConstraintValidator.php +++ b/src/Plugin/Validation/Constraint/PretixDateConstraintValidator.php @@ -14,6 +14,7 @@ class PretixDateConstraintValidator extends ConstraintValidator { /** * {@inheritdoc} */ + #[\Override] public function validate($item, Constraint $constraint) { if (!$item instanceof PretixDate || !$constraint instanceof PretixDateConstraint) { return; diff --git a/src/Pretix/EventHelper.php b/src/Pretix/EventHelper.php index d8ed420..e3f04b8 100644 --- a/src/Pretix/EventHelper.php +++ b/src/Pretix/EventHelper.php @@ -24,20 +24,6 @@ class EventHelper extends AbstractHelper { public const DATETIME_FORMAT = \DateTime::ATOM; - /** - * The order helper. - * - * @var \Drupal\itk_pretix\Pretix\OrderHelper - */ - private OrderHelper $orderHelper; - - /** - * The module handler interface. - * - * @var \Drupal\Core\Extension\ModuleHandlerInterface - */ - private ModuleHandlerInterface $moduleHandler; - /** * Constructor. * @@ -56,12 +42,10 @@ public function __construct( Connection $database, ConfigFactoryInterface $configFactory, LoggerChannelFactoryInterface $loggerFactory, - OrderHelper $orderHelper, - ModuleHandlerInterface $moduleHandler, + private readonly OrderHelper $orderHelper, + private readonly ModuleHandlerInterface $moduleHandler, ) { parent::__construct($database, $configFactory, $loggerFactory); - $this->orderHelper = $orderHelper; - $this->moduleHandler = $moduleHandler; } /** @@ -203,7 +187,7 @@ public function synchronizePretixSubEvents(Event $event, NodeInterface $node, Fi $pretixSubEventIds[] = $subEvent->getId(); } } - catch (\Exception $exception) { + catch (\Exception) { // @todo Do something clever here. } @@ -237,7 +221,7 @@ private function synchronizePretixSubEvent(PretixDate $item, Event $event, NodeI $templateSubEvents = $client->getSubEvents($templateEvent); } catch (\Exception $exception) { - throw $this->clientException($this->t('Cannot get template event sub-events'), $exception->getCode(), $exception); + throw $this->clientException($this->t('Cannot get template event sub-events'), $exception->getCode()); } if (0 === $templateSubEvents->count()) { throw $this->clientException($this->t('Cannot get template event sub-events')); @@ -388,9 +372,7 @@ private function synchronizePretixSubEvent(PretixDate $item, Event $event, NodeI $availabilities = $this->orderHelper ->setPretixClient($client) ->getSubEventAvailabilities($subEvent); - $subEventData['availability'] = $availabilities->map(static function (Quota $quota) { - return $quota->toArray(); - })->toArray(); + $subEventData['availability'] = $availabilities->map(static fn(Quota $quota) => $quota->toArray())->toArray(); } catch (\Exception $exception) { $exception->getCode(); @@ -523,7 +505,7 @@ public function validateTemplateEvent(Event $event, Client $client) { try { $subEvents = $client->getSubEvents($event); } - catch (\Exception $exception) { + catch (\Exception) { return [ 'event_slug' => t('Cannot get sub-events.'), ]; @@ -539,7 +521,7 @@ public function validateTemplateEvent(Event $event, Client $client) { try { $quotas = $client->getQuotas($event, ['subevent' => $subEvent->getId()]); } - catch (\Exception $exception) { + catch (\Exception) { return [ 'event_slug' => t('Cannot get sub-event quotas.'), ]; @@ -601,7 +583,7 @@ public function runExporter(NodeInterface $node, string $identifier, array $para throw new ExporterException($response->getReasonPhrase()); } - return json_decode($response->getBody(), TRUE); + return json_decode((string) $response->getBody(), TRUE); } /** @@ -668,7 +650,7 @@ private function getEventSlug(NodeInterface $node) { $template = $configuration['pretix_event_slug_template'] ?? '!nid'; // Make sure that node id is used in template. - if (FALSE === strpos($template, '!nid')) { + if (!str_contains((string) $template, '!nid')) { $template .= '-!nid'; } @@ -715,7 +697,7 @@ private function getLocation(PretixDate $item) { * * @throws \Exception */ - private function getDate($value) { + private function getDate(mixed $value) { if (NULL === $value) { return NULL; } diff --git a/src/Pretix/OrderHelper.php b/src/Pretix/OrderHelper.php index 1da0a9f..82d7127 100644 --- a/src/Pretix/OrderHelper.php +++ b/src/Pretix/OrderHelper.php @@ -154,7 +154,7 @@ private function indexCollection(Collection $collection, string $property) { * * @throws \Exception */ - public function getOrderLines($order) { + public function getOrderLines($order): never { throw new \RuntimeException(__METHOD__ . ' not implemented'); } @@ -164,7 +164,7 @@ public function getOrderLines($order) { * @param \Drupal\node\NodeInterface $node * The node. */ - public function getAvailability(NodeInterface $node) { + public function getAvailability(NodeInterface $node): never { throw new \RuntimeException(__METHOD__ . ' not implemented'); } diff --git a/tests/src/Functional/LoadTest.php b/tests/src/Functional/LoadTest.php index e6cc87a..653c5a3 100644 --- a/tests/src/Functional/LoadTest.php +++ b/tests/src/Functional/LoadTest.php @@ -36,6 +36,7 @@ class LoadTest extends BrowserTestBase { /** * {@inheritdoc} */ + #[\Override] protected function setUp(): void { parent::setUp(); $this->user = $this->drupalCreateUser(['administer site configuration']); From 16f6c515aa34f1b245f2bda4ca1437bd14588cfc Mon Sep 17 00:00:00 2001 From: Mikkel Ricky Date: Fri, 28 Feb 2025 00:08:41 +0100 Subject: [PATCH 6/8] Cleaned up use of services --- itk_pretix.info.yml | 3 +- itk_pretix.module | 27 +++++++-- itk_pretix.services.yml | 57 +++++-------------- .../PretixEventExportersController.php | 6 +- src/Controller/PretixWebhookController.php | 6 +- src/Exporter/Manager.php | 7 ++- src/Form/PretixConfigForm.php | 7 ++- .../FieldFormatter/PretixDateFormatter.php | 2 +- .../Field/FieldWidget/PretixDateWidget.php | 2 +- .../FieldWidget/PretixEventSettingsWidget.php | 5 +- 10 files changed, 59 insertions(+), 63 deletions(-) diff --git a/itk_pretix.info.yml b/itk_pretix.info.yml index 25a5ede..85990b6 100644 --- a/itk_pretix.info.yml +++ b/itk_pretix.info.yml @@ -1,7 +1,8 @@ name: 'ITK Pretix' type: module description: 'A module using pretix API to generate events from field values' -core_version_requirement: ^9 || ^10 +# We're autowiring services and need Drupal 10.2 or newer (cf. https://www.drupal.org/node/3366757) +core_version_requirement: ^10.2 package: 'ITK' dependencies: - drupal:field diff --git a/itk_pretix.module b/itk_pretix.module index fdd962a..d7b593f 100644 --- a/itk_pretix.module +++ b/itk_pretix.module @@ -6,6 +6,9 @@ */ use Drupal\Core\Routing\RouteMatchInterface; +use Drupal\itk_pretix\Exporter\Manager as ExporterManager; +use Drupal\itk_pretix\Exporter\ManagerInterface as ExporterManagerInterface; +use Drupal\itk_pretix\NodeHelper; use Drupal\node\NodeInterface; /** @@ -92,33 +95,47 @@ function _itk_pretix_process_datetime_element(array $element) { * Implements hook_node_insert(). */ function itk_pretix_node_insert(NodeInterface $node) { - \Drupal::service('itk_pretix.node_helper')->synchronizeEvent($node, 'insert'); + _itk_pretix_get_node_helper()->synchronizeEvent($node, 'insert'); } /** * Implements hook_node_update(). */ function itk_pretix_node_update(NodeInterface $node) { - \Drupal::service('itk_pretix.node_helper')->synchronizeEvent($node, 'update'); + _itk_pretix_get_node_helper()->synchronizeEvent($node, 'update'); } /** * Implements hook_node_delete(). */ function itk_pretix_node_delete(NodeInterface $node) { - \Drupal::service('itk_pretix.node_helper')->synchronizeEvent($node, 'delete'); + _itk_pretix_get_node_helper()->synchronizeEvent($node, 'delete'); } /** * Implements hook_cloned_node_alter(). */ function itk_pretix_cloned_node_alter(NodeInterface $node) { - \Drupal::service('itk_pretix.node_helper')->clonedNodeAlter($node); + _itk_pretix_get_node_helper()->clonedNodeAlter($node); } /** * Implements hook_file_download(). */ function itk_pretix_file_download($uri) { - return \Drupal::service('itk_pretix.exporter_manager')->fileDownload($uri); + return _itk_pretix_get_exporter_manager()->fileDownload($uri); +} + +/** + * Get node helper. + */ +function _itk_pretix_get_node_helper(): NodeHelper { + return \Drupal::service(NodeHelper::class); +} + +/** + * Get exporter manager. + */ +function _itk_pretix_get_exporter_manager(): ExporterManagerInterface { + return \Drupal::service(ExporterManager::class); } diff --git a/itk_pretix.services.yml b/itk_pretix.services.yml index cfa301d..9502d54 100644 --- a/itk_pretix.services.yml +++ b/itk_pretix.services.yml @@ -1,61 +1,34 @@ services: - itk_pretix.node_helper: - class: Drupal\itk_pretix\NodeHelper - arguments: - - '@itk_pretix.event_helper' - - '@entity_type.manager' - - '@module_handler' - - '@messenger' - - itk_pretix.event_helper: - class: Drupal\itk_pretix\Pretix\EventHelper - arguments: - - '@database' - - '@config.factory' - - '@logger.factory' - - '@itk_pretix.order_helper' - - '@module_handler' - - itk_pretix.order_helper: - class: Drupal\itk_pretix\Pretix\OrderHelper - arguments: - - '@database' - - '@config.factory' - - '@logger.factory' - - itk_pretix.exporter_manager: - class: Drupal\itk_pretix\Exporter\Manager + # https://www.drupal.org/node/3366757 + _defaults: + autowire: true + + Drupal\itk_pretix\NodeHelper: + + Drupal\itk_pretix\Pretix\EventHelper: + + Drupal\itk_pretix\Pretix\OrderHelper: + + Drupal\itk_pretix\Exporter\Manager: lazy: true - arguments: - - '@file_system' - - '@itk_pretix.access_check' - - '@current_user' - - '@file_url_generator' tags: - { name: service_collector, tag: itk_pretix_exporter, call: addEventExporter } - itk_pretix.exporter.itk_pretix_exporter_check_in_list: - class: Drupal\itk_pretix\Exporter\CheckInListExporter + Drupal\itk_pretix\Exporter\CheckInListExporter: lazy: true tags: - { name: itk_pretix_exporter } - itk_pretix.exporter.itk_pretix_exporter_check_in_list_pdf: - class: Drupal\itk_pretix\Exporter\CheckInListPdfExporter + Drupal\itk_pretix\Exporter\CheckInListPdfExporter: lazy: true tags: - { name: itk_pretix_exporter } - itk_pretix.exporter.itk_pretix_exporter_orderlist: - class: Drupal\itk_pretix\Exporter\OrderListExporter + Drupal\itk_pretix\Exporter\OrderListExporter: lazy: true tags: - { name: itk_pretix_exporter } - itk_pretix.access_check: - class: Drupal\itk_pretix\Access\AccessCheck - arguments: - - '@current_user' - - '@current_route_match' + Drupal\itk_pretix\Access\AccessCheck: tags: - { name: access_check, applies_to: _itk_pretix_export_access_check } diff --git a/src/Controller/PretixEventExportersController.php b/src/Controller/PretixEventExportersController.php index e4a937a..7227a0f 100644 --- a/src/Controller/PretixEventExportersController.php +++ b/src/Controller/PretixEventExportersController.php @@ -7,12 +7,14 @@ use Drupal\Core\Url; use Drupal\itk_pretix\Exception\ExporterException; use Drupal\itk_pretix\Exporter\AbstractExporter; +use Drupal\itk_pretix\Pretix\EventHelper; use Drupal\node\NodeInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; +use Drupal\itk_pretix\Exporter\Manager as ExporterManager; /** * Pretix controller. @@ -45,8 +47,8 @@ class PretixEventExportersController extends ControllerBase { #[\Override] public static function create(ContainerInterface $container) { $instance = parent::create($container); - $instance->eventHelper = $container->get('itk_pretix.event_helper'); - $instance->exporterManager = $container->get('itk_pretix.exporter_manager'); + $instance->eventHelper = $container->get(EventHelper::class); + $instance->exporterManager = $container->get(ExporterManager::class); $instance->setMessenger($container->get('messenger')); $instance->session = $container->get('session'); diff --git a/src/Controller/PretixWebhookController.php b/src/Controller/PretixWebhookController.php index 9255e97..faaa181 100644 --- a/src/Controller/PretixWebhookController.php +++ b/src/Controller/PretixWebhookController.php @@ -53,9 +53,9 @@ class PretixWebhookController extends ControllerBase { #[\Override] public static function create(ContainerInterface $container) { $instance = parent::create($container); - $instance->orderHelper = $container->get('itk_pretix.order_helper'); - $instance->nodeHelper = $container->get('itk_pretix.node_helper'); - $instance->eventHelper = $container->get('itk_pretix.event_helper'); + $instance->orderHelper = $container->get(OrderHelper::class); + $instance->nodeHelper = $container->get(NodeHelper::class); + $instance->eventHelper = $container->get(EventHelper::class); $instance->database = $container->get('database'); return $instance; diff --git a/src/Exporter/Manager.php b/src/Exporter/Manager.php index e4fc54b..803fe2e 100644 --- a/src/Exporter/Manager.php +++ b/src/Exporter/Manager.php @@ -4,7 +4,8 @@ use Drupal\Core\File\FileExists; use Drupal\Core\File\FileSystem; -use Drupal\Core\File\FileUrlGenerator; +use Drupal\Core\File\FileSystemInterface; +use Drupal\Core\File\FileUrlGeneratorInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\itk_pretix\Access\AccessCheck; @@ -38,10 +39,10 @@ class Manager implements ManagerInterface { * Constructor. */ public function __construct( - private readonly FileSystem $fileSystem, + private readonly FileSystemInterface $fileSystem, private readonly AccessCheck $accessCheck, private readonly AccountInterface $currentUser, - private readonly FileUrlGenerator $fileUrlGenerator, + private readonly FileUrlGeneratorInterface $fileUrlGenerator, ) {} /** diff --git a/src/Form/PretixConfigForm.php b/src/Form/PretixConfigForm.php index ae413f8..ca1ca14 100644 --- a/src/Form/PretixConfigForm.php +++ b/src/Form/PretixConfigForm.php @@ -8,6 +8,7 @@ use Drupal\itk_pretix\Exporter\ManagerInterface as ExporterManagerInterface; use Drupal\itk_pretix\Pretix\EventHelper; use Drupal\itk_pretix\Pretix\OrderHelper; +use Drupal\itk_pretix\Exporter\Manager as ExporterManager; use ItkDev\Pretix\Api\Client; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -35,9 +36,9 @@ public function __construct( public static function create(ContainerInterface $container) { return new static( $container->get('config.factory'), - $container->get('itk_pretix.event_helper'), - $container->get('itk_pretix.order_helper'), - $container->get('itk_pretix.exporter_manager') + $container->get(EventHelper::class), + $container->get(OrderHelper::class), + $container->get(ExporterManager::class) ); } diff --git a/src/Plugin/Field/FieldFormatter/PretixDateFormatter.php b/src/Plugin/Field/FieldFormatter/PretixDateFormatter.php index 7ed0642..5ac372d 100644 --- a/src/Plugin/Field/FieldFormatter/PretixDateFormatter.php +++ b/src/Plugin/Field/FieldFormatter/PretixDateFormatter.php @@ -51,7 +51,7 @@ public static function create(ContainerInterface $container, array $configuratio $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], - $container->get('itk_pretix.event_helper') + $container->get(EventHelper::class) ); } diff --git a/src/Plugin/Field/FieldWidget/PretixDateWidget.php b/src/Plugin/Field/FieldWidget/PretixDateWidget.php index 23b9296..34ffac1 100644 --- a/src/Plugin/Field/FieldWidget/PretixDateWidget.php +++ b/src/Plugin/Field/FieldWidget/PretixDateWidget.php @@ -53,7 +53,7 @@ public static function create(ContainerInterface $container, array $configuratio $configuration['field_definition'], $configuration['settings'], $configuration['third_party_settings'], - $container->get('itk_pretix.event_helper') + $container->get(EventHelper::class) ); } diff --git a/src/Plugin/Field/FieldWidget/PretixEventSettingsWidget.php b/src/Plugin/Field/FieldWidget/PretixEventSettingsWidget.php index 757084b..7fd54c8 100644 --- a/src/Plugin/Field/FieldWidget/PretixEventSettingsWidget.php +++ b/src/Plugin/Field/FieldWidget/PretixEventSettingsWidget.php @@ -2,6 +2,7 @@ namespace Drupal\itk_pretix\Plugin\Field\FieldWidget; +use Drupal\Core\Database\Exception\EventException; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\WidgetBase; @@ -50,8 +51,8 @@ public static function create(ContainerInterface $container, array $configuratio $configuration['field_definition'], $configuration['settings'], $configuration['third_party_settings'], - $container->get('itk_pretix.event_helper'), - $container->get('itk_pretix.node_helper') + $container->get(EventHelper::class), + $container->get(NodeHelper::class) ); } From 34af13a5d57e1fece6a7b5c6647e8a1ad472e669 Mon Sep 17 00:00:00 2001 From: Mikkel Ricky Date: Fri, 28 Feb 2025 00:35:21 +0100 Subject: [PATCH 7/8] Updated GitHub Actions --- .github/workflows/pr.yaml | 132 ++++++++++++++------------------------ CHANGELOG.md | 2 + compose.yaml | 1 + composer.json | 6 -- 4 files changed, 52 insertions(+), 89 deletions(-) diff --git a/.github/workflows/pr.yaml b/.github/workflows/pr.yaml index 1091c55..8037f3f 100644 --- a/.github/workflows/pr.yaml +++ b/.github/workflows/pr.yaml @@ -1,5 +1,10 @@ on: pull_request + name: Review + +env: + COMPOSE_USER: runner + jobs: changelog: runs-on: ubuntu-latest @@ -8,7 +13,7 @@ jobs: fail-fast: false steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v4 with: fetch-depth: 2 @@ -17,94 +22,55 @@ jobs: - name: Check that changelog has been updated. run: git diff --exit-code origin/${{ github.base_ref }} -- CHANGELOG.md && exit 1 || exit 0 - test-composer-files: - name: Validate composer + + composer-validate: runs-on: ubuntu-latest - strategy: - matrix: - php-versions: [ '8.1' ] - dependency-version: [ prefer-lowest, prefer-stable ] steps: - - uses: actions/checkout@master - - name: Setup PHP, with composer and extensions - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php-versions }} - extensions: json - coverage: none - tools: composer:v2 - # https://github.com/shivammathur/setup-php#cache-composer-dependencies - - name: Get composer cache directory - id: composer-cache - run: echo "::set-output name=dir::$(composer config cache-files-dir)" - - name: Cache dependencies - uses: actions/cache@v2 - with: - path: ${{ steps.composer-cache.outputs.dir }} - key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} - restore-keys: ${{ runner.os }}-composer- + - uses: actions/checkout@v4 - name: Validate composer files run: | - composer validate --strict composer.json - # Check that dependencies resolve. - composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction - php-check-coding-standards: - name: PHP - Check Coding Standards + docker compose run --rm phpfpm composer validate + - name: Check that dependencies resolve + run: | + docker compose run --rm phpfpm composer update + + check-composer-normalize: runs-on: ubuntu-latest - strategy: - matrix: - php-versions: [ '8.1' ] - dependency-version: [ prefer-lowest, prefer-stable ] steps: - - uses: actions/checkout@master - - name: Setup PHP, with composer and extensions - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php-versions }} - extensions: json - coverage: none - tools: composer:v2 - # https://github.com/shivammathur/setup-php#cache-composer-dependencies - - name: Get composer cache directory - id: composer-cache - run: echo "::set-output name=dir::$(composer config cache-files-dir)" - - name: Cache dependencies - uses: actions/cache@v2 - with: - path: ${{ steps.composer-cache.outputs.dir }} - key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} - restore-keys: ${{ runner.os }}-composer- - - name: Install Dependencies - run: | - composer install --no-interaction --no-progress - - name: PHPCS - run: | - composer coding-standards-check/phpcs - php-code-analysis: - name: PHP code analysis + - uses: actions/checkout@v4 + - run: | + docker compose run --rm phpfpm composer install + docker compose run --rm phpfpm composer normalize --dry-run + + coding-standards-check-phpcs: runs-on: ubuntu-latest - strategy: - matrix: - php-versions: [ '8.1' ] steps: - - uses: actions/checkout@master - - name: Setup PHP, with composer and extensions - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php-versions }} - extensions: json - coverage: none - tools: composer:v2 - # https://github.com/shivammathur/setup-php#cache-composer-dependencies - - name: Get composer cache directory - id: composer-cache - run: echo "::set-output name=dir::$(composer config cache-files-dir)" - - name: Cache dependencies - uses: actions/cache@v2 - with: - path: ${{ steps.composer-cache.outputs.dir }} - key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} - restore-keys: ${{ runner.os }}-composer- - - name: Code analysis - run: | + - uses: actions/checkout@v4 + - run: | + docker compose run --rm phpfpm composer install + docker compose run --rm phpfpm composer coding-standards-check/phpcs + + code-analysis: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - run: | ./scripts/code-analysis + + proxy-classes: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - run: | + ./scripts/generate-proxy-classes + - name: Check nothing has changed + run: git diff --exit-code + + rector: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - run: | + ./scripts/rector + - name: Check nothing has changed + run: git diff --exit-code diff --git a/CHANGELOG.md b/CHANGELOG.md index 55125d9..9f9c0f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ before starting to add changes. ## [Unreleased] +* [PR-70](https://github.com/itk-dev/itk_pretix/pull/70) + Cleaned up and refactored Code * [PR-69](https://github.com/itk-dev/itk_pretix/pull/69) Added “Registration deadline” to date diff --git a/compose.yaml b/compose.yaml index 6e55e8d..fcd57b0 100644 --- a/compose.yaml +++ b/compose.yaml @@ -1,6 +1,7 @@ services: phpfpm: image: itkdev/php8.3-fpm:latest + user: ${COMPOSE_USER:-deploy} profiles: - dev volumes: diff --git a/composer.json b/composer.json index 0a2df0c..0ab9d7c 100644 --- a/composer.json +++ b/composer.json @@ -36,12 +36,6 @@ } }, "scripts": { - "code-analysis": [ - "@code-analysis/phpstan" - ], - "code-analysis/phpstan": [ - "phpstan analyse --configuration=phpstan.neon" - ], "coding-standards-apply": [ "@coding-standards-apply/phpcs" ], From 7aadcdfaf82a2f72c5c246f2150a69db75347823 Mon Sep 17 00:00:00 2001 From: Mikkel Ricky Date: Fri, 28 Feb 2025 01:01:18 +0100 Subject: [PATCH 8/8] Cleaned up --- src/Plugin/Field/FieldWidget/PretixEventSettingsWidget.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Plugin/Field/FieldWidget/PretixEventSettingsWidget.php b/src/Plugin/Field/FieldWidget/PretixEventSettingsWidget.php index 7fd54c8..7063889 100644 --- a/src/Plugin/Field/FieldWidget/PretixEventSettingsWidget.php +++ b/src/Plugin/Field/FieldWidget/PretixEventSettingsWidget.php @@ -2,7 +2,6 @@ namespace Drupal\itk_pretix\Plugin\Field\FieldWidget; -use Drupal\Core\Database\Exception\EventException; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\WidgetBase;