|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * DISCLAIMER |
| 4 | + * |
| 5 | + * Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer versions in the future. |
| 6 | + * |
| 7 | + * @category Smile |
| 8 | + * @package Smile\ElasticsuiteAnalytics |
| 9 | + * @author Richard BAYET <richard.bayet@smile.fr> |
| 10 | + * @copyright 2025 Smile |
| 11 | + * @license Open Software License ("OSL") v. 3.0 |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Smile\ElasticsuiteTracker\Model\Data\Fixer\Event; |
| 15 | + |
| 16 | +use Magento\Framework\View\Layout\PageType\Config as PageTypeConfig; |
| 17 | +use Smile\ElasticsuiteCore\Api\Client\ClientInterface; |
| 18 | +use Smile\ElasticsuiteCore\Api\Index\IndexSettingsInterface; |
| 19 | +use Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Request\Query\Builder as QueryBuilder; |
| 20 | +use Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory; |
| 21 | +use Smile\ElasticsuiteCore\Search\Request\QueryInterface; |
| 22 | +use Smile\ElasticsuiteTracker\Api\EventIndexInterface; |
| 23 | +use Smile\ElasticsuiteTracker\Model\Data\Fixer\DataFixerInterface; |
| 24 | + |
| 25 | +/** |
| 26 | + * Behavioral data fixer for mistyped checkout_cart_add events where the page type identifier |
| 27 | + * is located in "page.identifier" instead of "page.type.identifier". |
| 28 | + * |
| 29 | + * @category Smile |
| 30 | + * @package Smile\ElasticsuiteTracker |
| 31 | + */ |
| 32 | +class MistypedAddToCartAddEvent implements DataFixerInterface |
| 33 | +{ |
| 34 | + /** |
| 35 | + * @var QueryFactory |
| 36 | + */ |
| 37 | + private $queryFactory; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var QueryBuilder |
| 41 | + */ |
| 42 | + private $queryBuilder; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var IndexSettingsInterface |
| 46 | + */ |
| 47 | + private $indexSettings; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var PageTypeConfig |
| 51 | + */ |
| 52 | + private $pageTypeConfig; |
| 53 | + |
| 54 | + /** |
| 55 | + * @var ClientInterface |
| 56 | + */ |
| 57 | + private $client; |
| 58 | + |
| 59 | + /** |
| 60 | + * Constructor. |
| 61 | + * |
| 62 | + * @param QueryFactory $queryFactory Query factory. |
| 63 | + * @param QueryBuilder $queryBuilder Query Builder. |
| 64 | + * @param IndexSettingsInterface $indexSettings Index settings. |
| 65 | + * @param PageTypeConfig $pageTypeConfig Layout page types config. |
| 66 | + * @param ClientInterface $client Elasticsearch client. |
| 67 | + */ |
| 68 | + public function __construct( |
| 69 | + QueryFactory $queryFactory, |
| 70 | + QueryBuilder $queryBuilder, |
| 71 | + IndexSettingsInterface $indexSettings, |
| 72 | + PageTypeConfig $pageTypeConfig, |
| 73 | + ClientInterface $client |
| 74 | + ) { |
| 75 | + $this->queryFactory = $queryFactory; |
| 76 | + $this->queryBuilder = $queryBuilder; |
| 77 | + $this->indexSettings = $indexSettings; |
| 78 | + $this->pageTypeConfig = $pageTypeConfig; |
| 79 | + $this->client = $client; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * {@inheritDoc} |
| 84 | + */ |
| 85 | + public function fixInvalidData(int $storeId): int |
| 86 | + { |
| 87 | + $result = DataFixerInterface::FIX_COMPLETE; |
| 88 | + |
| 89 | + try { |
| 90 | + $indexAlias = $this->indexSettings->getIndexAliasFromIdentifier( |
| 91 | + EventIndexInterface::INDEX_IDENTIFIER, |
| 92 | + $storeId |
| 93 | + ); |
| 94 | + |
| 95 | + $query = $this->queryBuilder->buildQuery( |
| 96 | + $this->queryFactory->create( |
| 97 | + QueryInterface::TYPE_BOOL, |
| 98 | + [ |
| 99 | + 'must' => [ |
| 100 | + $this->queryFactory->create( |
| 101 | + QueryInterface::TYPE_MISSING, |
| 102 | + ['field' => 'page.type.identifier'] |
| 103 | + ), |
| 104 | + $this->queryFactory->create( |
| 105 | + QueryInterface::TYPE_TERM, |
| 106 | + ['field' => 'page.identifier', 'value' => 'checkout_cart_add'] |
| 107 | + ), |
| 108 | + ], |
| 109 | + ] |
| 110 | + ) |
| 111 | + ); |
| 112 | + |
| 113 | + $updatePageIdentifier = <<<EOF |
| 114 | +if (!ctx._source.page.containsKey('type')) { ctx._source.page.type = params.typeInfo; } |
| 115 | +else { ctx._source.page.type.identifier=params.typeInfo.identifier; ctx._source.page.type.label=params.typeInfo.label; } |
| 116 | +ctx._source.page.remove('identifier'); |
| 117 | +ctx._source.page.remove('label'); |
| 118 | +EOF; |
| 119 | + $indicesNames = $this->client->getIndicesNameByAlias($indexAlias); |
| 120 | + foreach ($indicesNames as $indexName) { |
| 121 | + $params = [ |
| 122 | + 'index' => $indexName, |
| 123 | + 'body' => [ |
| 124 | + 'query' => $query, |
| 125 | + 'script' => [ |
| 126 | + 'source' => $updatePageIdentifier, |
| 127 | + 'lang' => 'painless', |
| 128 | + 'params' => [ |
| 129 | + 'typeInfo' => [ |
| 130 | + 'identifier' => 'checkout_cart_add', |
| 131 | + 'label' => stripslashes($this->getPageTypeLabel('checkout_cart_add')), |
| 132 | + ], |
| 133 | + ], |
| 134 | + ], |
| 135 | + 'conflicts' => 'proceed', |
| 136 | + ], |
| 137 | + ]; |
| 138 | + $this->client->updateByQuery($params); |
| 139 | + } |
| 140 | + } catch (\Exception $e) { |
| 141 | + $result = DataFixerInterface::FIX_FAILURE; |
| 142 | + } |
| 143 | + |
| 144 | + return $result; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Human-readable version of the page type identifier. |
| 149 | + * |
| 150 | + * @param string $pageTypeIdentifier Page type identifier. |
| 151 | + * |
| 152 | + * @return string |
| 153 | + */ |
| 154 | + private function getPageTypeLabel($pageTypeIdentifier) |
| 155 | + { |
| 156 | + foreach ($this->pageTypeConfig->getPageTypes() as $identifier => $pageType) { |
| 157 | + if ($pageTypeIdentifier === $identifier) { |
| 158 | + return $pageType['label']; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + return ''; |
| 163 | + } |
| 164 | +} |
0 commit comments