gbprod/algolia-specification-bundle

This package is abandoned and no longer maintained. No replacement package was suggested.

This bundle integrates algolia-specification with Symfony

v2.1.0 2018-02-26 08:29 UTC

This package is auto-updated.

Last update: 2021-08-07 10:28:55 UTC


README

This bundle integrates algolia-specification with Symfony.

Build Status codecov Scrutinizer Code Quality Dependency Status

Latest Stable Version Total Downloads Latest Unstable Version License

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()
    )
);