Skip to content

Commit

Permalink
Merge pull request #9 from Setono/feature-all-products
Browse files Browse the repository at this point in the history
Added ability to configure how to treat no rules eligibility
  • Loading branch information
igormukhingmailcom authored Oct 10, 2019
2 parents 0920ba2 + 09a13e6 commit 002ef41
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 17 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ setono_sylius_callout:
# That way you can trigger callouts assign process manually when
# finish adding all rules
# manual_triggering: true

no_rules_eligible: false
# Set this option to true if you want no rules to be
# treated as eligible (e.g. callout without rules will
# be applied to all products)
# no_rules_eligible: true
```

### Step 5: Import routing
Expand Down
6 changes: 6 additions & 0 deletions features/shop/displaying_product_callouts.feature
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,9 @@ Feature: Displaying Product Callouts
And this callout is disabled for "United States" channel
When I browse products from taxon "T-Shirts"
Then I should see 0 products with callout "Disabled"

@ui
Scenario: Callouts without rules should be applied to all products
Given there is a callout "No rules" with "<p>No rules</p>" html
When I browse products from taxon "T-Shirts"
Then I should see 3 product with callout "No rules"
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ function it_eligible_only_when_rules_specified(
$this->isEligible($product, $callout)->shouldReturn(false);
}

function it_eligible_when_no_rules_specified_but_isNoRulesEligible_option_set_to_true(
ServiceRegistryInterface $ruleRegistry,
CalloutInterface $callout,
CalloutsAwareInterface $product
): void {
$this->beConstructedWith($ruleRegistry, true);
$callout->hasRules()->willReturn(false);

$this->isEligible($product, $callout)->shouldReturn(true);
}

function it_checks_eligibility(
CalloutInterface $callout,
CalloutRuleInterface $rule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@ final class CalloutRulesEligibilityChecker implements CalloutEligibilityCheckerI
/** @var ServiceRegistryInterface */
private $ruleRegistry;

public function __construct(ServiceRegistryInterface $ruleRegistry)
/** @var bool */
private $isNoRulesEligible;

public function __construct(ServiceRegistryInterface $ruleRegistry, bool $isNoRulesEligible = false)
{
$this->ruleRegistry = $ruleRegistry;
$this->isNoRulesEligible = $isNoRulesEligible;
}

public function isEligible(CalloutsAwareInterface $product, CalloutInterface $callout): bool
{
// If a Callout has no rules - it won't be applied to any products
// As far as Callout should be applied to some group of products, not to all products
if (!$callout->hasRules()) {
return false;
return $this->isNoRulesEligible;
}

// All rules should pass for Product to be eligible
Expand Down
4 changes: 4 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public function getConfigTreeBuilder(): TreeBuilder
->info('Set it to true if you have thousands of products in your store and/or want to trigger callout assign manually.')
->defaultFalse()
->end()
->booleanNode('no_rules_eligible')
->info('Set it to true if you want no rules to be treated as eligible (callout will be applied to all products).')
->defaultFalse()
->end()
->end()
;

Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/SetonoSyliusCalloutExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function load(array $config, ContainerBuilder $container): void
$loader->load('services.xml');

$container->setParameter('setono_sylius_callout.manual_triggering', $config['manual_triggering']);
$container->setParameter('setono_sylius_callout.no_rules_eligible', $config['no_rules_eligible']);
$this->registerResources('setono_sylius_callout', $config['driver'], $config['resources'], $container);
}
}
26 changes: 26 additions & 0 deletions src/EventListener/CalloutDoctrineEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,30 @@
namespace Setono\SyliusCalloutPlugin\EventListener;

use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\Events;
use Doctrine\ORM\PersistentCollection;
use Setono\SyliusCalloutPlugin\Model\CalloutInterface;
use Setono\SyliusCalloutPlugin\Model\CalloutRuleInterface;
use Symfony\Component\Messenger\MessageBusInterface;

final class CalloutDoctrineEventSubscriber extends AbstractCalloutDoctrineEventSubscriber implements EventSubscriber
{
/** @var bool */
private $isNoRulesEligible;

public function __construct(
EntityManager $calloutManager,
MessageBusInterface $commandBus,
bool $manualTriggering = false,
bool $isNoRulesEligible = false
) {
parent::__construct($calloutManager, $commandBus, $manualTriggering);

$this->isNoRulesEligible = $isNoRulesEligible;
}

public function getSubscribedEvents()
{
return [
Expand All @@ -26,6 +42,16 @@ public function onFlush(OnFlushEventArgs $args): void
$em = $args->getEntityManager();
$uow = $em->getUnitOfWork();

if ($this->isNoRulesEligible) {
foreach ($uow->getScheduledEntityInsertions() as $entity) {
if (!$entity instanceof CalloutInterface) {
continue;
}

$this->scheduleCalloutUpdate($entity);
}
}

// Here we need only rules collection changes to track as far as
// creating/updating Callout itself (without rules) not require
// any assign triggering
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<service id="setono_sylius_callout.callout_eligibility_checker.rules"
class="Setono\SyliusCalloutPlugin\Callout\Checker\Eligibility\CalloutRulesEligibilityChecker">
<argument type="service" id="setono_sylius_callout.registry.callout_rule_checker" />
<argument>%setono_sylius_callout.no_rules_eligible%</argument>
<tag name="setono_sylius_callout.callout_eligibility_checker" />
</service>

Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services/event_listener.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<service id="setono_sylius_callout.doctrine_event_subscriber.callout"
class="Setono\SyliusCalloutPlugin\EventListener\CalloutDoctrineEventSubscriber"
parent="Setono\SyliusCalloutPlugin\EventListener\AbstractCalloutDoctrineEventSubscriber">
<argument>%setono_sylius_callout.no_rules_eligible%</argument>
<tag name="doctrine.event_subscriber" />
</service>

Expand Down
10 changes: 0 additions & 10 deletions src/Resources/config/validation/Callout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,5 @@
</constraint>
</property>

<property name="rules">
<constraint name="Count">
<option name="min">1</option>
<option name="minMessage">setono_sylius_callout.callout.rules.min</option>
<option name="groups">
<value>setono_sylius_callout</value>
</option>
</constraint>
</property>

</class>
</constraint-mapping>
2 changes: 0 additions & 2 deletions src/Resources/translations/validators.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ setono_sylius_callout:
priority:
not_blank: Priority cannot be blank.
numeric: Priority must be an integer.
rules:
min: Specify at least one rule.
callout_translation:
text:
not_blank: Text cannot be blank.
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
imports:
- { resource: "@SetonoSyliusCalloutPlugin/Resources/config/app/config.yaml"}
- { resource: "@SetonoSyliusCalloutPlugin/Resources/config/app/fixtures.yaml"}

setono_sylius_callout:
no_rules_eligible: true
14 changes: 13 additions & 1 deletion tests/Behat/Context/Setup/ProductCalloutContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function thereIsAProductCalloutWithRuleConfiguredWithProduct(string $name
* @Given /^there is a callout "([^"]+)" with "Is new" rule configured with (\d+) days? and with "([^"]+)" html$/
* @Given /^there is a callout "([^"]+)" with "Is new" rule configured with (\d+) days? and with "([^"]+)" html in ("[^"]+" channel)$/
*/
public function thereIsAnIsNewProductCalloutWithRuleConfiguredWithProduct(string $name, string $days, string $html, ChannelInterface $channel = null): void
public function thereIsAnIsNewProductCalloutWithRuleConfiguredWithDays(string $name, string $days, string $html, ChannelInterface $channel = null): void
{
$callout = $this->createCallout($name, $html, $channel);
$callout->addRule($this->calloutRuleFactory->createIsNewProduct((int)$days));
Expand All @@ -86,6 +86,18 @@ public function thereIsAnIsNewProductCalloutWithRuleConfiguredWithProduct(string
$this->objectManager->flush();
}

/**
* @Given /^there is a callout "([^"]+)" with "([^"]+)" html$/
* @Given /^there is a callout "([^"]+)" with "([^"]+)" html in ("[^"]+" channel)$/
*/
public function thereIsCalloutWithoutRules(string $name, string $html, ChannelInterface $channel = null): void
{
$callout = $this->createCallout($name, $html, $channel);

$this->objectManager->persist($callout);
$this->objectManager->flush();
}

/**
* @Given /^(the callout "([^"]+)") is disabled for ("[^"]+" channel)$/
* @Given /^(the callout "([^"]+)") is disabled for (this channel)$/
Expand Down

0 comments on commit 002ef41

Please sign in to comment.