Skip to content

Commit

Permalink
Merge pull request #6 from LeoMoshko/bug/issues_4_Locale_should_not_h…
Browse files Browse the repository at this point in the history
…ardcoded

fix Locale should not be hardcoded
  • Loading branch information
LeoMoshko authored Sep 29, 2021
2 parents 4fd3b69 + 8d23572 commit bb70c95
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
9 changes: 7 additions & 2 deletions src/Form/Provider/AttributeOptionsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@ class AttributeOptionsProvider implements AttributeOptionsProviderInterface
/** @var RepositoryInterface */
private $productAttributeRepository;

/** @var string */
private $localeCode;

public function __construct(
ProductAttributeValueRepository $productAttributeValueRepository,
RepositoryInterface $productAttributeRepository
RepositoryInterface $productAttributeRepository,
string $localeCode
)
{
$this->productAttributeValueRepository = $productAttributeValueRepository;
$this->productAttributeRepository = $productAttributeRepository;
$this->localeCode = $localeCode;
}

public function provideOptionsByAttributeCode(string $code): array
Expand All @@ -49,7 +54,7 @@ public function provideOptionsByAttributeCode(string $code): array
$qb->andWhere('av.attribute = :attribute');
$qb->andWhere('av.localeCode = :localeCode');
$qb->setParameter(':attribute', $attribute->getId());
$qb->setParameter(':localeCode', 'en_US');
$qb->setParameter(':localeCode', $this->localeCode);

$choices = [];
foreach($qb->getQuery()->getResult() as $value) {
Expand Down
12 changes: 11 additions & 1 deletion src/Matcher/SizeChartValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@

final class SizeChartValidator implements SizeChartValidatorInterface
{
/** @var string */
private $localeCode;

public function __construct(
string $localeCode
)
{
$this->localeCode = $localeCode;
}

public function isValidForTheProduct(SizeChartInterface $sizeChart, ProductInterface $product): bool
{
$criteria = $sizeChart->getCriteria();
Expand All @@ -32,7 +42,7 @@ public function isValidForTheProduct(SizeChartInterface $sizeChart, ProductInter
foreach($attributeCodes as $attributeCode) {
/** @var string $value */
$value = $criteria['attribute' . $attributeCode ] ?? '';
$productAttributeValue = $product->getAttributeByCodeAndLocale($attributeCode, 'en_US');
$productAttributeValue = $product->getAttributeByCodeAndLocale($attributeCode, $this->localeCode);

if (!$productAttributeValue || $productAttributeValue->getValue() !== $value) {
return false;
Expand Down
10 changes: 8 additions & 2 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ Architects of this package:
@author Piotr Lewandowski <[email protected]>
-->

<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<imports>
<import resource="services/**/*.xml" />
</imports>
Expand All @@ -26,7 +29,10 @@ Architects of this package:
</service>

<!-- Size Chart Matcher -->
<service id="madcoders_sizechart.matcher.validator" class="Madcoders\SyliusSizechartPlugin\Matcher\SizeChartValidator" />
<service id="madcoders_sizechart.matcher.validator"
class="Madcoders\SyliusSizechartPlugin\Matcher\SizeChartValidator">
<argument>%locale%</argument>
</service>
<service id="madcoders_sizechart.matcher.live_matcher" class="Madcoders\SyliusSizechartPlugin\Matcher\LiveMatcher">
<argument type="service" id="madcoders_sizechart.repository.size_chart" />
<argument type="service" id="madcoders_sizechart.matcher.validator" />
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services/forms.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class="Madcoders\SyliusSizechartPlugin\Form\Provider\AttributeOptionsProvider">
<argument type="service" id="sylius.repository.product_attribute_value" />
<argument type="service" id="sylius.repository.product_attribute" />
<argument>%locale%</argument>
</service>

<!-- size chart option criteria form view factory -->
Expand Down

0 comments on commit bb70c95

Please sign in to comment.