gbprod / algolia-specification-bundle
This bundle integrates algolia-specification with Symfony
Installs: 11 236
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 1
Type:bundle
Requires
- php: ^7.0
- gbprod/algolia-specification: ^2.0
- symfony/expression-language: ^2.|^3.1|^4.0
- symfony/framework-bundle: ^2.|^3.1|^4.0
- symfony/yaml: ^2.|^3.1|^4.0
Requires (Dev)
- phpunit/phpunit: ^6.0|^7.0
This package is auto-updated.
Last update: 2021-08-07 10:28:55 UTC
README
This bundle integrates algolia-specification with Symfony.
Installation
Download bundle using composer :
composer require gbprod/algolia-specification-bundle
Declare in your app/AppKernel.php
file:
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new GBProd\AlgoliaSpecificationBundle\AlgoliaSpecificationBundle(), // ... ); }
Create your specification and your query factory
Take a look to Specification and Algolia Specification libraries for more informations.
Create a specification
<?php namespace GBProd\Acme\CoreDomain\Specification\Product; use GBProd\Specification\Specification; class IsAvailable implements Specification { public function isSatisfiedBy($candidate) { return $candidate->isSellable() && $candidate->expirationDate() > new \DateTime('now') ; } }
Create a query factory
<?php namespace GBProd\Acme\Infrastructure\Algolia\QueryFactory\Product; use GBProd\AlgoliaSpecification\QueryFactory\Factory; use GBProd\Specification\Specification; use Algolia\QueryBuilder; class IsAvailableFactory implements Factory { public function create(Specification $spec) { return 'available=1'; } }
Configuration
Declare your Factory
# src/GBProd/Acme/AcmeBundle/Resource/config/service.yml services: acme.algolia.query_factory.is_available: class: GBProd\Acme\Infrastructure\Algolia\QueryFactory\Product\IsAvailableFactory tags: - { name: algolia.query_factory, specification: GBProd\Acme\CoreDomain\Specification\Product\IsAvailable }
Inject handler in your repository class
# src/GBProd/Acme/AcmeBundle/Resource/config/service.yml services: acme.product_repository: class: GBProd\Acme\Infrastructure\Product\AlgoliaProductRepository arguments: - "@algolia.client" - "@gbprod.algolia_specification_handler"
<?php namespace GBProd\Acme\Infrastructure\Product; use Algolia\Client; use GBProd\AlgoliaSpecification\Handler; use GBProd\Specification\Specification; class AlgoliaProductRepository implements ProductRepository { private $client; private $handler; public function __construct(Client $em, Handler $handler) { $this->client = $client; $this->handler = $handler; } public function findSatisfying(Specification $specification) { $index = $this->client->initIndex('products'); $query = $this->handler->handle($specification); return $type->search(['filters' => $query]); } }
Usage
<?php $products = $productRepository->findSatisfying( new AndX( new IsAvailable(), new IsLowStock() ) );