Skip to content
This repository was archived by the owner on Nov 6, 2021. It is now read-only.

Commit d52370a

Browse files
author
Florian Horn
committed
Changed implementation of fuzzyfyr for Products and Categories to match with EQP of *Magento® 2*
1 parent 2993f51 commit d52370a

File tree

3 files changed

+60
-377
lines changed

3 files changed

+60
-377
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file, in reverse
77
- Added fuzzyfyr for CMS Pages
88
- Added fuzzyfyr for CMS Blocks
99
- Extended fuzzyfyr for Customers with address fuzzing
10+
- Changed implementation of fuzzyfyr for Products and Categories to match with EQP of *Magento® 2*
1011

1112
## 1.0.1
1213

Observer/CategoriesObserver.php

Lines changed: 28 additions & 188 deletions
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,32 @@
1313
use AllInData\ContentFuzzyfyr\Model\Configuration;
1414
use Magento\Catalog\Model\ResourceModel\Category\Collection as CategoryCollection;
1515
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
16-
use Magento\Framework\DB\Adapter\AdapterInterface;
16+
use Magento\Catalog\Model\ResourceModel\Category as CategoryResource;
17+
use Magento\Catalog\Model\ResourceModel\CategoryFactory as CategoryResourceFactory;
1718
use Magento\Framework\Event\ObserverInterface;
18-
use Magento\Framework\Setup\ModuleDataSetupInterface;
1919

2020
class CategoriesObserver implements ObserverInterface
2121
{
22-
/*
23-
* Flags
24-
*/
25-
const ENTITY_TYPE_CODE = 'catalog_category';
26-
const ENTITY_FIELD_TYPE_TEXT = 'text';
27-
const ENTITY_FIELD_TYPE_VARCHAR = 'varchar';
28-
2922
/**
3023
* @var CategoryCollectionFactory
3124
*/
3225
protected $categoryCollectionFactory;
3326
/**
34-
* @var ModuleDataSetupInterface
27+
* @var CategoryResourceFactory
3528
*/
36-
protected $setup;
29+
protected $categoryResourceFactory;
3730

3831
/**
39-
* CategoriesObserver constructor.
32+
* CategorysObserver constructor.
4033
* @param CategoryCollectionFactory $categoryCollectionFactory
41-
* @param ModuleDataSetupInterface $setup
34+
* @param CategoryResourceFactory $categoryResourceFactory
4235
*/
43-
public function __construct(
44-
CategoryCollectionFactory $categoryCollectionFactory,
45-
ModuleDataSetupInterface $setup
46-
) {
36+
public function __construct(CategoryCollectionFactory $categoryCollectionFactory, CategoryResourceFactory $categoryResourceFactory)
37+
{
4738
$this->categoryCollectionFactory = $categoryCollectionFactory;
48-
$this->setup = $setup;
39+
$this->categoryResourceFactory = $categoryResourceFactory;
4940
}
5041

51-
5242
/**
5343
* {@inheritdoc}
5444
*/
@@ -61,193 +51,43 @@ public function execute(\Magento\Framework\Event\Observer $observer)
6151
return;
6252
}
6353

64-
$db = $this->setup->getConnection()->startSetup();
54+
/** @var CategoryResource $categoryResource */
55+
$categoryResource = $this->categoryResourceFactory->create();
56+
6557
/** @var CategoryCollection $categoryCollection */
6658
$categoryCollection = $this->categoryCollectionFactory->create();
6759
$categoryCollection->load();
6860
foreach ($categoryCollection->getItems() as $category) {
6961
/** @var \Magento\Catalog\Model\Category $category */
70-
$this->updateData($db, $configuration, $category);
62+
$this->updateData($configuration, $category);
63+
$categoryResource->save($category);
7164
}
72-
$db->endSetup();
7365
}
7466

