Skip to content

Commit 907e97c

Browse files
committed
Made the event subscribers shop context aware
1 parent 8afe1fd commit 907e97c

27 files changed

+166
-155
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/features export-ignore
33
/spec export-ignore
44
/tests export-ignore
5+
/.editorconfig export-ignore
56
/.gitattributes export-ignore
67
/.gitignore export-ignore
78
/.scrutinizer.yml export-ignore

composer.json

+12-22
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,10 @@
1111
"license": "MIT",
1212
"require": {
1313
"php": "^7.2",
14+
"psr/event-dispatcher": "^1.0",
1415
"setono/tag-bag-bundle": "^1.0",
1516
"sylius/sylius": "^1.3.0",
16-
"thecodingmachine/safe": "^0.1.14"
17-
},
18-
"conflict": {
19-
"friends-of-behat/symfony-extension": ">=2.0.5 <= 2.0.8",
20-
"symfony/browser-kit": "4.1.8",
21-
"symfony/dependency-injection": "4.1.8",
22-
"symfony/doctrine-bridge": "4.3.0",
23-
"symfony/dom-crawler": "4.1.8",
24-
"symfony/routing": "4.1.8",
25-
"symfony/symfony": "4.1.8"
17+
"thecodingmachine/safe": "^0.1.16"
2618
},
2719
"require-dev": {
2820
"behat/behat": "^3.4",
@@ -34,24 +26,26 @@
3426
"friends-of-behat/suite-settings-extension": "^1.0",
3527
"friends-of-behat/symfony-extension": "^2.0",
3628
"friends-of-behat/variadic-extension": "^1.1",
29+
"korbeil/phpstan-generic-rules": "^0.2.1",
3730
"lakion/mink-debug-extension": "^1.2.3",
3831
"localheinz/composer-normalize": "^1.1",
3932
"phpspec/phpspec": "^5.0",
40-
"phpstan/phpstan-doctrine": "^0.10",
41-
"phpstan/phpstan-shim": "^0.10",
42-
"phpstan/phpstan-symfony": "^0.10",
43-
"phpstan/phpstan-webmozart-assert": "^0.10",
44-
"phpunit/phpunit": "^7.0",
33+
"phpstan/phpstan": "^0.11",
34+
"phpstan/phpstan-doctrine": "^0.11",
35+
"phpstan/phpstan-strict-rules": "^0.11",
36+
"phpstan/phpstan-webmozart-assert": "^0.11",
37+
"phpunit/phpunit": "^8.0",
4538
"roave/security-advisories": "dev-master",
4639
"sensiolabs/security-checker": "^5.0",
4740
"setono/sylius-tag-bag-plugin": "^1.0",
48-
"sylius-labs/coding-standard": "^2.0",
41+
"sylius-labs/coding-standard": "^3.1",
4942
"symfony/browser-kit": "^3.4 || ^4.1",
5043
"symfony/debug-bundle": "^3.4 || ^4.1",
5144
"symfony/dotenv": "^4.2",
5245
"symfony/intl": "^3.4 || ^4.1",
5346
"symfony/web-profiler-bundle": "^3.4 || ^4.1",
54-
"symfony/web-server-bundle": "^3.4 || ^4.1"
47+
"symfony/web-server-bundle": "^3.4 || ^4.1",
48+
"thecodingmachine/phpstan-safe-rule": "^0.1"
5549
},
5650
"config": {
5751
"sort-packages": true
@@ -74,12 +68,8 @@
7468
},
7569
"prefer-stable": true,
7670
"scripts": {
77-
"analyse": [
78-
"@ensure-test-container-exists",
79-
"./vendor/bin/phpstan analyse -c phpstan.neon -l max src"
80-
],
71+
"analyse": "vendor/bin/phpstan analyse -c phpstan.neon -l max src",
8172
"check-style": "vendor/bin/ecs check --ansi spec src",
82-
"ensure-test-container-exists": "[[ -f tests/Application/var/cache/test/ApplicationTests_Setono_SyliusFacebookTrackingPlugin_Application_KernelTestDebugContainer.xml ]] || (cd tests/Application && php -d memory_limit=-1 bin/console cache:clear --env=test)",
8373
"fix-style": "vendor/bin/ecs check --ansi spec src --fix",
8474
"phpspec": "vendor/bin/phpspec run --ansi",
8575
"phpunit": "vendor/bin/phpunit",

phpstan.neon

+17-18
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
includes:
2-
- vendor/phpstan/phpstan-doctrine/extension.neon
3-
- vendor/phpstan/phpstan-symfony/extension.neon
4-
- vendor/phpstan/phpstan-webmozart-assert/extension.neon
2+
- vendor/korbeil/phpstan-generic-rules/extension.neon
3+
- vendor/phpstan/phpstan-doctrine/extension.neon
4+
- vendor/phpstan/phpstan-strict-rules/rules.neon
5+
- vendor/phpstan/phpstan-webmozart-assert/extension.neon
6+
- vendor/thecodingmachine/phpstan-safe-rule/phpstan-safe-rule.neon
57

68
parameters:
7-
reportUnmatchedIgnoredErrors: false
9+
reportUnmatchedIgnoredErrors: true
810

9-
symfony:
10-
container_xml_path: tests/Application/var/cache/test/ApplicationTests_Setono_SyliusFacebookTrackingPlugin_Application_KernelTestDebugContainer.xml
11+
excludes_analyse:
12+
# Makes PHPStan crash
13+
- 'src/DependencyInjection/Configuration.php'
1114

12-
excludes_analyse:
13-
# Makes PHPStan crash
14-
- 'src/DependencyInjection/Configuration.php'
15-
16-
# Test dependencies
17-
- 'tests/Application/app/**.php'
18-
- 'tests/Application/src/**.php'
19-
20-
ignoreErrors:
21-
- '/Parameter #1 $configuration of method Symfony\Component\DependencyInjection\Extension\Extension::processConfiguration() expects Symfony\Component\Config\Definition\ConfigurationInterface, Symfony\Component\Config\Definition\ConfigurationInterface|null given./'
22-
- '/Method Symfony\\Contracts\\EventDispatcher\\EventDispatcherInterface\:\:dispatch\(\) invoked with 2 parameters, 1 required\./'
23-
- '/Parameter \#1 \$event of method Symfony\\Contracts\\EventDispatcher\\EventDispatcherInterface\:\:dispatch\(\) expects object, string given\./'
15+
ignoreErrors:
16+
- '/Parameter #1 $configuration of method Symfony\Component\DependencyInjection\Extension\Extension::processConfiguration() expects Symfony\Component\Config\Definition\ConfigurationInterface, Symfony\Component\Config\Definition\ConfigurationInterface|null given./'
17+
- '/Method Setono\\SyliusFacebookTrackingPlugin\\Builder\\BuilderInterface\:\:create\(\) has no return typehint specified\./'
18+
- '/Method Setono\\SyliusFacebookTrackingPlugin\\Builder\\BuilderInterface\:\:createFromJson\(\) has no return typehint specified\./'
19+
- '/Method Setono\\SyliusFacebookTrackingPlugin\\Builder\\Builder\:\:create\(\) has no return typehint specified\./'
20+
- '/Method Setono\\SyliusFacebookTrackingPlugin\\Builder\\Builder\:\:createFromJson\(\) has no return typehint specified\./'
21+
- '/\:\:addContent\(\) has parameter \$content with no typehint specified/'
22+
- '/\:\:addContentId\(\) has parameter \$contentId with no typehint specified/'

src/Builder/AddToCartBuilder.php

-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,4 @@ final class AddToCartBuilder extends Builder
1111
ContentTypeAwareBuilderTrait,
1212
ValueAwareBuilderTrait
1313
;
14-
15-
public const EVENT_NAME = 'setono_sylius_facebook_tracking.builder.add_to_cart';
1614
}

