Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set searchable false by default and add fixtures #111

Merged
merged 5 commits into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@ server.stop: ## Stop the local webserver
es.reindex: ## Reindex elasticsearch
${CONSOLE} monsieurbiz:search:populate

doctrine.diff: ## Doctrine diff
${CONSOLE} doctrine:migration:diff

doctrine.migrate: ## Doctrine diff
${CONSOLE} doctrine:migration:migrate

###
### HELP
### ¯¯¯¯
Expand Down
42 changes: 42 additions & 0 deletions dist/src/Migrations/Version20220202125901.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of Monsieur Biz' Search plugin for Sylius.
*
* (c) Monsieur Biz <sylius@monsieurbiz.com>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace App\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20220202125901 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE sylius_product_attribute CHANGE searchable searchable TINYINT(1) DEFAULT \'0\' NOT NULL');
$this->addSql('ALTER TABLE sylius_product_option CHANGE searchable searchable TINYINT(1) DEFAULT \'0\' NOT NULL');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE sylius_product_attribute CHANGE searchable searchable TINYINT(1) DEFAULT \'1\' NOT NULL');
$this->addSql('ALTER TABLE sylius_product_option CHANGE searchable searchable TINYINT(1) DEFAULT \'1\' NOT NULL');
}
}
98 changes: 98 additions & 0 deletions src/Fixture/Factory/SearchableFixtureFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

/*
* This file is part of Monsieur Biz' Search plugin for Sylius.
*
* (c) Monsieur Biz <sylius@monsieurbiz.com>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace MonsieurBiz\SyliusSearchPlugin\Fixture\Factory;

use MonsieurBiz\SyliusSearchPlugin\Entity\Product\SearchableInterface;
use Sylius\Bundle\CoreBundle\Fixture\Factory\AbstractExampleFactory;
use Sylius\Bundle\CoreBundle\Fixture\OptionsResolver\LazyOption;
use Sylius\Component\Product\Model\ProductAttributeInterface;
use Sylius\Component\Product\Model\ProductOptionInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class SearchableFixtureFactory extends AbstractExampleFactory implements SearchableFixtureFactoryInterface
{
/**
* @var RepositoryInterface
*/
protected $productAttributeRepository;

/**
* @var RepositoryInterface
*/
protected $productOptionRepository;

/**
* @var OptionsResolver
*/
private $optionsResolver;

/**
* SearchableFixtureFactory constructor.
*/
public function __construct(
RepositoryInterface $productAttributeRepository,
RepositoryInterface $productOptionRepository
) {
$this->productAttributeRepository = $productAttributeRepository;
$this->productOptionRepository = $productOptionRepository;
$this->optionsResolver = new OptionsResolver();
$this->configureOptions($this->optionsResolver);
}

/**
* @inheritdoc
*/
protected function configureOptions(OptionsResolver $resolver): void
{
$resolver
->setDefault('attribute', null)
->setAllowedTypes('attribute', ['null', 'string', ProductAttributeInterface::class])
->setNormalizer('attribute', LazyOption::findOneBy($this->productAttributeRepository, 'code'))
->setDefault('option', null)
->setAllowedTypes('option', ['null', 'string', ProductOptionInterface::class])
->setNormalizer('option', LazyOption::findOneBy($this->productOptionRepository, 'code'))
->setDefault('filterable', false)
->setDefault('searchable', false)
;
}

/**
* @throws \Exception
*
* @return object
*/
public function create(array $options = [])
{
$options = $this->optionsResolver->resolve($options);

if (isset($options['attribute']) && !empty($options['attribute'])) {
$object = $options['attribute'];
} elseif (isset($options['option']) && !empty($options['option'])) {
$object = $options['option'];
} else {
throw new \Exception('You need to specify an attribute or an option to be filterable.');
}

if (!$object instanceof SearchableInterface) {
throw new \Exception(sprintf('Your class "%s" is not an instance of %s', \get_class($object), SearchableInterface::class));
}

/** @var SearchableInterface $object */
$object->setFilterable(((bool) $options['filterable']) ?? false);
$object->setSearchable(((bool) $options['searchable']) ?? false);

return $object;
}
}
20 changes: 20 additions & 0 deletions src/Fixture/Factory/SearchableFixtureFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of Monsieur Biz' Search plugin for Sylius.
*
* (c) Monsieur Biz <sylius@monsieurbiz.com>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace MonsieurBiz\SyliusSearchPlugin\Fixture\Factory;

