-
-
Notifications
You must be signed in to change notification settings - Fork 16
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 #91 from Setono/add-resolvers
Add resolvers to make the plugin more extendable
- Loading branch information
Showing
28 changed files
with
468 additions
and
28 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
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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusAnalyticsPlugin\Event; | ||
|
||
use Setono\GoogleAnalyticsMeasurementProtocol\Request\Body\Event\Item\Item; | ||
|
||
/** | ||
* Is fired when an item has been resolved. Use this to do common manipulations on the item before it's sent to Google | ||
*/ | ||
final class ItemResolved | ||
{ | ||
public function __construct( | ||
public Item $item, | ||
/** @var array<string, mixed> $context */ | ||
public array $context = [], | ||
) { | ||
} | ||
} |
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
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,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusAnalyticsPlugin\Resolver\Brand; | ||
|
||
use Sylius\Component\Core\Model\ProductInterface; | ||
use Sylius\Component\Core\Model\ProductVariantInterface; | ||
|
||
interface BrandResolverInterface | ||
{ | ||
public function resolveFromProduct(ProductInterface $product): ?string; | ||
|
||
public function resolveFromProductVariant(ProductVariantInterface $productVariant): ?string; | ||
} |
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,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusAnalyticsPlugin\Resolver\Brand; | ||
|
||
use Setono\CompositeCompilerPass\CompositeService; | ||
use Sylius\Component\Core\Model\ProductInterface; | ||
use Sylius\Component\Core\Model\ProductVariantInterface; | ||
|
||
/** | ||
* @property list<BrandResolverInterface> $services | ||
* | ||
* @extends CompositeService<BrandResolverInterface> | ||
*/ | ||
final class CompositeBrandResolver extends CompositeService implements BrandResolverInterface | ||
{ | ||
public function resolveFromProduct(ProductInterface $product): ?string | ||
{ | ||
foreach ($this->services as $brandResolver) { | ||
$val = $brandResolver->resolveFromProduct($product); | ||
if (null !== $val) { | ||
return $val; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function resolveFromProductVariant(ProductVariantInterface $productVariant): ?string | ||
{ | ||
foreach ($this->services as $brandResolver) { | ||
$val = $brandResolver->resolveFromProductVariant($productVariant); | ||
if (null !== $val) { | ||
return $val; | ||
} | ||
} | ||
|
||
return 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,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusAnalyticsPlugin\Resolver\Category; | ||
|
||
use Sylius\Component\Core\Model\ProductInterface; | ||
use Sylius\Component\Core\Model\ProductVariantInterface; | ||
use Sylius\Component\Taxonomy\Model\TaxonInterface; | ||
|
||
final class CategoryResolver implements CategoryResolverInterface | ||
{ | ||
public function resolveFromProduct(ProductInterface $product): array | ||
{ | ||
$taxon = $product->getMainTaxon() ?? $product->getTaxons()->first(); | ||
if (!$taxon instanceof TaxonInterface) { | ||
return []; | ||
} | ||
|
||
/** | ||
* Presume the $product has this taxon hierarchy: Apparel > Shirts > Crew > Short sleeve | ||
* After the call below, $hierarchy will be ['Crew', 'Shirts', 'Apparel']. Notice that the values will | ||
* actually still be taxon objects and not strings yet, but I am just showing it like this for readability | ||
*/ | ||
$hierarchy = $taxon->getAncestors()->toArray(); | ||
|
||
/** | ||
* Now we prepend the 'Short sleeve' to the $hierarchy variable, so the resulting array is now: | ||
* ['Short sleeve', 'Crew', 'Shirts', 'Apparel'] | ||
*/ | ||
array_unshift($hierarchy, $taxon); | ||
|
||
/** | ||
* Now we just need to reverse the array to make sure the top level taxon is first and so on, | ||
* and finally map the taxon objects to strings and the returned array will be ['Apparel', 'Shirts', 'Crew', 'Short sleeve'] | ||
*/ | ||
return array_values(array_map( | ||
static function (TaxonInterface $taxon): string { | ||
return (string) $taxon->getName(); | ||
}, | ||
array_reverse($hierarchy), | ||
)); | ||
} | ||
|
||
public function resolveFromProductVariant(ProductVariantInterface $productVariant): array | ||
{ | ||
$product = $productVariant->getProduct(); | ||
if (!$product instanceof ProductInterface) { | ||
return []; | ||
} | ||
|
||
return $this->resolveFromProduct($product); | ||
} | ||
} |
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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusAnalyticsPlugin\Resolver\Category; | ||
|
||
use Sylius\Component\Core\Model\ProductInterface; | ||
use Sylius\Component\Core\Model\ProductVariantInterface; | ||
|
||
/** | ||
* A category is used on the item when a purchase event, add to cart, and many similar events are fired. | ||
* It tells Google which category a specific product belongs to. | ||
* | ||
* The methods in this interface must return a list of categories, starting with the top level category at index 0. | ||
* This means if you have a category (breadcrumb) like Apparel > Shirts > Crew > Short sleeve the resulting array would | ||
* look like this: | ||
* | ||
* [ | ||
* 'Apparel', 'Shirts', 'Crew', 'Short sleeve' | ||
* ] | ||
*/ | ||
interface CategoryResolverInterface | ||
{ | ||
/** | ||
* @return list<string> | ||
*/ | ||
public function resolveFromProduct(ProductInterface $product): array; | ||
|
||
/** | ||
* @return list<string> | ||
*/ | ||
public function resolveFromProductVariant(ProductVariantInterface $productVariant): array; | ||
} |
Oops, something went wrong.