FieldsProvider.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Mtf\Util\Generate\Fixture;
  7. use Magento\Framework\App\ResourceConnection;
  8. use Magento\Framework\ObjectManagerInterface;
  9. use Magento\Eav\Model\Config;
  10. use Magento\Framework\DB\Adapter\AdapterInterface;
  11. /**
  12. * Provider of fields from database.
  13. */
  14. class FieldsProvider
  15. {
  16. /**
  17. * EAV configuration.
  18. *
  19. * @var Config
  20. */
  21. protected $eavConfig;
  22. /**
  23. * Resources and connections registry and factory.
  24. *
  25. * @var Resource
  26. */
  27. protected $resource;
  28. /**
  29. * Magento connection.
  30. *
  31. * @var AdapterInterface
  32. */
  33. protected $connection;
  34. /**
  35. * @constructor
  36. * @param ObjectManagerInterface $objectManager
  37. */
  38. public function __construct(ObjectManagerInterface $objectManager)
  39. {
  40. $this->eavConfig = $objectManager->create(\Magento\Eav\Model\Config::class);
  41. $this->resource = $objectManager->create(\Magento\Framework\App\ResourceConnection::class);
  42. }
  43. /**
  44. * Check connection to DB.
  45. *
  46. * @return bool
  47. */
  48. public function checkConnection()
  49. {
  50. $this->connection = $this->getConnection('core');
  51. if (!$this->connection || $this->connection instanceof \Zend_Db_Adapter_Exception) {
  52. echo ('Connection to Magento 2 database is absent. Fixture data has not been fetched.' . PHP_EOL);
  53. return false;
  54. }
  55. return true;
  56. }
  57. /**
  58. * Collect fields for the entity based on its type.
  59. *
  60. * @param array $fixture
  61. * @return array
  62. */
  63. public function getFields(array $fixture)
  64. {
  65. $method = $fixture['type'] . 'CollectFields';
  66. if (!method_exists($this, $method)) {
  67. return [];
  68. }
  69. return $this->$method($fixture);
  70. }
  71. /**
  72. * Collect fields for the entity with eav type.
  73. *
  74. * @param array $fixture
  75. * @return array
  76. */
  77. protected function eavCollectFields(array $fixture)
  78. {
  79. $entityType = $fixture['entity_type'];
  80. $collection = $this->eavConfig->getEntityType($entityType)->getAttributeCollection();
  81. $attributes = [];
  82. foreach ($collection as $attribute) {
  83. if (isset($fixture['product_type'])) {
  84. $applyTo = $attribute->getApplyTo();
  85. if (!empty($applyTo) && !in_array($fixture['product_type'], $applyTo)) {
  86. continue;
  87. }
  88. }
  89. /** @var $attribute \Magento\Eav\Model\Entity\Attribute */
  90. $code = $attribute->getAttributeCode();
  91. $attributes[$code] = [
  92. 'attribute_code' => $code,
  93. 'backend_type' => $attribute->getBackendType(),
  94. 'is_required' => $attribute->getIsRequired(),
  95. 'default_value' => $attribute->getDefaultValue(),
  96. 'input' => $attribute->getFrontendInput(),
  97. ];
  98. }
  99. return $attributes;
  100. }
  101. /**
  102. * Collect fields for the entity with table type.
  103. *
  104. * @param array $fixture
  105. * @return array
  106. */
  107. protected function tableCollectFields(array $fixture)
  108. {
  109. return $this->flatCollectFields($fixture);
  110. }
  111. /**
  112. * Collect fields for the entity with flat type.
  113. *
  114. * @param array $fixture
  115. * @return array
  116. */
  117. protected function flatCollectFields(array $fixture)
  118. {
  119. $entityType = $fixture['entity_type'];
  120. /** @var $connection \Magento\Framework\DB\Adapter\AdapterInterface */
  121. $fields = $this->connection->describeTable($entityType);
  122. $attributes = [];
  123. foreach ($fields as $code => $field) {
  124. $attributes[$code] = [
  125. 'attribute_code' => $code,
  126. 'backend_type' => $field['DATA_TYPE'],
  127. 'is_required' => ($field['PRIMARY'] || $field['IDENTITY']),
  128. 'default_value' => $field['DEFAULT'],
  129. 'input' => '',
  130. ];
  131. }
  132. return $attributes;
  133. }
  134. /**
  135. * Collect fields for the entity with composite type.
  136. *
  137. * @param array $fixture
  138. * @return array
  139. */
  140. protected function compositeCollectFields(array $fixture)
  141. {
  142. $entityTypes = $fixture['entities'];
  143. $fields = [];
  144. foreach ($entityTypes as $entityType) {
  145. $fields = array_merge($fields, $this->connection->describeTable($entityType));
  146. }
  147. $attributes = [];
  148. foreach ($fields as $code => $field) {
  149. $attributes[$code] = [
  150. 'attribute_code' => $code,
  151. 'backend_type' => $field['DATA_TYPE'],
  152. 'is_required' => ($field['PRIMARY'] || $field['IDENTITY']),
  153. 'default_value' => $field['DEFAULT'],
  154. 'input' => '',
  155. ];
  156. }
  157. return $attributes;
  158. }
  159. /**
  160. * Retrieve connection to resource specified by $resourceName.
  161. *
  162. * @param string $resourceName
  163. * @return \Exception|false|\Magento\Framework\DB\Adapter\AdapterInterface
  164. */
  165. protected function getConnection($resourceName)
  166. {
  167. try {
  168. $connection = $this->resource->getConnection($resourceName);
  169. return $connection;
  170. } catch (\Exception $e) {
  171. echo $e->getMessage() . PHP_EOL;
  172. return $e;
  173. }
  174. }
  175. }