use Sylius\Bundle\CoreBundle\Fixture\Factory\ExampleFactoryInterface;

interface SearchableFixtureFactoryInterface extends ExampleFactoryInterface
{
}
54 changes: 54 additions & 0 deletions src/Fixture/SearchableFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/*
* This file is part of Monsieur Biz' Search plugin for Sylius.
*
* (c) Monsieur Biz <sylius@monsieurbiz.com>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace MonsieurBiz\SyliusSearchPlugin\Fixture;

use Doctrine\ORM\EntityManagerInterface;
use MonsieurBiz\SyliusSearchPlugin\Fixture\Factory\SearchableFixtureFactoryInterface;
use Sylius\Bundle\CoreBundle\Fixture\AbstractResourceFixture;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;

class SearchableFixture extends AbstractResourceFixture implements SearchableFixtureInterface
{
/**
* SearchableFixture constructor.
*/
public function __construct(
EntityManagerInterface $productManager,
SearchableFixtureFactoryInterface $exampleFactory
) {
parent::__construct($productManager, $exampleFactory);
}

/**
* @inheritdoc
*/
public function getName(): string
{
return 'monsieurbiz_sylius_search';
}

/**
* @inheritdoc
*/
protected function configureResourceNode(ArrayNodeDefinition $resourceNode): void
{
$resourceNode
->children()
->scalarNode('attribute')->end()
->scalarNode('option')->end()
->booleanNode('filterable')->defaultValue(true)->end()
->booleanNode('searchable')->defaultValue(true)->end()
;
}
}
20 changes: 20 additions & 0 deletions src/Fixture/SearchableFixtureInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of Monsieur Biz' Search plugin for Sylius.
*
* (c) Monsieur Biz <sylius@monsieurbiz.com>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace MonsieurBiz\SyliusSearchPlugin\Fixture;

use Sylius\Bundle\FixturesBundle\Fixture\FixtureInterface;

interface SearchableFixtureInterface extends FixtureInterface
{
}
4 changes: 2 additions & 2 deletions src/Model/Product/SearchableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
trait SearchableTrait
{
/**
* @ORM\Column(name="searchable", type="boolean", nullable=false, options={"default"=true})
* @ORM\Column(name="searchable", type="boolean", nullable=false, options={"default"=false})
*/
protected bool $searchable = true;
protected bool $searchable = false;

/**
* @ORM\Column(name="filterable", type="boolean", nullable=false, options={"default"=false})
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ imports:
- { resource: "services.yaml" }
- { resource: "monsieurbiz_search.yaml" }
- { resource: "sylius/ui.yaml" }
- { resource: "sylius/fixtures.yaml" }
- { resource: "monsieurbiz/settings.yaml" }
- { resource: "messenger.yaml" }
2 changes: 2 additions & 0 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ services:
_instanceof:
MonsieurBiz\SyliusSearchPlugin\AutoMapper\ProductAttributeValueReader\ReaderInterface:
tags: [ 'monsieurbiz.search.automapper.product_attribute_value_reader' ]
MonsieurBiz\SyliusSearchPlugin\Fixture\SearchableFixtureInterface:
tags: ['sylius_fixtures.fixture']

MonsieurBiz\SyliusSearchPlugin\:
resource: '../../*'
Expand Down
23 changes: 23 additions & 0 deletions src/Resources/config/sylius/fixtures.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
sylius_fixtures:
suites:
default:
fixtures:
monsieurbiz_sylius_search:
options:
custom:
cap_collection:
attribute: 'cap_collection'
filterable: true
searchable: true
dress_collection:
attribute: 'dress_collection'
filterable: true
searchable: true
dress_height:
option: 'dress_height'
filterable: true
searchable: true
dress_size:
option: 'dress_size'
filterable: true
searchable: true