|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Baldwin\UrlDataIntegrityChecker\Checker\Catalog\Category\UrlKey; |
| 6 | + |
| 7 | +use Baldwin\UrlDataIntegrityChecker\Checker\Catalog\Category\UrlKey as UrlKeyChecker; |
| 8 | +use Baldwin\UrlDataIntegrityChecker\Util\Stores as StoresUtil; |
| 9 | +use Magento\Catalog\Api\Data\CategoryInterface; |
| 10 | +use Magento\Catalog\Model\Attribute\ScopeOverriddenValueFactory as AttributeScopeOverriddenValueFactory; |
| 11 | +use Magento\Catalog\Model\Category as CategoryModel; |
| 12 | +use Magento\Catalog\Model\ResourceModel\Category\Collection as CategoryCollection; |
| 13 | +use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory; |
| 14 | +use Magento\Store\Model\Store; |
| 15 | + |
| 16 | +class EmptyUrlKey |
| 17 | +{ |
| 18 | + const EMPTY_PROBLEM_DESCRIPTION = 'Category has an empty url_key value. This needs to be fixed.'; |
| 19 | + |
| 20 | + private $storesUtil; |
| 21 | + private $categoryCollectionFactory; |
| 22 | + private $attributeScopeOverriddenValueFactory; |
| 23 | + |
| 24 | + public function __construct( |
| 25 | + StoresUtil $storesUtil, |
| 26 | + CategoryCollectionFactory $categoryCollectionFactory, |
| 27 | + AttributeScopeOverriddenValueFactory $attributeScopeOverriddenValueFactory |
| 28 | + ) { |
| 29 | + $this->storesUtil = $storesUtil; |
| 30 | + $this->categoryCollectionFactory = $categoryCollectionFactory; |
| 31 | + $this->attributeScopeOverriddenValueFactory = $attributeScopeOverriddenValueFactory; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @return array<array<string, mixed>> |
| 36 | + */ |
| 37 | + public function execute(): array |
| 38 | + { |
| 39 | + $categoryData = $this->checkForEmptyUrlKeyAttributeValues(); |
| 40 | + |
| 41 | + return $categoryData; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @return array<array<string, mixed>> |
| 46 | + */ |
| 47 | + private function checkForEmptyUrlKeyAttributeValues(): array |
| 48 | + { |
| 49 | + $categoriesWithProblems = []; |
| 50 | + |
| 51 | + $storeIds = $this->storesUtil->getAllStoreIds(); |
| 52 | + foreach ($storeIds as $storeId) { |
| 53 | + // we need a left join when using the non-default store view |
| 54 | + // and especially for the case where storeId 0 doesn't have a value set for this attribute |
| 55 | + $joinType = $storeId === Store::DEFAULT_STORE_ID ? 'inner' : 'left'; |
| 56 | + |
| 57 | + $collection = $this->categoryCollectionFactory->create(); |
| 58 | + $collection |
| 59 | + ->setStoreId($storeId) |
| 60 | + ->addAttributeToSelect(UrlKeyChecker::URL_KEY_ATTRIBUTE) |
| 61 | + ->addAttributeToSelect('name') |
| 62 | + ->addAttributeToFilter('level', ['gt' => 1]) // cats with levels 0 or 1 aren't used in the frontend |
| 63 | + ->addAttributeToFilter('entity_id', ['neq' => CategoryModel::TREE_ROOT_ID]) |
| 64 | + ->addAttributeToFilter([ |
| 65 | + [ |
| 66 | + 'attribute' => UrlKeyChecker::URL_KEY_ATTRIBUTE, |
| 67 | + 'null' => true, |
| 68 | + ], |
| 69 | + [ |
| 70 | + 'attribute' => UrlKeyChecker::URL_KEY_ATTRIBUTE, |
| 71 | + 'eq' => '', |
| 72 | + ], |
| 73 | + ], null, $joinType) |
| 74 | + ; |
| 75 | + |
| 76 | + $categoriesWithProblems[] = $this->getCategoriesWithProblems($storeId, $collection); |
| 77 | + } |
| 78 | + |
| 79 | + if (!empty($categoriesWithProblems)) { |
| 80 | + $categoriesWithProblems = array_merge(...$categoriesWithProblems); |
| 81 | + } |
| 82 | + |
| 83 | + return $categoriesWithProblems; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @param CategoryCollection<CategoryModel> $collection |
| 88 | + * |
| 89 | + * @return array<array<string, mixed>> |
| 90 | + */ |
| 91 | + private function getCategoriesWithProblems(int $storeId, CategoryCollection $collection): array |
| 92 | + { |
| 93 | + $problems = []; |
| 94 | + |
| 95 | + foreach ($collection as $category) { |
| 96 | + $isOverridden = $this |
| 97 | + ->attributeScopeOverriddenValueFactory |
| 98 | + ->create() |
| 99 | + ->containsValue(CategoryInterface::class, $category, UrlKeyChecker::URL_KEY_ATTRIBUTE, $storeId) |
| 100 | + ; |
| 101 | + |
| 102 | + if ($isOverridden || $storeId === Store::DEFAULT_STORE_ID) { |
| 103 | + $problems[] = [ |
| 104 | + 'catId' => (int) $category->getEntityId(), |
| 105 | + 'name' => $category->getName(), |
| 106 | + 'storeId' => $storeId, |
| 107 | + 'problem' => self::EMPTY_PROBLEM_DESCRIPTION, |
| 108 | + ]; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return $problems; |
| 113 | + } |
| 114 | +} |
0 commit comments