diff --git a/.github/workflows/run-checks-tests.yaml b/.github/workflows/run-checks-tests.yaml new file mode 100644 index 0000000..b58d1f7 --- /dev/null +++ b/.github/workflows/run-checks-tests.yaml @@ -0,0 +1,28 @@ +on: [push] +concurrency: + group: ${{ github.ref }} + cancel-in-progress: true +name: "Checks and tests" +jobs: + checks-and-tests: + name: Run checks and tests + runs-on: ubuntu-22.04 + steps: + - name: GIT checkout branch - ${{ github.ref }} + uses: actions/checkout@v4 + with: + ref: ${{ github.ref }} + - name: Install PHP, extensions and tools + uses: shivammathur/setup-php@v2 + with: + php-version: '8.1' + extensions: bcmath, gd, intl, pdo_pgsql, redis, pgsql, zip + tools: composer + - name: Install Composer dependencies + run: composer install --optimize-autoloader --no-interaction + - name: Run parallel-lint + run: php vendor/bin/parallel-lint ./src ./tests + - name: Run Easy Coding Standards + run: php vendor/bin/ecs check --verbose ./src ./tests + - name: Run PHPUnit + run: php vendor/bin/phpunit tests diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d2f57af --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/.idea +/.php_cs.cache +/composer.lock +/vendor +/.phpunit.result.cache diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..e9ce588 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,10 @@ +# Contributing + +Thank you for your contributions to Shopsys Article Feed Persoo package. +Together we are making Shopsys Platform better. + +This repository is READ-ONLY. +If you want to [report issues](https://github.com/shopsys/shopsys/issues/new) and/or send [pull requests](https://github.com/shopsys/shopsys/compare), +please use the main [Shopsys repository](https://github.com/shopsys/shopsys). + +Please, check our [Contribution Guide](https://docs.shopsys.com/en/latest/contributing/) before contributing. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..3684292 --- /dev/null +++ b/LICENSE @@ -0,0 +1,14 @@ +MIT License + +Copyright (c) 2017-2023 Shopsys s.r.o., http://www.shopsys.com + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..96bb10c --- /dev/null +++ b/README.md @@ -0,0 +1,46 @@ +# Shopsys Article Feed Persoo + +[![Downloads](https://img.shields.io/packagist/dt/shopsys/article-feed-persoo.svg)](https://packagist.org/packages/shopsys/article-feed-persoo) + +Plugin for [Shopsys Platform](https://www.shopsys.com/shopsys-platform/) responsible for generating XML article feed for [Persoo](https://persoo.cz). + +For detailed information about the feed see [official documentation](https://support.persoo.ai). + +This repository is maintained by [shopsys/shopsys] monorepo, information about changes is in [monorepo CHANGELOG.md](https://github.com/shopsys/shopsys/blob/master/CHANGELOG.md). + +## Installation + +The plugin is a Symfony bundle and is installed in the same way: + +### Download + +Download the package using [Composer](https://getcomposer.org/): + +``` +composer require shopsys/article-feed-persoo +``` + +## How to use + +You should see the new feed immediately after installation in the administration feed list. +All output XML files will be automatically generated by a cron module or they can be generated manually in administration when logged as superadmin. + +## Contributing + +Thank you for your contributions to Shopsys Article Feed Persoo package. +Together we are making Shopsys Platform better. + +This repository is READ-ONLY. +If you want to [report issues](https://github.com/shopsys/shopsys/issues/new) and/or send [pull requests](https://github.com/shopsys/shopsys/compare), +please use the main [Shopsys repository](https://github.com/shopsys/shopsys). + +Please, check our [Contribution Guide](https://docs.shopsys.com/en/latest/contributing/) before contributing. + +## Support + +What to do when you are in troubles or need some help? +The best way is to join our [Slack](https://join.slack.com/t/shopsysframework/shared_invite/zt-11wx9au4g-e5pXei73UJydHRQ7nVApAQ). + +If you want to [report issues](https://github.com/shopsys/shopsys/issues/new), please use the main [Shopsys repository](https://github.com/shopsys/shopsys). + +[shopsys/shopsys]: (https://github.com/shopsys/shopsys) diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..5c487f5 --- /dev/null +++ b/composer.json @@ -0,0 +1,51 @@ +{ + "name": "shopsys/article-feed-persoo", + "type": "library", + "description": "Shopsys Platform plugin interface for Persoo XML article feed generation", + "keywords": ["Persoo", "article feed", "XML feed", "plugin", "Shopsys Platform", "SSFW", "SSP"], + "license": "MIT", + "authors": [ + { + "name": "Shopsys", + "homepage": "https://www.shopsys.com/" + } + ], + "autoload": { + "psr-4": { + "Shopsys\\ArticleFeed\\PersooBundle\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Tests\\ArticleFeed\\PersooBundle\\": "tests/" + } + }, + "require": { + "php": "^8.1", + "ext-json": "*", + "ext-pdo": "*", + "doctrine/dbal": "^3.3.3", + "doctrine/doctrine-bundle": "^2.5.7", + "doctrine/orm": "^2.11.2", + "doctrine/persistence": "^2.4", + "shopsys/framework": "14.0.x-dev", + "shopsys/migrations": "14.0.x-dev", + "shopsys/plugin-interface": "14.0.x-dev", + "symfony/config": "^5.4", + "symfony/dependency-injection": "^5.4", + "symfony/form": "^5.4", + "symfony/http-kernel": "^5.4", + "symfony/translation": "^5.4" + }, + "require-dev": { + "phpunit/phpunit": "^9.5.20", + "shopsys/coding-standards": "14.0.x-dev", + "twig/twig": "^3.5.0" + }, + "config": { + "allow-plugins": { + "symfony/flex": true, + "dealerdirect/phpcodesniffer-composer-installer": true + } + } +} diff --git a/ecs.php b/ecs.php new file mode 100644 index 0000000..29e1e8f --- /dev/null +++ b/ecs.php @@ -0,0 +1,46 @@ +rule(ForceLateStaticBindingForProtectedConstantsSniff::class); + + /* + * this package is meant to be extensible using class inheritance, + * so we want to avoid private visibilities in the model namespace + */ + $services = $ecsConfig->services(); +$services->set('forbidden_private_visibility_fixer.article_feed_persoo', ForbiddenPrivateVisibilityFixer::class) + ->call('configure', [ + [ + 'analyzed_namespaces' => [ + 'Shopsys\ArticleFeed\PersooBundle\Model', + ], + ], + ]); + + $ecsConfig->skip([ + ObjectIsCreatedByFactorySniff::class => [ + __DIR__ . '/tests/*', + ], + PhpdocToPropertyTypeFixer::class => [ + __DIR__ . '/src/*', + ], + DeclareStrictTypesFixer::class => [ + __DIR__ . '/src/*', + ], + ]); + + $ecsConfig->import(__DIR__ . '/vendor/shopsys/coding-standards/ecs.php', null, true); +}; diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..9bec070 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,17 @@ + + + + + diff --git a/src/DependencyInjection/ShopsysArticleFeedPersooExtension.php b/src/DependencyInjection/ShopsysArticleFeedPersooExtension.php new file mode 100644 index 0000000..eefa216 --- /dev/null +++ b/src/DependencyInjection/ShopsysArticleFeedPersooExtension.php @@ -0,0 +1,35 @@ +load('services.yaml'); + } + + /** + * {@inheritdoc} + */ + public function prepend(ContainerBuilder $container): void + { + $container->prependExtensionConfig('doctrine_migrations', [ + 'migrations_paths' => [ + 'Shopsys\ArticleFeed\PersooBundle\Migrations' => __DIR__ . '/../Migrations', + ], + ]); + } +} diff --git a/src/Migrations/.gitkeep b/src/Migrations/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/Model/PersooArticleFacade.php b/src/Model/PersooArticleFacade.php new file mode 100644 index 0000000..11b1154 --- /dev/null +++ b/src/Model/PersooArticleFacade.php @@ -0,0 +1,40 @@ + + */ + public function getItems(DomainConfig $domainConfig, ?int $lastSeekId, int $maxResults): iterable + { + if ($lastSeekId === null) { + $lastSeekId = 0; + } + $articlesData = $this->combinedArticleElasticsearchFacade->getArticlesByDomainId($domainConfig->getId(), $lastSeekId, $maxResults); + + foreach ($articlesData as $article) { + yield $this->persooArticleFeedItemFactory->create($article, $lastSeekId); + $lastSeekId++; + } + } +} diff --git a/src/Model/PersooArticleFeedItem.php b/src/Model/PersooArticleFeedItem.php new file mode 100644 index 0000000..bee67a8 --- /dev/null +++ b/src/Model/PersooArticleFeedItem.php @@ -0,0 +1,34 @@ +id; + } +} diff --git a/src/Model/PersooArticleFeedItemFactory.php b/src/Model/PersooArticleFeedItemFactory.php new file mode 100644 index 0000000..2a5896b --- /dev/null +++ b/src/Model/PersooArticleFeedItemFactory.php @@ -0,0 +1,39 @@ +getImageUrl($articleData), + ); + } + + /** + * @param array $articleData + * @return string|null + */ + protected function getImageUrl(array $articleData): ?string + { + if (!array_key_exists('imageUrl', $articleData) || $articleData['imageUrl'] === null) { + return null; + } + + return FeedItemImageHelper::limitWidthInImageUrl($articleData['imageUrl']); + } +} diff --git a/src/PersooArticleFeed.php b/src/PersooArticleFeed.php new file mode 100644 index 0000000..bfdc97d --- /dev/null +++ b/src/PersooArticleFeed.php @@ -0,0 +1,47 @@ +persooArticleFeedInfo; + } + + /** + * {@inheritdoc} + */ + public function getTemplateFilepath(): string + { + return '@ShopsysArticleFeedPersoo/feed.xml.twig'; + } + + /** + * {@inheritdoc} + */ + public function getItems(DomainConfig $domainConfig, ?int $lastSeekId, int $maxResults): iterable + { + yield from $this->persooArticleFacade->getItems($domainConfig, $lastSeekId, $maxResults); + } +} diff --git a/src/PersooArticleFeedInfo.php b/src/PersooArticleFeedInfo.php new file mode 100644 index 0000000..ae20949 --- /dev/null +++ b/src/PersooArticleFeedInfo.php @@ -0,0 +1,34 @@ + + +{% endblock %} + +{% block item %} +
+ {{ item.title }} + {{ item.link }} + {% if item.description is not null %} + + {% endif %} + {% if item.imageLink is not null %} + {{ item.imageLink }} + {% endif %} +
+{% endblock %} + +{% block end %} +
+{% endblock %} diff --git a/src/ShopsysArticleFeedPersooBundle.php b/src/ShopsysArticleFeedPersooBundle.php new file mode 100644 index 0000000..d92fd43 --- /dev/null +++ b/src/ShopsysArticleFeedPersooBundle.php @@ -0,0 +1,27 @@ +addCompilerPass( + DoctrineOrmMappingsPass::createAnnotationMappingDriver( + [$this->getNamespace() . '\Model'], + [$this->getPath() . '/Model'], + ), + ); + } +} diff --git a/tests/PersooArticleFeedItemTest.php b/tests/PersooArticleFeedItemTest.php new file mode 100644 index 0000000..ae887df --- /dev/null +++ b/tests/PersooArticleFeedItemTest.php @@ -0,0 +1,68 @@ +create($articleData, $itemNumber); + + $this->assertSame($itemNumber, $persooArticleFeedItem->getSeekId()); + $this->assertSame($articleData['name'], $persooArticleFeedItem->title); + $this->assertSame($articleData['url'], $persooArticleFeedItem->link); + $this->assertSame($articleData['text'], $persooArticleFeedItem->description); + $this->assertSame($expectedImageUrl, $persooArticleFeedItem->imageLink); + } + + /** + * @return iterable + */ + public function articleFeedItemCreationDataProvider(): iterable + { + $commonArticleData = [ + 'name' => self::ARTICLE_NAME, + 'url' => self::ARTICLE_URL, + 'text' => self::ARTICLE_TEXT, + ]; + + yield [ + 'articleData' => [ + ...$commonArticleData, + 'imageUrl' => self::ARTICLE_IMAGE_URL, + ], + 'itemNumber' => 1, + 'expectedImageUrl' => self::ARTICLE_IMAGE_URL . '?width=605', + ]; + + yield [ + 'articleData' => [ + ...$commonArticleData, + 'imageUrl' => null, + ], + 'itemNumber' => 2, + 'expectedImageUrl' => null, + ]; + + yield [ + 'articleData' => $commonArticleData, + 'itemNumber' => 3, + 'expectedImageUrl' => null, + ]; + } +}