diff --git a/composer.json b/composer.json index 4600aee..299ddad 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,9 @@ "symfony/http-kernel": "^7.1", "symfony/framework-bundle": "^7.1", "symfony/twig-bundle": "^7.1", - "symfony/dotenv": "^7.1" + "symfony/dotenv": "^7.1", + "twig/intl-extra": "^3.13", + "twig/extra-bundle": "^3.13" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index 629c927..184cebc 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "f2dfc64ddf11dc6a94b37f7eac9ffc0f", + "content-hash": "4eef5feb5e2635d7fb7c69c8ed01a84c", "packages": [ { "name": "brick/math", @@ -2949,6 +2949,92 @@ ], "time": "2024-09-21T06:09:21+00:00" }, + { + "name": "symfony/intl", + "version": "v7.1.8", + "source": { + "type": "git", + "url": "https://github.com/symfony/intl.git", + "reference": "e56b243fc0afa5a12bd11dace4002ada5a7d99f8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/intl/zipball/e56b243fc0afa5a12bd11dace4002ada5a7d99f8", + "reference": "e56b243fc0afa5a12bd11dace4002ada5a7d99f8", + "shasum": "" + }, + "require": { + "php": ">=8.2", + "symfony/deprecation-contracts": "^2.5|^3" + }, + "conflict": { + "symfony/string": "<7.1" + }, + "require-dev": { + "symfony/filesystem": "^6.4|^7.0", + "symfony/var-exporter": "^6.4|^7.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\Intl\\": "" + }, + "exclude-from-classmap": [ + "/Tests/", + "/Resources/data/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Bernhard Schussek", + "email": "bschussek@gmail.com" + }, + { + "name": "Eriksen Costa", + "email": "eriksen.costa@infranology.com.br" + }, + { + "name": "Igor Wiedler", + "email": "igor@wiedler.ch" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Provides access to the localization data of the ICU library", + "homepage": "https://symfony.com", + "keywords": [ + "i18n", + "icu", + "internationalization", + "intl", + "l10n", + "localization" + ], + "support": { + "source": "https://github.com/symfony/intl/tree/v7.1.8" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2024-11-08T15:46:42+00:00" + }, { "name": "symfony/polyfill-ctype", "version": "v1.31.0", @@ -4194,6 +4280,144 @@ ], "time": "2024-06-28T08:00:31+00:00" }, + { + "name": "twig/extra-bundle", + "version": "v3.13.0", + "source": { + "type": "git", + "url": "https://github.com/twigphp/twig-extra-bundle.git", + "reference": "21a9a7aa9f79d4493bb6fed4eb2794339f9551f5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/twigphp/twig-extra-bundle/zipball/21a9a7aa9f79d4493bb6fed4eb2794339f9551f5", + "reference": "21a9a7aa9f79d4493bb6fed4eb2794339f9551f5", + "shasum": "" + }, + "require": { + "php": ">=8.0.2", + "symfony/framework-bundle": "^5.4|^6.4|^7.0", + "symfony/twig-bundle": "^5.4|^6.4|^7.0", + "twig/twig": "^3.0|^4.0" + }, + "require-dev": { + "league/commonmark": "^1.0|^2.0", + "symfony/phpunit-bridge": "^6.4|^7.0", + "twig/cache-extra": "^3.0", + "twig/cssinliner-extra": "^3.0", + "twig/html-extra": "^3.0", + "twig/inky-extra": "^3.0", + "twig/intl-extra": "^3.0", + "twig/markdown-extra": "^3.0", + "twig/string-extra": "^3.0" + }, + "type": "symfony-bundle", + "autoload": { + "psr-4": { + "Twig\\Extra\\TwigExtraBundle\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com", + "homepage": "http://fabien.potencier.org", + "role": "Lead Developer" + } + ], + "description": "A Symfony bundle for extra Twig extensions", + "homepage": "https://twig.symfony.com", + "keywords": [ + "bundle", + "extra", + "twig" + ], + "support": { + "source": "https://github.com/twigphp/twig-extra-bundle/tree/v3.13.0" + }, + "funding": [ + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/twig/twig", + "type": "tidelift" + } + ], + "time": "2024-09-01T20:39:12+00:00" + }, + { + "name": "twig/intl-extra", + "version": "v3.13.0", + "source": { + "type": "git", + "url": "https://github.com/twigphp/intl-extra.git", + "reference": "1b8d78c5db08bdc61015fd55009d2e84b3aa7e38" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/twigphp/intl-extra/zipball/1b8d78c5db08bdc61015fd55009d2e84b3aa7e38", + "reference": "1b8d78c5db08bdc61015fd55009d2e84b3aa7e38", + "shasum": "" + }, + "require": { + "php": ">=8.0.2", + "symfony/intl": "^5.4|^6.4|^7.0", + "twig/twig": "^3.13|^4.0" + }, + "require-dev": { + "symfony/phpunit-bridge": "^6.4|^7.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Twig\\Extra\\Intl\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com", + "homepage": "http://fabien.potencier.org", + "role": "Lead Developer" + } + ], + "description": "A Twig extension for Intl", + "homepage": "https://twig.symfony.com", + "keywords": [ + "intl", + "twig" + ], + "support": { + "source": "https://github.com/twigphp/intl-extra/tree/v3.13.0" + }, + "funding": [ + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/twig/twig", + "type": "tidelift" + } + ], + "time": "2024-09-03T13:08:40+00:00" + }, { "name": "twig/twig", "version": "v3.14.0", diff --git a/index.php b/index.php index 005d203..38ab369 100644 --- a/index.php +++ b/index.php @@ -1,5 +1,6 @@ autowire() ->autoconfigure() ; + $container->services()->set(AppExtension::class)->tag('twig.extension'); } protected function configureRoutes(RoutingConfigurator $routes): void diff --git a/index_old.php b/index_old.php deleted file mode 100644 index 785c415..0000000 --- a/index_old.php +++ /dev/null @@ -1,48 +0,0 @@ -database.sqlite missing. Run docker compose
docker compose exec php-app php index.php app:migrate
to create it.'); -} -$productRoute = new Route('/product/{product_id<\d+>}', ['_controller' => ProductController::class]); -$searchRoute = new Route('/?search={search_term}', ['_controller' => SearchController::class]); -$categoryRoute = new Route('/category/{category_name}', ['_controller' => CategoryController::class]); -$starRoute = new Route('/star/{product_id}', ['_controller' => StarController::class]); -$promosRoute = new Route('/promos', ['_controller' => PromosController::class]); -$indexRoute = new Route('/', ['_controller' => IndexController::class]); -$routes = new RouteCollection(); - -$routes->add('product_show', $productRoute); -$routes->add('search_show', $searchRoute); -$routes->add('category_show', $categoryRoute); -$routes->add('start_show', $starRoute); -$routes->add('promos_show', $promosRoute); -$routes->add('index_show', $indexRoute); - -$context = new RequestContext(); -$matcher = new UrlMatcher($routes, $context); -try { - $parameters = $matcher->match($_SERVER['REQUEST_URI']); -} catch (Exception $e) { - die($e->getMessage()); -} - -match ($parameters['_controller']) { - SearchController::class => (new $parameters['_controller']())($parameters['search_term']), - CategoryController::class => (new $parameters['_controller']())($parameters['category_name']), - ProductController::class, StarController::class => (new $parameters['_controller']())($parameters['product_id']), - PromosController::class, IndexController::class => (new $parameters['_controller']())(), - default => throw new Exception('Route not found') -}; diff --git a/src/Command/Migrate.php b/src/Command/Migrate.php index 2ece48c..cea7a04 100644 --- a/src/Command/Migrate.php +++ b/src/Command/Migrate.php @@ -51,7 +51,6 @@ class Migrate extends Command $table->string('name'); $table->integer('skuID')->unique(); $table->integer('availableQuantity'); - $table->integer('stock'); $table->json('categories'); $table->string('image'); $table->string('subTitle'); diff --git a/src/Command/ScrapeWebsite.php b/src/Command/ScrapeWebsite.php index b931a42..11bfbdd 100644 --- a/src/Command/ScrapeWebsite.php +++ b/src/Command/ScrapeWebsite.php @@ -69,7 +69,7 @@ class ScrapeWebsite extends Command $products = array_merge($products, $responseObject->products); $page++; $canLoadMore = $responseObject->canLoadMore; - } catch (GuzzleException $e) { + } catch (GuzzleException) { return $products; } } while ($canLoadMore); @@ -81,12 +81,10 @@ class ScrapeWebsite extends Command { /** @var Product $productModel */ $productModel = Product::firstOrNew(['skuID' => $product->skuID]); - //dd($productModel); $productModel->skuID = $product->skuID; $productModel->name = $product->name; $productModel->availableQuantity = $product->availableQuantity; - $productModel->stock = $product->stock; $productModel->categories = $product->categories; $productModel->image = $product->image; $productModel->subTitle = $product->subTitle; diff --git a/src/Controller/ProductController.php b/src/Controller/ProductController.php index b15e938..20ebb4b 100644 --- a/src/Controller/ProductController.php +++ b/src/Controller/ProductController.php @@ -2,7 +2,6 @@ namespace Krzysiej\RyobiCrawler\Controller; -use Illuminate\Support\ItemNotFoundException; use Krzysiej\RyobiCrawler\Models\Product; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; @@ -21,9 +20,9 @@ final class ProductController extends BaseController public function __invoke(int $productId): Response { $product = Product::with([ - 'price' => fn($query) => $query->orderBy('created_at', 'desc') + 'price' => fn($query) => $query->orderBy('created_at', 'desc'), + 'stock' => fn($query) => $query->orderBy('created_at', 'desc'), ])->find($productId); - if(null === $product) { throw $this->createNotFoundException('Product not found'); } diff --git a/src/Models/Product.php b/src/Models/Product.php index 8537677..54fd545 100644 --- a/src/Models/Product.php +++ b/src/Models/Product.php @@ -11,7 +11,6 @@ use Illuminate\Database\Eloquent\Relations\HasOne; * @property integer $skuID * @property string $name * @property integer $availableQuantity - * @property integer $stock * @property string[] $categories * @property string $image * @property string $subTitle diff --git a/src/Twig/AppExtension.php b/src/Twig/AppExtension.php new file mode 100644 index 0000000..0376fbd --- /dev/null +++ b/src/Twig/AppExtension.php @@ -0,0 +1,39 @@ + $query->whereColumn('price', '<', 'productStandardPrice'))->with(['currentPrice'])->count(); + } + + public function findByCreatedAtDate(Collection $items, string $date): Stock|Price|null + { + return $items->first(fn($item) => str_starts_with($item->created_at, $date)); + } +} \ No newline at end of file diff --git a/templates/product.html.twig b/templates/product.html.twig index 6e77d72..c52bfd8 100644 --- a/templates/product.html.twig +++ b/templates/product.html.twig @@ -6,7 +6,7 @@ {% if product.starred %}★{% else %} ☆ {% endif %} {{ product.name }} - {{ product.name }} + {{ product.name }} {{ product.subTitle }} link - {{ product.price.last.price }} - {% if product.price.last.price != product.price.last.productStandardPrice %}{{ product.price.last.productStandardPrice }} {{ ((1 - product.price.last.price / product.price.last.productStandardPrice)*100)|number_format(0) }}%{% else %}{% endif %} + {{ product.price.last.price | format_currency('PLN', {}, 'pl') }} + {% if product.price.last.price != product.price.last.productStandardPrice %}{{ product.price.last.productStandardPrice | format_currency('PLN', {}, 'pl') }} {{ ((1 - product.price.last.price / product.price.last.productStandardPrice)*100)|number_format(0) }}%{% else %}{% endif %} {% endfor %} diff --git a/templates/template.html.twig b/templates/template.html.twig index 0761055..bc05a88 100644 --- a/templates/template.html.twig +++ b/templates/template.html.twig @@ -19,7 +19,7 @@