diff --git a/.github/workflows/recipe.yaml b/.github/workflows/recipe.yaml index 6db3fa6..1555c93 100644 --- a/.github/workflows/recipe.yaml +++ b/.github/workflows/recipe.yaml @@ -9,24 +9,20 @@ jobs: recipe: - name: Flex recipe (PHP ${{ matrix.php }}, Sylius ${{ matrix.sylius }}) - runs-on: ubuntu-latest strategy: fail-fast: false matrix: - php: ['7.4' ,'8.0', '8.1'] - sylius: [ "~1.9.0", "~1.10.0", "~1.11.0", "~1.12.0"] + php: ['8.0', '8.1', '8.2'] + sylius: ["~1.11.0", "~1.12.0", "~1.13.0"] exclude: - - php: 8.1 - sylius: "~1.9.0" - - php: 8.0 - sylius: "~1.9.0" - - php: 7.4 - sylius: "~1.11.0" - - php: 7.4 - sylius: "~1.12.0" + - php: '8.2' + sylius: '~1.11.0' + - php: '8.0' + sylius: '~1.12.0' + - php: '8.0' + sylius: '~1.13.0' steps: - name: Setup PHP @@ -35,6 +31,7 @@ jobs: php-version: ${{ matrix.php }} extensions: gd, intl, json ini-values: date.timezone=UTC + tools: symfony-cli - name: Set project php-version run: | @@ -76,13 +73,13 @@ jobs: - name: Setup some requirements working-directory: ./sylius run: | - composer config --no-plugins allow-plugins true - composer config --no-plugins extra.symfony.allow-contrib true - composer config --no-plugins secure-http false - composer config --no-plugins --unset platform.php - composer config --no-plugins extra.symfony.docker false - composer config --no-plugins --json extra.symfony.endpoint '["https://api.github.com/repos/monsieurbiz/symfony-recipes/contents/index.json?ref=flex/master","flex://defaults"]' - composer config repositories.plugin '{"type": "path", "url": "../plugin/"}' + composer config --no-plugins allow-plugins true + composer config --no-plugins extra.symfony.allow-contrib true + composer config --no-plugins secure-http false + composer config --no-plugins --unset platform.php + composer config --no-plugins extra.symfony.docker false + composer config --no-plugins --json extra.symfony.endpoint '["https://api.github.com/repos/monsieurbiz/symfony-recipes/contents/index.json?ref=flex/master","flex://defaults"]' + composer config repositories.plugin '{"type": "path", "url": "../plugin/"}' - name: Require plugin & install all dependencies working-directory: ./sylius diff --git a/.github/workflows/security.yaml b/.github/workflows/security.yaml index 4e12805..c100cfa 100644 --- a/.github/workflows/security.yaml +++ b/.github/workflows/security.yaml @@ -8,14 +8,12 @@ jobs: security: - name: Security check (PHP ${{ matrix.php }}) - runs-on: ubuntu-latest strategy: fail-fast: false matrix: - php: ['7.4', '8.0', '8.1'] + php: ['8.1', '8.2'] steps: - uses: actions/checkout@v3 @@ -23,12 +21,12 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: ${{ matrix.php }} - ini-values: date.timezone=UTC + php-version: ${{ matrix.php }} + extensions: gd, intl, json - name: Set project php-version run: | - echo ${{ matrix.php }} > .php-version + echo "${{ matrix.php }}" > .php-version - name: Determine composer cache directory id: composer-cache-directory @@ -39,7 +37,7 @@ jobs: id: cache-composer with: path: ${{ steps.composer-cache-directory.outputs.directory }} - key: composer2-php:${{ matrix.php }}-${{ hashFiles('**/composer.json') }} + key: composer2-php:${{ matrix.php }}-${{ github.sha }} restore-keys: composer2-php:${{ matrix.php }}- - name: Ensure that composer cache directory exists @@ -52,3 +50,4 @@ jobs: run: composer update --prefer-dist - uses: symfonycorp/security-checker-action@v4 + diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 4bf7505..877ce6a 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -9,14 +9,12 @@ jobs: php: - name: Quality tests (PHP ${{ matrix.php }}) - runs-on: ubuntu-latest strategy: fail-fast: false matrix: - php: ['8.0', '8.1'] + php: ['8.1', '8.2'] env: SYMFONY_ARGS: --no-tls @@ -24,26 +22,21 @@ jobs: DOCKER_INTERACTIVE_ARGS: -t steps: - - uses: actions/checkout@v3 - - - uses: actions/setup-node@v3 + - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 with: node-version: '14' - - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: ${{ matrix.php }} - ini-values: date.timezone=UTC + php-version: ${{ matrix.php }} + extensions: gd, intl, json + ini-values: date.timezone=UTC + tools: symfony-cli - name: Set project php-version run: | - echo ${{ matrix.php }} > .php-version - - - name: Install symfony CLI - run: | - curl -1sLf 'https://dl.cloudsmith.io/public/symfony/stable/setup.deb.sh' | sudo -E bash - sudo apt install symfony-cli + echo "${{ matrix.php }}" > .php-version - name: Determine composer cache directory id: composer-cache-directory @@ -54,7 +47,7 @@ jobs: id: cache-composer with: path: ${{ steps.composer-cache-directory.outputs.directory }} - key: composer2-php:${{ matrix.php }}-${{ hashFiles('**/composer.json') }} + key: composer2-php:${{ matrix.php }}-${{ github.sha }} restore-keys: composer2-php:${{ matrix.php }}- - name: Ensure that composer cache directory exists diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index dc4289f..92da0ef 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -49,10 +49,8 @@ 'binary_operator_spaces' => true, 'blank_line_after_opening_tag' => true, 'blank_line_after_namespace' => true, + 'blank_lines_before_namespace' => true, 'blank_line_before_statement' => true, - 'braces' => [ - 'allow_single_line_closure' => true, - ], 'cast_spaces' => true, 'class_attributes_separation' => true, 'class_definition' => [ @@ -62,7 +60,7 @@ 'combine_consecutive_issets' => true, 'combine_consecutive_unsets' => true, 'comment_to_phpdoc' => true, - 'compact_nullable_typehint' => true, + 'compact_nullable_type_declaration' => true, 'concat_space' => [ 'spacing' => 'one', ], @@ -88,8 +86,12 @@ 'fully_qualified_strict_types' => true, 'function_declaration' => true, 'function_to_constant' => true, - 'function_typehint_space' => true, 'general_phpdoc_tag_rename' => true, + 'global_namespace_import' => [ + 'import_classes' => true, + 'import_constants' => false, + 'import_functions' => false, + ], 'header_comment' => [ 'header' => $header, 'location' => 'after_open', @@ -110,6 +112,7 @@ 'lowercase_static_reference' => true, 'magic_constant_casing' => true, 'method_argument_space' => true, + 'modernize_strpos' => false, 'modernize_types_casting' => true, 'multiline_comment_opening_closing' => true, 'multiline_whitespace_before_semicolons' => [ @@ -117,7 +120,7 @@ ], 'native_constant_invocation' => true, 'native_function_casing' => true, - 'new_with_braces' => true, + 'new_with_parentheses' => true, 'no_alias_functions' => true, 'no_alternative_syntax' => true, 'no_blank_lines_after_class_opening' => true, @@ -156,27 +159,27 @@ 'no_short_bool_cast' => true, 'no_spaces_after_function_name' => true, 'no_spaces_around_offset' => true, - 'no_spaces_inside_parenthesis' => true, + 'spaces_inside_parentheses' => true, 'no_superfluous_elseif' => true, 'no_superfluous_phpdoc_tags' => [ 'allow_mixed' => true, ], 'no_unset_cast' => true, 'no_unneeded_control_parentheses' => true, - 'no_unneeded_curly_braces' => true, + 'no_unneeded_braces' => true, 'no_unneeded_final_method' => true, 'no_unset_on_property' => true, 'no_unused_imports' => true, 'no_useless_else' => true, 'no_useless_return' => true, - 'no_trailing_comma_in_list_call' => true, - 'no_trailing_comma_in_singleline_array' => true, + 'no_trailing_comma_in_singleline' => true, 'no_trailing_whitespace' => true, 'no_trailing_whitespace_in_comment' => true, 'no_whitespace_before_comma_in_array' => true, 'no_whitespace_in_blank_line' => true, 'non_printable_character' => true, 'normalize_index_brace' => true, + 'nullable_type_declaration_for_default_null_value' => false, 'object_operator_without_whitespace' => true, 'ordered_imports' => [ 'imports_order' => [ @@ -211,7 +214,9 @@ 'phpdoc_order' => true, 'phpdoc_return_self_reference' => true, 'phpdoc_scalar' => true, - 'phpdoc_separation' => true, + 'phpdoc_separation' => ['groups' => [ + ['ORM\\*'], ['Assert\\*'], + ]], 'phpdoc_single_line_var_spacing' => true, 'phpdoc_tag_type' => true, 'phpdoc_to_comment' => false, @@ -231,7 +236,6 @@ 'self_accessor' => true, 'short_scalar_cast' => true, 'single_blank_line_at_eof' => true, - 'single_blank_line_before_namespace' => true, 'single_class_element_per_statement' => true, 'single_import_per_statement' => true, 'single_line_after_imports' => true, @@ -248,6 +252,7 @@ 'elements' => ['arrays'], ], 'trim_array_spaces' => true, + 'type_declaration_spaces' => true, 'unary_operator_spaces' => true, 'visibility_required' => [ 'elements' => [ diff --git a/Makefile b/Makefile index 4004253..e4775e2 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,15 @@ .DEFAULT_GOAL := help SHELL=/bin/bash APP_DIR=tests/Application -SYLIUS_VERSION=1.12.0 +SYLIUS_VERSION=1.13.0 SYMFONY=cd ${APP_DIR} && symfony COMPOSER=symfony composer CONSOLE=${SYMFONY} console export COMPOSE_PROJECT_NAME=sales-reports +export MIGRATIONS_NAMESPACE=MonsieurBiz\\SyliusSalesReportsPlugin\\Migrations +export USER_UID=$(shell id -u) PLUGIN_NAME=sylius-${COMPOSE_PROJECT_NAME}-plugin -COMPOSE=docker-compose +COMPOSE=docker compose YARN=yarn ### @@ -76,13 +78,15 @@ setup_application: $(MAKE) ${APP_DIR}/php.ini (cd ${APP_DIR} && ${COMPOSER} install --no-interaction) $(MAKE) apply_dist - (cd ${APP_DIR} && ${COMPOSER} require --no-progress monsieurbiz/${PLUGIN_NAME}="*@dev") + (cd ${APP_DIR} && ${COMPOSER} require --no-progress --no-interaction monsieurbiz/${PLUGIN_NAME}="*@dev") rm -rf ${APP_DIR}/var/cache ${APP_DIR}/docker-compose.yaml: rm -f ${APP_DIR}/docker-compose.yml rm -f ${APP_DIR}/docker-compose.yaml + rm -f ${APP_DIR}/compose.yml # Remove Sylius file about Docker + rm -f ${APP_DIR}/compose.override.dist.yml # Remove Sylius file about Docker ln -s ../../docker-compose.yaml.dist ${APP_DIR}/docker-compose.yaml .PHONY: ${APP_DIR}/docker-compose.yaml @@ -113,10 +117,10 @@ test.composer: ## Validate composer.json ${COMPOSER} validate --strict test.phpstan: ## Run PHPStan - ${COMPOSER} phpstan || true + ${COMPOSER} phpstan test.phpmd: ## Run PHPMD - ${COMPOSER} phpmd || true + ${COMPOSER} phpmd test.phpunit: ## Run PHPUnit ${COMPOSER} phpunit @@ -134,7 +138,7 @@ test.container: ## Lint the symfony container ${CONSOLE} lint:container test.yaml: ## Lint the symfony Yaml files - ${CONSOLE} lint:yaml ../../recipes ../../src/Resources/config + ${CONSOLE} lint:yaml ../../src/Resources/config --parse-tags test.schema: ## Validate MySQL Schema ${CONSOLE} doctrine:schema:validate @@ -165,6 +169,9 @@ sylius.assets: ## Install all assets with symlinks messenger.setup: ## Setup Messenger transports ${CONSOLE} messenger:setup-transports +doctrine.diff: ## Doctrine diff + ${CONSOLE} doctrine:migration:diff --namespace="${MIGRATIONS_NAMESPACE}" + ### ### PLATFORM ### ¯¯¯¯¯¯¯¯ diff --git a/README.md b/README.md index 0247337..d99ff0d 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,33 @@ [![Banner of Sylus Sales Reports plugin](docs/images/banner.jpg)](https://monsieurbiz.com/agence-web-experte-sylius) -

Sales Reports

+

Sylius Sales Reports

[![Sales Reports Plugin license](https://img.shields.io/github/license/monsieurbiz/SyliusSalesReportsPlugin?public)](https://github.com/monsieurbiz/SyliusSalesReportsPlugin/blob/master/LICENSE.txt) -[![Tests Status](https://img.shields.io/github/workflow/status/monsieurbiz/SyliusSalesReportsPlugin/Tests?logo=github)](https://github.com/monsieurbiz/SyliusSalesReportsPlugin/actions?query=workflow%3ATests) -[![Security Status](https://img.shields.io/github/workflow/status/monsieurbiz/SyliusSalesReportsPlugin/Security?label=security&logo=github)](https://github.com/monsieurbiz/SyliusSalesReportsPlugin/actions?query=workflow%3ASecurity) +[![Tests Status](https://img.shields.io/github/actions/workflow/status/monsieurbiz/SyliusSalesReportsPlugin/tests.yaml?branch=master&logo=github)](https://github.com/monsieurbiz/SyliusSalesReportsPlugin/actions?query=workflow%3ATests) +[![Recipe Status](https://img.shields.io/github/actions/workflow/status/monsieurbiz/SyliusSalesReportsPlugin/recipe.yaml?branch=master&label=recipes&logo=github)](https://github.com/monsieurbiz/SyliusSalesReportsPlugin/actions?query=workflow%3ASecurity) +[![Security Status](https://img.shields.io/github/actions/workflow/status/monsieurbiz/SyliusSalesReportsPlugin/security.yaml?branch=master&label=security&logo=github)](https://github.com/monsieurbiz/SyliusSalesReportsPlugin/actions?query=workflow%3ASecurity) + A simple plugin to have sales reports in Sylius ![Reports form](screenshots/reports_form.png) +## Compatibility + +| Sylius Version | PHP Version | +|---|---| +| 1.11 | 8.0 - 8.1 | +| 1.12 | 8.1 - 8.2 | +| 1.13 | 8.1 - 8.2 | + ## Installation +If you want to use our recipes, you can configure your composer.json by running: + +```bash +composer config --no-plugins --json extra.symfony.endpoint '["https://api.github.com/repos/monsieurbiz/symfony-recipes/contents/index.json?ref=flex/master","flex://defaults"]' +``` + ```bash composer require monsieurbiz/sylius-sales-reports-plugin ``` diff --git a/composer.json b/composer.json index 8e5cca6..0a23383 100644 --- a/composer.json +++ b/composer.json @@ -5,43 +5,17 @@ "description": "A simple plugin to have sales reports in Sylius", "license": "MIT", "require": { - "php": "~7.4|~8.0", - "sylius/sylius": ">=1.8 <1.13" + "php": "^8.0", + "sylius/sylius": ">=1.11 <1.14" }, "require-dev": { - "behat/behat": "^3.6.1", - "behat/mink-selenium2-driver": "^1.4", - "dmore/behat-chrome-extension": "^1.3", - "dmore/chrome-mink-driver": "^2.7", - "doctrine/data-fixtures": "^1.4", - "ergebnis/composer-normalize": "^2.5", - "friends-of-behat/mink": "^1.8", - "friends-of-behat/mink-browserkit-driver": "^1.4", - "friends-of-behat/mink-extension": "^2.4", - "friends-of-behat/page-object-extension": "^0.3", - "friends-of-behat/symfony-extension": "^2.1", - "friends-of-behat/variadic-extension": "^1.3", - "hwi/oauth-bundle": "^1.1", - "lchrusciel/api-test-case": "^5.0", - "matthiasnoback/symfony-config-test": "^4.2", - "matthiasnoback/symfony-dependency-injection-test": "^4.1", - "mikey179/vfsstream": "^1.6", - "mockery/mockery": "^1.4", - "pamil/prophecy-common": "^0.1", - "phpspec/phpspec": "^6.1 || ^7.2", - "phpstan/phpstan": "^0.12.57", - "phpstan/phpstan-doctrine": "^0.12.19", - "phpstan/phpstan-webmozart-assert": "^0.12.7", - "phpunit/phpunit": "^8.5", - "psalm/plugin-mockery": "^0.3", - "psr/event-dispatcher": "^1.0", - "sylius-labs/coding-standard": "^3.1", - "symfony/browser-kit": "^4.4", - "symfony/debug-bundle": "^4.4", - "symfony/dotenv": "^4.4", - "symfony/flex": "^1.7", - "symfony/web-profiler-bundle": "^4.4", - "phpmd/phpmd": "@stable" + "friendsofphp/php-cs-fixer": "^3.16", + "phpspec/phpspec": "^7.0", + "phpstan/phpstan": "^1.8.4", + "phpstan/phpstan-doctrine": "^1.3.2", + "phpstan/phpstan-webmozart-assert": "^1.1", + "phpunit/phpunit": "^10.5", + "phpmd/phpmd": "^2.15" }, "prefer-stable": true, "autoload": { @@ -61,23 +35,20 @@ "phpspec": "phpspec run" }, "extra": { + "branch-alias": { + "dev-master": "1.2-dev" + }, "symfony": { "docker": false, - "endpoint": [ - "https://api.github.com/repos/monsieurbiz/symfony-recipes/contents/index.json?ref=flex/master", - "flex://defaults" - ] - }, - "branch-alias": { - "dev-master": "1.0-dev" + "endpoint": ["https://api.github.com/repos/monsieurbiz/symfony-recipes/contents/index.json?ref=flex/master", "flex://defaults"] } }, "config": { "allow-plugins": { - "dealerdirect/phpcodesniffer-composer-installer": true, "symfony/thanks": true, - "ergebnis/composer-normalize": true, "symfony/flex": true, + "dealerdirect/phpcodesniffer-composer-installer": true, + "ergebnis/composer-normalize": true, "php-http/discovery": true } } diff --git a/dist/.env.local b/dist/.env.local new file mode 100644 index 0000000..6c1a99e --- /dev/null +++ b/dist/.env.local @@ -0,0 +1 @@ +SYLIUS_FIXTURES_HOSTNAME=${SYMFONY_DEFAULT_ROUTE_HOST:-localhost} diff --git a/dist/.gitkeep b/dist/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/phpstan.neon b/phpstan.neon index 20a92fc..d1d870a 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,18 +1,15 @@ -includes: - - vendor/phpstan/phpstan-doctrine/extension.neon - - vendor/phpstan/phpstan-webmozart-assert/extension.neon - parameters: - level: max + level: 8 paths: - %rootDir%/src/ - checkMissingIterableValueType: false - - excludes_analyse: + excludePaths: # Makes PHPStan crash - - 'src/DependencyInjection/Configuration.php' - 'src/DependencyInjection/MonsieurBizSyliusSalesReportsExtension.php' # Test dependencies - 'tests/Application/**/*' + + ignoreErrors: + - identifier: missingType.generics + - identifier: missingType.iterableValue diff --git a/recipes/1.0-dev b/recipes/1.0-dev deleted file mode 120000 index c33aea4..0000000 --- a/recipes/1.0-dev +++ /dev/null @@ -1 +0,0 @@ -1.0/ \ No newline at end of file diff --git a/recipes/1.0/config/packages/monsieurbiz_sylius_sales_reports_plugin.yaml b/recipes/1.0/config/packages/monsieurbiz_sylius_sales_reports_plugin.yaml deleted file mode 100644 index 4190343..0000000 --- a/recipes/1.0/config/packages/monsieurbiz_sylius_sales_reports_plugin.yaml +++ /dev/null @@ -1,2 +0,0 @@ -imports: - - { resource: "@MonsieurBizSyliusSalesReportsPlugin/Resources/config/config.yaml" } diff --git a/recipes/1.0/config/routes/monsieurbiz_sylius_sales_reports_plugin.yaml b/recipes/1.0/config/routes/monsieurbiz_sylius_sales_reports_plugin.yaml deleted file mode 100644 index f3acbaf..0000000 --- a/recipes/1.0/config/routes/monsieurbiz_sylius_sales_reports_plugin.yaml +++ /dev/null @@ -1,2 +0,0 @@ -monsieurbiz_sales_reports_plugin: - resource: "@MonsieurBizSyliusSalesReportsPlugin/Resources/config/routing.yaml" diff --git a/recipes/1.0/manifest.json b/recipes/1.0/manifest.json deleted file mode 100644 index d0567fb..0000000 --- a/recipes/1.0/manifest.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "bundles": { - "MonsieurBiz\\SyliusSalesReportsPlugin\\MonsieurBizSyliusSalesReportsPlugin": [ - "all" - ] - }, - "copy-from-recipe": { - "config/": "%CONFIG_DIR%/" - } -} diff --git a/src/Controller/Admin/ReportsController.php b/src/Controller/Admin/ReportsController.php index 29df6f5..a6297c8 100644 --- a/src/Controller/Admin/ReportsController.php +++ b/src/Controller/Admin/ReportsController.php @@ -13,6 +13,7 @@ namespace MonsieurBiz\SyliusSalesReportsPlugin\Controller\Admin; +use DateTimeInterface; use MonsieurBiz\SyliusSalesReportsPlugin\Event\CustomReportEvent; use MonsieurBiz\SyliusSalesReportsPlugin\Exception\InvalidDateException; use MonsieurBiz\SyliusSalesReportsPlugin\Form\Type\DateType; @@ -42,6 +43,8 @@ final class ReportsController extends AbstractController /** * ReportsController constructor. + * + * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function __construct( ReportRepository $reportRepository, @@ -53,6 +56,8 @@ public function __construct( /** * View the report for a single date. + * + * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function indexAction(Request $request): Response { @@ -83,30 +88,30 @@ public function indexAction(Request $request): Response $isPeriod = true; } $channel = $data['channel']; - $from = $data['date'] ?? $data['from']; - $to = $data['date'] ?? $data['to']; + $fromDate = $data['date'] ?? $data['from']; + $toDate = $data['date'] ?? $data['to']; // Reverse date if from date greater than end date - if ($from > $to) { - $tmp = $to; - $to = $from; - $from = $tmp; - $data['from'] = $from; - $data['to'] = $to; + if ($fromDate > $toDate) { + $tmp = $toDate; + $toDate = $fromDate; + $fromDate = $tmp; + $data['from'] = $fromDate; + $data['to'] = $toDate; } Assert::isInstanceOf($channel, ChannelInterface::class); - Assert::isInstanceOf($from, \DateTimeInterface::class); - Assert::isInstanceOf($to, \DateTimeInterface::class); + Assert::isInstanceOf($fromDate, DateTimeInterface::class); + Assert::isInstanceOf($toDate, DateTimeInterface::class); // Form is valid, we can generate the report try { - $totalSalesResult = $this->reportRepository->getSalesForChannelForDates($channel, $from, $to); - $averageSalesResult = $this->reportRepository->getAverageSalesForChannelForDates($channel, $from, $to); - $productSalesResult = $this->reportRepository->getProductSalesForChannelForDates($channel, $from, $to); - $productVariantSalesResult = $this->reportRepository->getProductVariantSalesForChannelForDates($channel, $from, $to); - $productOptionSalesResult = $this->reportRepository->getProductOptionSalesForChannelForDates($channel, $from, $to); - $productOptionValueSalesResult = $this->reportRepository->getProductOptionValueSalesForChannelForDates($channel, $from, $to); + $totalSalesResult = $this->reportRepository->getSalesForChannelForDates($channel, $fromDate, $toDate); + $averageSalesResult = $this->reportRepository->getAverageSalesForChannelForDates($channel, $fromDate, $toDate); + $productSalesResult = $this->reportRepository->getProductSalesForChannelForDates($channel, $fromDate, $toDate); + $productVariantSalesResult = $this->reportRepository->getProductVariantSalesForChannelForDates($channel, $fromDate, $toDate); + $productOptionSalesResult = $this->reportRepository->getProductOptionSalesForChannelForDates($channel, $fromDate, $toDate); + $productOptionValueSalesResult = $this->reportRepository->getProductOptionValueSalesForChannelForDates($channel, $fromDate, $toDate); } catch (InvalidDateException $e) { $form->addError(new FormError($e->getMessage())); @@ -115,14 +120,14 @@ public function indexAction(Request $request): Response ]); } - $event = new CustomReportEvent($channel, $from, $to); + $event = new CustomReportEvent($channel, $fromDate, $toDate); $this->eventDispatcher->dispatch($event); return $this->render('@MonsieurBizSyliusSalesReportsPlugin/Admin/view.html.twig', [ 'form' => $form->createView(), 'form_period' => $formPeriod->createView(), - 'from' => $from, - 'to' => $to, + 'from' => $fromDate, + 'to' => $toDate, 'channel' => $data['channel'], 'total_sales_result' => $totalSalesResult, 'average_sales_result' => $averageSalesResult, diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 404b4fd..95644f5 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -23,14 +23,6 @@ final class Configuration implements ConfigurationInterface */ public function getConfigTreeBuilder(): TreeBuilder { - $treeBuilder = new TreeBuilder(MonsieurBizSyliusSalesReportsExtension::EXTENSION_CONFIG_NAME); - if (method_exists($treeBuilder, 'getRootNode')) { - $rootNode = $treeBuilder->getRootNode(); - } else { - // BC layer for symfony/config 4.1 and older - $rootNode = $treeBuilder->root(MonsieurBizSyliusSalesReportsExtension::EXTENSION_CONFIG_NAME); - } - - return $treeBuilder; + return new TreeBuilder(MonsieurBizSyliusSalesReportsExtension::EXTENSION_CONFIG_NAME); } } diff --git a/src/Event/CustomReportEvent.php b/src/Event/CustomReportEvent.php index 7bb7287..404a4f2 100644 --- a/src/Event/CustomReportEvent.php +++ b/src/Event/CustomReportEvent.php @@ -13,6 +13,7 @@ namespace MonsieurBiz\SyliusSalesReportsPlugin\Event; +use DateTimeInterface; use MonsieurBiz\SyliusSalesReportsPlugin\Exception\AlreadyExistsReport; use MonsieurBiz\SyliusSalesReportsPlugin\Exception\NotExistsReport; use Sylius\Component\Core\Model\ChannelInterface; @@ -31,19 +32,19 @@ final class CustomReportEvent extends Event private $channel; /** - * @var \DateTimeInterface + * @var DateTimeInterface */ private $fromDate; /** - * @var \DateTimeInterface|null + * @var DateTimeInterface|null */ private $toDate; public function __construct( ChannelInterface $channel, - \DateTimeInterface $fromDate, - ?\DateTimeInterface $toDate = null + DateTimeInterface $fromDate, + ?DateTimeInterface $toDate = null ) { $this->channel = $channel; $this->fromDate = $fromDate; @@ -66,7 +67,7 @@ public function getCustomReports(): array public function addReport(string $key, array $data): void { if (isset($this->customReports[$key])) { - throw new AlreadyExistsReport(sprintf('Report "%s" already exists', $key)); + throw new AlreadyExistsReport(\sprintf('Report "%s" already exists', $key)); } $this->customReports[$key] = $data; } @@ -79,7 +80,7 @@ public function addReport(string $key, array $data): void public function removeReport(string $key): void { if (!isset($this->customReports[$key])) { - throw new NotExistsReport(sprintf('Report "%s" does not exist', $key)); + throw new NotExistsReport(\sprintf('Report "%s" does not exist', $key)); } unset($this->customReports[$key]); } @@ -89,12 +90,12 @@ public function getChannel(): ChannelInterface return $this->channel; } - public function getFromDate(): \DateTimeInterface + public function getFromDate(): DateTimeInterface { return $this->fromDate; } - public function getToDate(): ?\DateTimeInterface + public function getToDate(): ?DateTimeInterface { return $this->toDate; } diff --git a/src/Exception/AlreadyExistsReport.php b/src/Exception/AlreadyExistsReport.php index 6057c3d..b324993 100644 --- a/src/Exception/AlreadyExistsReport.php +++ b/src/Exception/AlreadyExistsReport.php @@ -13,6 +13,8 @@ namespace MonsieurBiz\SyliusSalesReportsPlugin\Exception; -class AlreadyExistsReport extends \Exception +use Exception; + +class AlreadyExistsReport extends Exception { } diff --git a/src/Exception/InvalidDateException.php b/src/Exception/InvalidDateException.php index 0466160..8c1951c 100644 --- a/src/Exception/InvalidDateException.php +++ b/src/Exception/InvalidDateException.php @@ -13,6 +13,8 @@ namespace MonsieurBiz\SyliusSalesReportsPlugin\Exception; -class InvalidDateException extends \Exception +use Exception; + +class InvalidDateException extends Exception { } diff --git a/src/Exception/MissingLocaleException.php b/src/Exception/MissingLocaleException.php index 87f3f28..63156ce 100644 --- a/src/Exception/MissingLocaleException.php +++ b/src/Exception/MissingLocaleException.php @@ -13,6 +13,8 @@ namespace MonsieurBiz\SyliusSalesReportsPlugin\Exception; -class MissingLocaleException extends \Exception +use Exception; + +class MissingLocaleException extends Exception { } diff --git a/src/Exception/NotExistsReport.php b/src/Exception/NotExistsReport.php index c5c5494..043af27 100644 --- a/src/Exception/NotExistsReport.php +++ b/src/Exception/NotExistsReport.php @@ -13,6 +13,8 @@ namespace MonsieurBiz\SyliusSalesReportsPlugin\Exception; -class NotExistsReport extends \Exception +use Exception; + +class NotExistsReport extends Exception { } diff --git a/src/Form/Type/DateType.php b/src/Form/Type/DateType.php index a1a11d3..a1e9dfc 100644 --- a/src/Form/Type/DateType.php +++ b/src/Form/Type/DateType.php @@ -21,6 +21,9 @@ class DateType extends AbstractType { + /** + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ public function buildForm(FormBuilderInterface $builder, array $options): void { $builder diff --git a/src/Form/Type/PeriodType.php b/src/Form/Type/PeriodType.php index c5217f3..ed9445d 100644 --- a/src/Form/Type/PeriodType.php +++ b/src/Form/Type/PeriodType.php @@ -21,6 +21,9 @@ class PeriodType extends AbstractType { + /** + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ public function buildForm(FormBuilderInterface $builder, array $options): void { $builder diff --git a/src/MonsieurBizSyliusSalesReportsPlugin.php b/src/MonsieurBizSyliusSalesReportsPlugin.php index a0cc1ee..ef8dfc1 100644 --- a/src/MonsieurBizSyliusSalesReportsPlugin.php +++ b/src/MonsieurBizSyliusSalesReportsPlugin.php @@ -13,6 +13,7 @@ namespace MonsieurBiz\SyliusSalesReportsPlugin; +use LogicException; use Sylius\Bundle\CoreBundle\Application\SyliusPluginTrait; use Symfony\Component\DependencyInjection\Extension\ExtensionInterface; use Symfony\Component\HttpKernel\Bundle\Bundle; @@ -24,22 +25,17 @@ final class MonsieurBizSyliusSalesReportsPlugin extends Bundle /** * Returns the plugin's container extension. * - * @throws \LogicException + * @throws LogicException * * @return ExtensionInterface|null The container extension */ public function getContainerExtension(): ?ExtensionInterface { if (null === $this->containerExtension) { + $this->containerExtension = false; $extension = $this->createContainerExtension(); - if (null !== $extension) { - if (!$extension instanceof ExtensionInterface) { - throw new \LogicException(sprintf('Extension %s must implement %s.', \get_class($extension), ExtensionInterface::class)); - } $this->containerExtension = $extension; - } else { - $this->containerExtension = false; } } diff --git a/src/Repository/AbstractReportRepository.php b/src/Repository/AbstractReportRepository.php index fcc1102..054ae0e 100644 --- a/src/Repository/AbstractReportRepository.php +++ b/src/Repository/AbstractReportRepository.php @@ -13,6 +13,7 @@ namespace MonsieurBiz\SyliusSalesReportsPlugin\Repository; +use DateTimeInterface; use Doctrine\ORM\EntityRepository; use Doctrine\ORM\QueryBuilder; use Sylius\Component\Core\Model\AdjustmentInterface; @@ -22,6 +23,9 @@ use Sylius\Component\Core\Repository\OrderRepositoryInterface; use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface; +/** + * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) + */ abstract class AbstractReportRepository { // Adjustment if Admin Order Creation plugin is installed @@ -83,8 +87,8 @@ public function __construct( */ protected function getOrderItemUnitValues( ChannelInterface $channel, - \DateTimeInterface $from, - \DateTimeInterface $to + DateTimeInterface $fromDate, + DateTimeInterface $toDate ): array { $queryBuilder = $this->createOrderQuery() ->select($this->getSelectColumns(true, false, false)) @@ -92,7 +96,7 @@ protected function getOrderItemUnitValues( ->leftJoin('item.variant', 'variant') ->leftJoin('item.units', 'element') ; - $queryBuilder = $this->appendAdjustmentsAndParameters($queryBuilder, $channel, $from, $to); + $queryBuilder = $this->appendAdjustmentsAndParameters($queryBuilder, $channel, $fromDate, $toDate); return $queryBuilder->getQuery()->getArrayResult(); } @@ -102,15 +106,15 @@ protected function getOrderItemUnitValues( */ protected function getOrderItemValues( ChannelInterface $channel, - \DateTimeInterface $from, - \DateTimeInterface $to + DateTimeInterface $fromDate, + DateTimeInterface $toDate ): array { $queryBuilder = $this->createOrderQuery() ->select($this->getSelectColumns(false, true, false)) ->leftJoin('o.items', 'element') ->leftJoin('element.variant', 'variant') ; - $queryBuilder = $this->appendAdjustmentsAndParameters($queryBuilder, $channel, $from, $to); + $queryBuilder = $this->appendAdjustmentsAndParameters($queryBuilder, $channel, $fromDate, $toDate); return $queryBuilder->getQuery()->getArrayResult(); } @@ -120,11 +124,11 @@ protected function getOrderItemValues( */ protected function getOrderValues( ChannelInterface $channel, - \DateTimeInterface $from, - \DateTimeInterface $to + DateTimeInterface $fromDate, + DateTimeInterface $toDate ): array { $queryBuilder = $this->createOrderQuery()->select($this->getSelectColumns(false, false, true)); - $queryBuilder = $this->appendAdjustmentsAndParameters($queryBuilder, $channel, $from, $to, true); + $queryBuilder = $this->appendAdjustmentsAndParameters($queryBuilder, $channel, $fromDate, $toDate, true); return $queryBuilder->getQuery()->getArrayResult(); } @@ -135,6 +139,9 @@ protected function getOrderValues( * Column without_tax is for unit price without tax in item units * Columns without_tax_promo, without_tax_shipping, tax columns are respectively for promotions, shipping, tax amounts * Columns item and total are respectively for total for items total for orders (With shipping etc.). + * + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + * @SuppressWarnings(PHPMD.BooleanArgumentFlag) */ protected function getSelectColumns(bool $isItemUnit = false, bool $isItem = false, bool $isOrder = false): string { @@ -166,13 +173,15 @@ protected function getSelectColumns(bool $isItemUnit = false, bool $isItem = fal /** * Make joins with all adjustments, add conditions and set parameters to query. * + * @SuppressWarnings(PHPMD.BooleanArgumentFlag) + * * @return mixed */ protected function appendAdjustmentsAndParameters( QueryBuilder $queryBuilder, ChannelInterface $channel, - \DateTimeInterface $from, - \DateTimeInterface $to, + DateTimeInterface $fromDate, + DateTimeInterface $toDate, bool $isOrder = false ) { $elementAlias = $isOrder ? 'o' : 'element'; @@ -180,16 +189,36 @@ protected function appendAdjustmentsAndParameters( return $queryBuilder // Adjustments joins ->leftJoin($elementAlias . '.adjustments', 'tax_adjustment', 'WITH', 'tax_adjustment.type = :tax_type') - ->leftJoin($elementAlias . '.adjustments', 'shipping_adjustment', 'WITH', - 'shipping_adjustment.type = :shipping_type') - ->leftJoin($elementAlias . '.adjustments', 'order_promotion_adjustment', 'WITH', - 'order_promotion_adjustment.type = :order_promotion_type OR order_promotion_adjustment.type = :admin_order_promotion_type') - ->leftJoin($elementAlias . '.adjustments', 'order_item_promotion_adjustment', 'WITH', - 'order_item_promotion_adjustment.type = :order_item_promotion_type OR order_item_promotion_adjustment.type = :admin_order_item_promotion_type') - ->leftJoin($elementAlias . '.adjustments', 'order_shipping_promotion_adjustment', 'WITH', - 'order_shipping_promotion_adjustment.type = :order_shipping_promotion_type') - ->leftJoin($elementAlias . '.adjustments', 'order_unit_promotion_adjustment', 'WITH', - 'order_unit_promotion_adjustment.type = :order_unit_promotion_type') + ->leftJoin( + $elementAlias . '.adjustments', + 'shipping_adjustment', + 'WITH', + 'shipping_adjustment.type = :shipping_type' + ) + ->leftJoin( + $elementAlias . '.adjustments', + 'order_promotion_adjustment', + 'WITH', + 'order_promotion_adjustment.type = :order_promotion_type OR order_promotion_adjustment.type = :admin_order_promotion_type' + ) + ->leftJoin( + $elementAlias . '.adjustments', + 'order_item_promotion_adjustment', + 'WITH', + 'order_item_promotion_adjustment.type = :order_item_promotion_type OR order_item_promotion_adjustment.type = :admin_order_item_promotion_type' + ) + ->leftJoin( + $elementAlias . '.adjustments', + 'order_shipping_promotion_adjustment', + 'WITH', + 'order_shipping_promotion_adjustment.type = :order_shipping_promotion_type' + ) + ->leftJoin( + $elementAlias . '.adjustments', + 'order_unit_promotion_adjustment', + 'WITH', + 'order_unit_promotion_adjustment.type = :order_unit_promotion_type' + ) // Adjustments parameters ->setParameter('tax_type', AdjustmentInterface::TAX_ADJUSTMENT) ->setParameter('shipping_type', AdjustmentInterface::SHIPPING_ADJUSTMENT) @@ -208,13 +237,15 @@ protected function appendAdjustmentsAndParameters( ->setParameter('channel', $channel) ->setParameter('states', [OrderInterface::STATE_FULFILLED, OrderInterface::STATE_NEW]) ->setParameter('payment_states', [OrderPaymentStates::STATE_PAID]) // @TODO Take care of OrderPaymentStates::STATE_PARTIALLY_PAID - ->setParameter('from', $from) - ->setParameter('to', $to) + ->setParameter('from', $fromDate) + ->setParameter('to', $toDate) ; } /** * Populate result array with options and option values data. + * + * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ protected function populateOptions(string $localeCode): array { @@ -252,8 +283,12 @@ protected function getVariantsOptions(string $localeCode): array $queryBuilder = $this->createProductVariantQuery() ->select('v.id AS variant_id, option.code AS option_code, option_translation.name AS option_label, option_value.code AS option_value_code, option_value_translation.value AS option_value_label') ->leftJoin('v.optionValues', 'option_value') - ->leftJoin('option_value.translations', 'option_value_translation', 'WITH', - 'option_value_translation.locale = :locale') + ->leftJoin( + 'option_value.translations', + 'option_value_translation', + 'WITH', + 'option_value_translation.locale = :locale' + ) ->leftJoin('option_value.option', 'option') ->leftJoin('option.translations', 'option_translation', 'WITH', 'option_translation.locale = :locale') ->setParameter('locale', $localeCode) @@ -291,6 +326,9 @@ protected function initResult(): void /** * Increment results with given array. + * + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + * @SuppressWarnings(PHPMD.UnusedLocalVariable) */ protected function addResults(array $elementResults, ?string $groupField = null): void { @@ -314,6 +352,10 @@ protected function addResults(array $elementResults, ?string $groupField = null) /** * Make the sum of results by elements. + * + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + * @SuppressWarnings(PHPMD.ElseExpression) + * @SuppressWarnings(PHPMD.UnusedLocalVariable) */ protected function addResultsByElement( array $elementResults, @@ -365,6 +407,8 @@ protected function addResultsByElement( /** * Make the average of results depending on number of elements. + * + * @SuppressWarnings(PHPMD.UnusedLocalVariable) */ protected function averageResult(): void { @@ -374,9 +418,11 @@ protected function averageResult(): void $this->result[$key] = round($this->result[$key] / $numberOfElements); } $this->result['number_of_elements'] = \count($this->elements); - } else { - $this->result['number_of_elements'] = 0; + + return; } + + $this->result['number_of_elements'] = 0; } /** diff --git a/src/Repository/ReportRepository.php b/src/Repository/ReportRepository.php index 97b512e..54b70b4 100644 --- a/src/Repository/ReportRepository.php +++ b/src/Repository/ReportRepository.php @@ -13,6 +13,9 @@ namespace MonsieurBiz\SyliusSalesReportsPlugin\Repository; +use DateTime; +use DateTimeInterface; +use Exception; use MonsieurBiz\SyliusSalesReportsPlugin\Exception\InvalidDateException; use MonsieurBiz\SyliusSalesReportsPlugin\Exception\MissingLocaleException; use Sylius\Component\Core\Model\ChannelInterface; @@ -26,27 +29,27 @@ class ReportRepository extends AbstractReportRepository */ public function getSalesForChannelForDates( ChannelInterface $channel, - \DateTimeInterface $from, - ?\DateTimeInterface $to = null, + DateTimeInterface $fromDate, + ?DateTimeInterface $toDate = null, ?string $groupField = null ): array { - $to = $to ?? $from; // If to is null, take the same day as from to make report on one day + $toDate = $toDate ?? $fromDate; // If to is null, take the same day as from to make report on one day try { - $from = new \DateTime($from->format('Y-m-d') . ' 00:00:00'); - $to = new \DateTime($to->format('Y-m-d') . ' 23:59:59'); - } catch (\Exception $e) { + $fromDate = new DateTime($fromDate->format('Y-m-d') . ' 00:00:00'); + $toDate = new DateTime($toDate->format('Y-m-d') . ' 23:59:59'); + } catch (Exception $e) { throw new InvalidDateException('Invalid date given to report.'); } $this->initResult(); // Order Item Units values - $this->addResults($this->getOrderItemUnitValues($channel, $from, $to), $groupField); + $this->addResults($this->getOrderItemUnitValues($channel, $fromDate, $toDate), $groupField); // Order Items values - $this->addResults($this->getOrderItemValues($channel, $from, $to), $groupField); + $this->addResults($this->getOrderItemValues($channel, $fromDate, $toDate), $groupField); // Order values - $this->addResults($this->getOrderValues($channel, $from, $to), $groupField); + $this->addResults($this->getOrderValues($channel, $fromDate, $toDate), $groupField); // Divide results by number of elements if needed $this->averageResult(); @@ -61,10 +64,10 @@ public function getSalesForChannelForDates( */ public function getAverageSalesForChannelForDates( ChannelInterface $channel, - \DateTimeInterface $from, - ?\DateTimeInterface $to = null + DateTimeInterface $fromDate, + ?DateTimeInterface $toDate = null ): array { - return $this->getSalesForChannelForDates($channel, $from, $to, 'order_id'); + return $this->getSalesForChannelForDates($channel, $fromDate, $toDate, 'order_id'); } /** @@ -74,15 +77,15 @@ public function getAverageSalesForChannelForDates( */ public function getProductVariantSalesForChannelForDates( ChannelInterface $channel, - \DateTimeInterface $from, - ?\DateTimeInterface $to = null + DateTimeInterface $fromDate, + ?DateTimeInterface $toDate = null ): array { - $to = $to ?? $from; // If to is null, take the same day as from to make report on one day + $toDate = $toDate ?? $fromDate; // If to is null, take the same day as from to make report on one day try { - $from = new \DateTime($from->format('Y-m-d') . ' 00:00:00'); - $to = new \DateTime($to->format('Y-m-d') . ' 23:59:59'); - } catch (\Exception $e) { + $fromDate = new DateTime($fromDate->format('Y-m-d') . ' 00:00:00'); + $toDate = new DateTime($toDate->format('Y-m-d') . ' 23:59:59'); + } catch (Exception $e) { throw new InvalidDateException('Invalid date given to report.'); } @@ -90,11 +93,15 @@ public function getProductVariantSalesForChannelForDates( // Order Item Units values $this->addResultsByElement( - $this->getOrderItemUnitValues($channel, $from, $to), 'variant_id', 'variant_name' + $this->getOrderItemUnitValues($channel, $fromDate, $toDate), + 'variant_id', + 'variant_name' ); // Order Items values $this->addResultsByElement( - $this->getOrderItemValues($channel, $from, $to), 'variant_id', 'variant_name' + $this->getOrderItemValues($channel, $fromDate, $toDate), + 'variant_id', + 'variant_name' ); return $this->results; @@ -108,15 +115,15 @@ public function getProductVariantSalesForChannelForDates( */ public function getProductOptionSalesForChannelForDates( ChannelInterface $channel, - \DateTimeInterface $from, - ?\DateTimeInterface $to = null + DateTimeInterface $fromDate, + ?DateTimeInterface $toDate = null ): array { - $to = $to ?? $from; // If to is null, take the same day as from to make report on one day + $toDate = $toDate ?? $fromDate; // If to is null, take the same day as from to make report on one day try { - $from = new \DateTime($from->format('Y-m-d') . ' 00:00:00'); - $to = new \DateTime($to->format('Y-m-d') . ' 23:59:59'); - } catch (\Exception $e) { + $fromDate = new DateTime($fromDate->format('Y-m-d') . ' 00:00:00'); + $toDate = new DateTime($toDate->format('Y-m-d') . ' 23:59:59'); + } catch (Exception $e) { throw new InvalidDateException('Invalid date given to report.'); } @@ -124,11 +131,15 @@ public function getProductOptionSalesForChannelForDates( // Order Item Units values $this->addResultsByElement( - $this->getOrderItemUnitValues($channel, $from, $to), 'variant_id', 'variant_name' + $this->getOrderItemUnitValues($channel, $fromDate, $toDate), + 'variant_id', + 'variant_name' ); // Order Items values $this->addResultsByElement( - $this->getOrderItemValues($channel, $from, $to), 'variant_id', 'variant_name' + $this->getOrderItemValues($channel, $fromDate, $toDate), + 'variant_id', + 'variant_name' ); // Populate array with options values data @@ -153,15 +164,15 @@ public function getProductOptionSalesForChannelForDates( */ public function getProductOptionValueSalesForChannelForDates( ChannelInterface $channel, - \DateTimeInterface $from, - ?\DateTimeInterface $to = null + DateTimeInterface $fromDate, + ?DateTimeInterface $toDate = null ): array { - $to = $to ?? $from; // If to is null, take the same day as from to make report on one day + $toDate = $toDate ?? $fromDate; // If to is null, take the same day as from to make report on one day try { - $from = new \DateTime($from->format('Y-m-d') . ' 00:00:00'); - $to = new \DateTime($to->format('Y-m-d') . ' 23:59:59'); - } catch (\Exception $e) { + $fromDate = new DateTime($fromDate->format('Y-m-d') . ' 00:00:00'); + $toDate = new DateTime($toDate->format('Y-m-d') . ' 23:59:59'); + } catch (Exception $e) { throw new InvalidDateException('Invalid date given to report.'); } @@ -169,11 +180,15 @@ public function getProductOptionValueSalesForChannelForDates( // Order Item Units values $this->addResultsByElement( - $this->getOrderItemUnitValues($channel, $from, $to), 'variant_id', 'variant_name' + $this->getOrderItemUnitValues($channel, $fromDate, $toDate), + 'variant_id', + 'variant_name' ); // Order Items values $this->addResultsByElement( - $this->getOrderItemValues($channel, $from, $to), 'variant_id', 'variant_name' + $this->getOrderItemValues($channel, $fromDate, $toDate), + 'variant_id', + 'variant_name' ); // Populate array with options values data @@ -185,8 +200,12 @@ public function getProductOptionValueSalesForChannelForDates( // Reinit results to generate a new one $this->results = []; - $this->addResultsByElement($resultsWithOptions, 'option_value_code', 'option_value_label', - ['option_code', 'option_label']); + $this->addResultsByElement( + $resultsWithOptions, + 'option_value_code', + 'option_value_label', + ['option_code', 'option_label'] + ); return $this->results; } @@ -198,15 +217,15 @@ public function getProductOptionValueSalesForChannelForDates( */ public function getProductSalesForChannelForDates( ChannelInterface $channel, - \DateTimeInterface $from, - ?\DateTimeInterface $to = null + DateTimeInterface $fromDate, + ?DateTimeInterface $toDate = null ): array { - $to = $to ?? $from; // If to is null, take the same day as from to make report on one day + $toDate = $toDate ?? $fromDate; // If to is null, take the same day as from to make report on one day try { - $from = new \DateTime($from->format('Y-m-d') . ' 00:00:00'); - $to = new \DateTime($to->format('Y-m-d') . ' 23:59:59'); - } catch (\Exception $e) { + $fromDate = new DateTime($fromDate->format('Y-m-d') . ' 00:00:00'); + $toDate = new DateTime($toDate->format('Y-m-d') . ' 23:59:59'); + } catch (Exception $e) { throw new InvalidDateException('Invalid date given to report.'); } @@ -214,11 +233,15 @@ public function getProductSalesForChannelForDates( // Order Item Units values $this->addResultsByElement( - $this->getOrderItemUnitValues($channel, $from, $to), 'product_id', 'product_name' + $this->getOrderItemUnitValues($channel, $fromDate, $toDate), + 'product_id', + 'product_name' ); // Order Items values $this->addResultsByElement( - $this->getOrderItemValues($channel, $from, $to), 'product_id', 'product_name' + $this->getOrderItemValues($channel, $fromDate, $toDate), + 'product_id', + 'product_name' ); return $this->results;