7567
/**
76-
* @param AdapterInterface $db
7768
* @param Configuration $configuration
7869
* @param \Magento\Catalog\Model\Category $category
7970
* @return \Magento\Catalog\Model\Category
80-
* @throws \Zend_Db_Statement_Exception
8171
*/
82-
protected function updateData(AdapterInterface $db, Configuration $configuration, \Magento\Catalog\Model\Category $category)
72+
protected function updateData(Configuration $configuration, \Magento\Catalog\Model\Category $category)
8373
{
84-
$this->pushData($db, $configuration, $category, 'description', $configuration->getDummyContentText());
85-
$this->pushData($db, $configuration, $category, 'meta_title', $configuration->getDummyContentText());
86-
$this->pushData($db, $configuration, $category, 'meta_keywords', $configuration->getDummyContentText());
87-
$this->pushData($db, $configuration, $category, 'meta_description', $configuration->getDummyContentText());
88-
89-
return $category;
90-
}
91-
92-
/**
93-
* @param AdapterInterface $db
94-
* @param Configuration $configuration
95-
* @param \Magento\Catalog\Model\Category $category
96-
* @param string $field
97-
* @param string $value
98-
* @throws \Zend_Db_Statement_Exception
99-
*/
100-
protected function pushData(AdapterInterface $db, Configuration $configuration, \Magento\Catalog\Model\Category $category, $field, $value)
101-
{
102-
// --- Field type TEXT
103-
if ($this->hasAttribute($db, $category, self::ENTITY_FIELD_TYPE_TEXT, $field)) {
104-
$this->updateAttributeByQuery($db, $category, self::ENTITY_FIELD_TYPE_TEXT, $field, $value, $configuration->isUseOnlyEmpty());
105-
} else {
106-
$this->insertAttributeByQuery($db, $category, self::ENTITY_FIELD_TYPE_TEXT, $field, $value);
107-
}
108-
109-
// --- Field type VARCHAR
110-
if ($this->hasAttribute($db, $category, self::ENTITY_FIELD_TYPE_VARCHAR, $field)) {
111-
$this->updateAttributeByQuery($db, $category, self::ENTITY_FIELD_TYPE_VARCHAR, $field, $value, $configuration->isUseOnlyEmpty());
112-
} else {
113-
$this->insertAttributeByQuery($db, $category, self::ENTITY_FIELD_TYPE_VARCHAR, $field, $value);
114-
}
115-
}
116-
117-
/**
118-
* @param AdapterInterface $db
119-
* @param \Magento\Catalog\Model\Category $category
120-
* @param string $fieldType
121-
* @param string $field
122-
* @return bool
123-
* @throws \Zend_Db_Statement_Exception
124-
*/
125-
protected function hasAttribute(AdapterInterface $db, \Magento\Catalog\Model\Category $category, $fieldType, $field)
126-
{
127-
$query = 'SELECT e.value
128-
FROM
129-
%1$s AS e
130-
WHERE
131-
e.attribute_id = :attributeid AND
132-
e.store_id = :storeid AND
133-
e.entity_id = :entityid';
134-
135-
$query = sprintf(
136-
$query,
137-
$db->getTableName('catalog_category_entity_' . $fieldType)
138-
);
139-
$stmt = $db->query($query, [
140-
':entityid' => $category->getEntityId(),
141-
':attributeid' => $this->getAttributeId($db, $field),
142-
':storeid' => 0
143-
]);
144-
145-
if (!$stmt->execute()) {
146-
return false;
74+
if (!$configuration->isUseOnlyEmpty() ||
75+
($configuration->isUseOnlyEmpty() && empty($category->getDescription()))) {
76+
$category->setDescription($configuration->getDummyContentText());
14777
}
148-
149-
return !!$stmt->rowCount();
150-
}
151-
152-
/**
153-
* @param AdapterInterface $db
154-
* @param string $field
155-
* @return string|bool FALSE if query fails
156-
* @throws \Zend_Db_Statement_Exception
157-
*/
158-
protected function getAttributeId(AdapterInterface $db, $field)
159-
{
160-
$query = 'SELECT ea.attribute_id
161-
FROM
162-
%1$s AS ea
163-
LEFT JOIN %2$s AS eet ON entity_type_code = :code
164-
WHERE
165-
eet.entity_type_id = ea.entity_type_id AND
166-
ea.attribute_code = :field';
167-
168-
$query = sprintf(
169-
$query,
170-
$db->getTableName('eav_attribute'),
171-
$db->getTableName('eav_entity_type')
172-
);
173-
$stmt = $db->query($query, [
174-
':field' => $field,
175-
':code' => self::ENTITY_TYPE_CODE
176-
]);
177-
178-
if (!$stmt->execute()) {
179-
return false;
78+
if (!$configuration->isUseOnlyEmpty() ||
79+
($configuration->isUseOnlyEmpty() && empty($category->getMetaTitle()))) {
80+
$category->setMetaTitle($configuration->getDummyContentText());
18081
}
181-
182-
$result = $stmt->fetch(\Zend_Db::FETCH_ASSOC);
183-
if (empty($result)) {
184-
return false;
82+
if (!$configuration->isUseOnlyEmpty() ||
83+
($configuration->isUseOnlyEmpty() && empty($category->getMetaKeywords()))) {
84+
$category->setMetaKeywords($configuration->getDummyContentText());
18585
}
186-
187-
return $result['attribute_id'] ?: false;
188-
}
189-
190-
/**
191-
* @param AdapterInterface $db
192-
* @param \Magento\Catalog\Model\Category $category
193-
* @param string $fieldType
194-
* @param string $field
195-
* @param string $value
196-
* @param boolean $useOnlyEmpty
197-
* @throws \Zend_Db_Statement_Exception
198-
*/
199-
protected function updateAttributeByQuery(AdapterInterface $db, \Magento\Catalog\Model\Category $category, $fieldType, $field, $value, $useOnlyEmpty)
200-
{
201-
$query = 'UPDATE
202-
%1$s AS e
203-
SET
204-
e.value = :value
205-
WHERE
206-
e.attribute_id = :attributeid AND
207-
e.store_id = :storeid AND
208-
e.entity_id = :entityid';
209-
210-
if ($useOnlyEmpty) {
211-
$query .= ' AND (e.value = "" OR e.value IS NULL)';
86+
if (!$configuration->isUseOnlyEmpty() ||
87+
($configuration->isUseOnlyEmpty() && empty($category->getMetaDescription()))) {
88+
$category->setMetaDescription($configuration->getDummyContentText());
21289
}
21390

214-
$queryText = sprintf(
215-
$query,
216-
$db->getTableName('catalog_category_entity_' . $fieldType)
217-
);
218-
$stmt = $db->query($queryText, [
219-
':entityid' => $category->getEntityId(),
220-
':attributeid' => $this->getAttributeId($db, $field),
221-
':storeid' => 0,
222-
':value' => $value
223-
]);
224-
$stmt->execute();
225-
}
226-
227-
/**
228-
* @param AdapterInterface $db
229-
* @param \Magento\Catalog\Model\Category $category
230-
* @param string $fieldType
231-
* @param string $field
232-
* @param string $value
233-
* @throws \Zend_Db_Statement_Exception
234-
*/
235-
protected function insertAttributeByQuery(AdapterInterface $db, \Magento\Catalog\Model\Category $category, $fieldType, $field, $value)
236-
{
237-
$query = 'INSERT IGNORE INTO
238-
%1$s (`attribute_id`, `store_id`, `entity_id`, `value`)
239-
VALUES (:attributeid, :storeid, :entityid, :value)';
240-
241-
$queryText = sprintf(
242-
$query,
243-
$db->getTableName('catalog_category_entity_' . $fieldType)
244-
);
245-
$stmt = $db->query($queryText, [
246-
':entityid' => $category->getEntityId(),
247-
':attributeid' => $this->getAttributeId($db, $field),
248-
':storeid' => 0,
249-
':value' => $value
250-
]);
251-
$stmt->execute();
91+
return $category;
25292
}
25393
}

0 commit comments

Comments
 (0)