-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from maximehuran/feature/fixtures
Set searchable false by default and add fixtures
- Loading branch information
Showing
10 changed files
with
268 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |