1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\ConfigurableProduct\Model\Attribute;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\Attribute\LockValidatorInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\EntityManager\MetadataPool;
/**
* Class LockValidator
*/
class LockValidator implements LockValidatorInterface
{
/**
* @var Resource
*/
protected $resource;
/**
* @var MetadataPool
*/
private $metadataPool;
/**
* Constructor
*
* @param ResourceConnection $resource
*/
public function __construct(
ResourceConnection $resource
) {
$this->resource = $resource;
}
/**
* Check attribute lock state
*
* @param \Magento\Framework\Model\AbstractModel $object
* @param null $attributeSet
* @throws \Magento\Framework\Exception\LocalizedException
* @return void
*/
public function validate(\Magento\Framework\Model\AbstractModel $object, $attributeSet = null)
{
$metadata = $this->getMetadataPool()->getMetadata(ProductInterface::class);
$connection = $this->resource->getConnection();
$bind = ['attribute_id' => $object->getAttributeId()];
$select = clone $connection->select();
$select->reset()
->from(
['main_table' => $this->resource->getTableName('catalog_product_super_attribute')],
['psa_count' => 'COUNT(product_super_attribute_id)']
)->join(
['entity' => $this->resource->getTableName('catalog_product_entity')],
'main_table.product_id = entity.' . $metadata->getLinkField()
)->where('main_table.attribute_id = :attribute_id')
->group('main_table.attribute_id')
->limit(1);
if ($attributeSet !== null) {
$bind['attribute_set_id'] = $attributeSet;
$select->where('entity.attribute_set_id = :attribute_set_id');
}
if ($connection->fetchOne($select, $bind)) {
throw new \Magento\Framework\Exception\LocalizedException(
__('This attribute is used in configurable products.')
);
}
}
/**
* Get MetadataPool instance
* @return MetadataPool
*/
private function getMetadataPool()
{
if (!$this->metadataPool) {
$this->metadataPool = ObjectManager::getInstance()->get(MetadataPool::class);
}
return $this->metadataPool;
}
}