src/Builder/Builder.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
abstract class Builder implements BuilderInterface
1212
{
1313
public const CONTENT_TYPE_PRODUCT = 'product';
14+
1415
public const CONTENT_TYPE_PRODUCT_GROUP = 'product_group';
1516

16-
/**
17-
* @var array
18-
*/
17+
/** @var array */
1918
protected $data = [];
2019

2120
private function __construct()
@@ -27,6 +26,9 @@ public static function create()
2726
return new static();
2827
}
2928

29+
/**
30+
* @throws JsonException
31+
*/
3032
public static function createFromJson(string $json)
3133
{
3234
$new = new static();
@@ -41,8 +43,6 @@ public function getData(): array
4143
}
4244

4345
/**
44-
* @return string
45-
*
4646
* @throws JsonException
4747
*/
4848
public function getJson(): string

src/Builder/ContentBuilder.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88

99
final class ContentBuilder extends Builder
1010
{
11-
public const EVENT_NAME = 'setono_sylius_facebook_tracking.builder.item';
12-
11+
/**
12+
* @param mixed $id
13+
*/
1314
public function setId($id): self
1415
{
1516
Assert::scalar($id);

src/Builder/ContentIdsAwareBuilderTrait.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88

99
trait ContentIdsAwareBuilderTrait
1010
{
11-
/**
12-
* @var array
13-
*/
11+
/** @var array */
1412
protected $data = [];
1513

14+
/**
15+
* @param string|int $contentId
16+
*/
1617
public function addContentId($contentId): self
1718
{
1819
if (!isset($this->data['content_ids'])) {

src/Builder/ContentNameAwareBuilderTrait.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66

77
trait ContentNameAwareBuilderTrait
88
{
9-
/**
10-
* @var array
11-
*/
9+
/** @var array */
1210
protected $data = [];
1311

1412
public function setContentName(string $contentName): self

src/Builder/ContentTypeAwareBuilderTrait.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88

99
trait ContentTypeAwareBuilderTrait
1010
{
11-
/**
12-
* @var array
13-
*/
11+
/** @var array */
1412
protected $data = [];
1513

1614
public function setContentType(string $contentType): self

src/Builder/ContentsAwareBuilderTrait.php

+11-4
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,29 @@
55
namespace Setono\SyliusFacebookTrackingPlugin\Builder;
66

77
use InvalidArgumentException;
8+
use Safe\Exceptions\StringsException;
9+
use function Safe\sprintf;
810

911
trait ContentsAwareBuilderTrait
1012
{
11-
/**
12-
* @var array
13-
*/
13+
/** @var array */
1414
protected $data = [];
1515

16+
/**
17+
* @param array|BuilderInterface $content
18+
*
19+
* @throws StringsException
20+
*/
1621
public function addContent($content): self
1722
{
1823
if ($content instanceof BuilderInterface) {
1924
$content = $content->getData();
2025
}
2126

2227
if (!is_array($content)) {
23-
throw new InvalidArgumentException(sprintf('The $content parameter needs to be an array or instance of %s', BuilderInterface::class));
28+
throw new InvalidArgumentException(sprintf(
29+
'The $content parameter needs to be an array or instance of %s', BuilderInterface::class
30+
));
2431
}
2532

2633
if (!isset($this->data['contents'])) {

src/Builder/PurchaseBuilder.php

-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,4 @@ final class PurchaseBuilder extends Builder
1111
ContentTypeAwareBuilderTrait,
1212
ValueAwareBuilderTrait
1313
;
14-
15-
public const EVENT_NAME = 'setono_sylius_facebook_tracking.builder.purchase';
1614
}

src/Builder/ValueAwareBuilderTrait.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66

77
trait ValueAwareBuilderTrait
88
{
9-
/**
10-
* @var array
11-
*/
9+
/** @var array */
1210
protected $data = [];
1311

1412
public function setCurrency(string $currency): self

src/Builder/ViewContentBuilder.php

-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,4 @@ final class ViewContentBuilder extends Builder
1212
ContentTypeAwareBuilderTrait,
1313
ValueAwareBuilderTrait
1414
;
15-
16-
public const EVENT_NAME = 'setono_sylius_facebook_tracking.builder.view_content';
1715
}

src/Context/PixelContext.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,10 @@
99

1010
final class PixelContext implements PixelContextInterface
1111
{
12-
/**
13-
* @var ChannelContextInterface
14-
*/
12+
/** @var ChannelContextInterface */
1513
private $channelContext;
1614

17-
/**
18-
* @var PixelRepositoryInterface
19-
*/
15+
/** @var PixelRepositoryInterface */
2016
private $propertyRepository;
2117

2218
public function __construct(ChannelContextInterface $channelContext, PixelRepositoryInterface $propertyRepository)

src/DependencyInjection/Configuration.php

-3
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ public function getConfigTreeBuilder(): TreeBuilder
4040
return $treeBuilder;
4141
}
4242

43-
/**
44-
* @param ArrayNodeDefinition $node
45-
*/
4643
private function addResourcesSection(ArrayNodeDefinition $node): void
4744
{
4845
$node

src/Event/BuilderEvent.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99

1010
final class BuilderEvent extends Event
1111
{
12-
/**
13-
* @var BuilderInterface
14-
*/
12+
/** @var BuilderInterface */
1513
private $builder;
1614

15+
/** @var mixed|null */
1716
private $subject;
1817

18+
/**
19+
* @param mixed|null $subject
20+
*/
1921
public function __construct(BuilderInterface $builder, $subject = null)
2022
{
2123
$this->builder = $builder;
@@ -27,6 +29,9 @@ public function getBuilder(): BuilderInterface
2729
return $this->builder;
2830
}
2931

32+
/**
33+
* @return mixed|null
34+
*/
3035
public function getSubject()
3136
{
3237
return $this->subject;

src/EventListener/AddLibrarySubscriber.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ public static function getSubscribedEvents(): array
2424

2525
public function add(GetResponseEvent $event): void
2626
{
27-
if (!$event->isMasterRequest()) {
27+
$request = $event->getRequest();
28+
29+
if (!$event->isMasterRequest() || !$this->isShopContext($request)) {
2830
return;
2931
}
3032

3133
// Only add the library on 'real' page loads, not AJAX requests like add to cart
32-
if ($event->getRequest()->isXmlHttpRequest()) {
34+
if ($request->isXmlHttpRequest()) {
3335
return;
3436
}
3537

src/EventListener/AddToCartSubscriber.php

+10-8
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,24 @@
1414
use Setono\TagBagBundle\TagBag\TagBagInterface;
1515
use Sylius\Component\Core\Model\OrderInterface;
1616
use Sylius\Component\Order\Context\CartContextInterface;
17+
use Symfony\Bundle\SecurityBundle\Security\FirewallMap;
1718
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
19+
use Symfony\Component\HttpFoundation\RequestStack;
1820

1921
final class AddToCartSubscriber extends TagSubscriber
2022
{
21-
/**
22-
* @var CartContextInterface
23-
*/
23+
/** @var CartContextInterface */
2424
private $cartContext;
2525

2626
public function __construct(
2727
TagBagInterface $tagBag,
2828
PixelContextInterface $pixelContext,
2929
EventDispatcherInterface $eventDispatcher,
30-
CartContextInterface $cartContext
30+
CartContextInterface $cartContext,
31+
RequestStack $requestStack,
32+
FirewallMap $firewallMap
3133
) {
32-
parent::__construct($tagBag, $pixelContext, $eventDispatcher);
34+
parent::__construct($tagBag, $pixelContext, $eventDispatcher, $requestStack, $firewallMap);
3335

3436
$this->cartContext = $cartContext;
3537
}
@@ -45,7 +47,7 @@ public static function getSubscribedEvents(): array
4547

4648
public function track(): void
4749
{
48-
if (!$this->hasPixels()) {
50+
if (!$this->isShopContext() || !$this->hasPixels()) {
4951
return;
5052
}
5153

@@ -75,12 +77,12 @@ public function track(): void
7577
->setItemPrice($this->moneyFormatter->format($item->getDiscountedUnitPrice()))
7678
;
7779

78-
$this->dispatch(ContentBuilder::EVENT_NAME, new BuilderEvent($contentBuilder, $item));
80+
$this->eventDispatcher->dispatch(new BuilderEvent($contentBuilder, $item));
7981

8082
$builder->addContent($contentBuilder);
8183
}
8284

83-
$this->dispatch(AddToCartBuilder::EVENT_NAME, new BuilderEvent($builder, $order));
85+
$this->eventDispatcher->dispatch(new BuilderEvent($builder, $order));
8486

8587
$this->tagBag->add(new FbqTag(
8688
Tags::TAG_ADD_TO_CART,

0 commit comments

Comments
 (0)