PageRenderTime 26ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/engine/Shopware/Bundle/AttributeBundle/Service/TableMapping.php

https://bitbucket.org/aoifescanlon/shopware-vanilla-install
PHP | 184 lines | 74 code | 19 blank | 91 comment | 4 complexity | 6852e7ea21fbb3a40b0e898fae478b5e MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, MIT, Apache-2.0
  1. <?php
  2. /**
  3. * Shopware 5
  4. * Copyright (c) shopware AG
  5. *
  6. * According to our dual licensing model, this program can be used either
  7. * under the terms of the GNU Affero General Public License, version 3,
  8. * or under a proprietary license.
  9. *
  10. * The texts of the GNU Affero General Public License with an additional
  11. * permission and of our proprietary license can be found at and
  12. * in the LICENSE file you have received along with this program.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * "Shopware" is a registered trademark of shopware AG.
  20. * The licensing of the program under the AGPLv3 does not imply a
  21. * trademark license. Therefore any rights, title and interest in
  22. * our trademarks remain entirely with us.
  23. */
  24. namespace Shopware\Bundle\AttributeBundle\Service;
  25. use Doctrine\DBAL\Connection;
  26. use Doctrine\DBAL\Schema\Column;
  27. /**
  28. * @category Shopware
  29. *
  30. * @copyright Copyright (c) shopware AG (http://www.shopware.com)
  31. */
  32. class TableMapping
  33. {
  34. /**
  35. * @var Connection
  36. */
  37. private $connection;
  38. /**
  39. * @var array
  40. */
  41. private $tables;
  42. /**
  43. * TableMapping constructor.
  44. *
  45. * @param Connection $connection
  46. * @param array $tableMapping
  47. */
  48. public function __construct(Connection $connection, array $tableMapping)
  49. {
  50. $this->connection = $connection;
  51. $this->tables = $tableMapping;
  52. }
  53. /**
  54. * @param string $table
  55. * @param string $name
  56. *
  57. * @throws \Exception
  58. *
  59. * @return bool
  60. */
  61. public function isIdentifierColumn($table, $name)
  62. {
  63. if (!array_key_exists($table, $this->tables)) {
  64. throw new \Exception(sprintf('Table %s is no attribute table', $table));
  65. }
  66. $config = $this->tables[$table];
  67. $identifiers = isset($config['identifiers']) ? $config['identifiers'] : [];
  68. $columns = array_map('strtolower', $identifiers);
  69. return in_array(strtolower($name), $columns);
  70. }
  71. /**
  72. * @param string $table
  73. * @param string $name
  74. *
  75. * @throws \Exception
  76. *
  77. * @return bool
  78. */
  79. public function isCoreColumn($table, $name)
  80. {
  81. if (!array_key_exists($table, $this->tables)) {
  82. throw new \Exception(sprintf('Table %s is no attribute table', $table));
  83. }
  84. $config = $this->tables[$table];
  85. $coreAttributes = isset($config['coreAttributes']) ? $config['coreAttributes'] : [];
  86. $columns = array_map('strtolower', $coreAttributes);
  87. return in_array(strtolower($name), $columns);
  88. }
  89. /**
  90. * @param $table
  91. *
  92. * @return null|string
  93. */
  94. public function getTableModel($table)
  95. {
  96. if (!array_key_exists($table, $this->tables)) {
  97. return null;
  98. }
  99. return $this->tables[$table]['model'];
  100. }
  101. /**
  102. * @return string[]
  103. */
  104. public function getAttributeTables()
  105. {
  106. return array_filter($this->tables, function ($table) {
  107. return !$table['readOnly'];
  108. });
  109. }
  110. /**
  111. * @param $table
  112. *
  113. * @return string
  114. */
  115. public function getTableForeignKey($table)
  116. {
  117. return $this->tables[$table]['foreignKey'];
  118. }
  119. /**
  120. * @param string $table
  121. *
  122. * @return bool
  123. */
  124. public function isAttributeTable($table)
  125. {
  126. return array_key_exists($table, $this->tables);
  127. }
  128. /**
  129. * @param string $table
  130. * @param string $column
  131. *
  132. * @return bool
  133. */
  134. public function isTableColumn($table, $column)
  135. {
  136. $columns = $this->connection->getSchemaManager()->listTableColumns($table);
  137. $names = array_map(function (Column $schemaColumn) {
  138. return strtolower($schemaColumn->getName());
  139. }, $columns);
  140. return in_array(strtolower($column), $names);
  141. }
  142. /**
  143. * @param string $table
  144. *
  145. * @throws \Exception
  146. *
  147. * @return array
  148. */
  149. public function getDependingTables($table)
  150. {
  151. if (!$this->isAttributeTable($table)) {
  152. throw new \Exception(sprintf('Table %s is no supported attribute table'));
  153. }
  154. return $this->tables[$table]['dependingTables'];
  155. }
  156. /**
  157. * @param string $table
  158. *
  159. * @return \Doctrine\DBAL\Schema\Column[]
  160. */
  161. public function getTableColumns($table)
  162. {
  163. return $this->connection->getSchemaManager()->listTableColumns($table);
  164. }
  165. }