/vendor/magento/framework/ObjectManager/Code/Generator/Persistor.php

https://gitlab.com/yousafsyed/easternglamor · PHP · 495 lines · 400 code · 21 blank · 74 comment · 8 complexity · 97297f71976764fd672d8f20f68a9966 MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. // @codingStandardsIgnoreFile
  7. namespace Magento\Framework\ObjectManager\Code\Generator;
  8. /**
  9. * Class Persistor
  10. */
  11. class Persistor extends \Magento\Framework\Code\Generator\EntityAbstract
  12. {
  13. /**
  14. * Entity type
  15. */
  16. const ENTITY_TYPE = 'persistor';
  17. /**
  18. * Retrieve class properties
  19. *
  20. * @return array
  21. */
  22. protected function _getClassProperties()
  23. {
  24. $properties = [
  25. [
  26. 'name' => $this->_getSourceFactoryPropertyName(),
  27. 'visibility' => 'protected',
  28. 'docblock' => [
  29. 'shortDescription' => 'Entity factory',
  30. 'tags' => [
  31. [
  32. 'name' => 'var',
  33. 'description' => $this->getSourceClassName() . 'Factory',
  34. ],
  35. ],
  36. ],
  37. ],
  38. [
  39. 'name' => $this->_getSourceResourcePropertyName(),
  40. 'visibility' => 'protected',
  41. 'docblock' => [
  42. 'shortDescription' => 'Resource model',
  43. 'tags' => [
  44. [
  45. 'name' => 'var',
  46. 'description' => $this->_getSourceResourceClassName(),
  47. ],
  48. ],
  49. ]
  50. ],
  51. [
  52. 'name' => 'resource',
  53. 'visibility' => 'protected',
  54. 'docblock' => [
  55. 'shortDescription' => 'Application Resource',
  56. 'tags' => [
  57. [
  58. 'name' => 'var',
  59. 'description' => '\Magento\Framework\App\ResourceConnection',
  60. ],
  61. ],
  62. ]
  63. ],
  64. [
  65. 'name' => 'connection',
  66. 'visibility' => 'protected',
  67. 'docblock' => [
  68. 'shortDescription' => 'Database Adapter',
  69. 'tags' => [
  70. [
  71. 'name' => 'var',
  72. 'description' => '\Magento\Framework\DB\Adapter\AdapterInterface',
  73. ],
  74. ],
  75. ]
  76. ],
  77. [
  78. 'name' => 'entitiesPool',
  79. 'visibility' => 'protected',
  80. 'defaultValue' => [],
  81. 'docblock' => [
  82. 'shortDescription' => '',
  83. 'tags' => [
  84. [
  85. 'name' => 'var',
  86. 'description' => 'array',
  87. ],
  88. ],
  89. ]
  90. ],
  91. [
  92. 'name' => 'stack',
  93. 'visibility' => 'protected',
  94. 'defaultValue' => [],
  95. 'docblock' => [
  96. 'shortDescription' => '',
  97. 'tags' => [
  98. [
  99. 'name' => 'var',
  100. 'description' => 'array',
  101. ],
  102. ],
  103. ]
  104. ],
  105. ];
  106. return $properties;
  107. }
  108. /**
  109. * Returns source factory property name
  110. *
  111. * @return string
  112. */
  113. protected function _getSourceFactoryPropertyName()
  114. {
  115. return lcfirst($this->getSourceClassNameWithoutNamespace()) . 'Factory';
  116. }
  117. /**
  118. * Returns source collection factory property name
  119. * @return string
  120. */
  121. protected function _getSourceResourcePropertyName() // InvoiceResource
  122. {
  123. return lcfirst($this->getSourceClassNameWithoutNamespace()) . "Resource";
  124. }
  125. /**
  126. * Returns collection factory class name
  127. *
  128. * @return string
  129. */
  130. protected function _getSourceResourceClassName() // Invoice\Resource
  131. {
  132. $temporary = str_replace('\\Api\\Data\\', '\\Model\\Spi\\', $this->getSourceClassName());
  133. $parts = explode('\\', ltrim($temporary, '\\'));
  134. $className = array_pop($parts);
  135. $className = str_replace('Interface', '', $className);
  136. return '\\' . implode('\\', $parts) . '\\' . $className . 'ResourceInterface';
  137. }
  138. /**
  139. * Returns list of methods for class generator
  140. *
  141. * @return array
  142. */
  143. protected function _getClassMethods()
  144. {
  145. return [
  146. $this->_getDefaultConstructorDefinition(),
  147. $this->_getGetConnectionMethod(),
  148. $this->_getLoadEntityMethod(),
  149. $this->_getRegisterDeletedMethod(),
  150. $this->_getRegisterNewMethod(),
  151. $this->_getRegisterFromArrayMethod(),
  152. $this->_getDoPersistMethod(),
  153. $this->_getDoPersistEntityMethod()
  154. ];
  155. }
  156. /**
  157. * Get default constructor definition for generated class
  158. *
  159. * @return array
  160. */
  161. protected function _getDefaultConstructorDefinition()
  162. {
  163. return [
  164. 'name' => '__construct',
  165. 'parameters' => [
  166. [
  167. 'name' => $this->_getSourceResourcePropertyName(),
  168. 'type' => $this->_getSourceResourceClassName(),
  169. ],
  170. [
  171. 'name' => $this->_getSourceFactoryPropertyName(),
  172. 'type' => $this->getSourceClassName() . 'Factory'
  173. ],
  174. [
  175. 'name' => 'resource',
  176. 'type' => '\Magento\Framework\App\ResourceConnection'
  177. ],
  178. ],
  179. 'body' => "\$this->"
  180. . $this->_getSourceResourcePropertyName()
  181. . " = \$" . $this->_getSourceResourcePropertyName() . ";\n"
  182. . "\$this->"
  183. . $this->_getSourceFactoryPropertyName()
  184. . " = \$" . $this->_getSourceFactoryPropertyName() . ";\n"
  185. . "\$this->resource = \$resource;"
  186. ,
  187. 'docblock' => [
  188. 'shortDescription' => ucfirst(static::ENTITY_TYPE) . ' constructor',
  189. 'tags' => [
  190. [
  191. 'name' => 'param',
  192. 'description' => $this->_getSourceResourceClassName()
  193. . " \$" . $this->_getSourceResourcePropertyName(),
  194. ],
  195. [
  196. 'name' => 'param',
  197. 'description' => $this->getSourceClassName() . 'Factory'
  198. . " \$" . $this->_getSourceFactoryPropertyName()
  199. ],
  200. [
  201. 'name' => 'param',
  202. 'description' => '\Magento\Framework\App\ResourceConnection $resource'
  203. ],
  204. ],
  205. ]
  206. ];
  207. }
  208. /**
  209. * @return array
  210. */
  211. protected function _getGetConnectionMethod()
  212. {
  213. $body = "if (!\$this->connection) {\n"
  214. . " \$this->connection = \$this->resource->getConnection("
  215. . "\\Magento\\Framework\\App\\ResourceConnection::DEFAULT_CONNECTION);\n"
  216. . "}\n"
  217. . "return \$this->connection;";
  218. return [
  219. 'name' => 'getConnection',
  220. 'parameters' => [],
  221. 'body' => $body,
  222. 'docblock' => [
  223. 'shortDescription' => 'Returns Adapter interface',
  224. 'tags' => [
  225. [
  226. 'name' => 'return',
  227. 'description' => "array \\Magento\\Framework\\DB\\Adapter\\AdapterInterface",
  228. ],
  229. ],
  230. ]
  231. ];
  232. }
  233. /**
  234. * Returns register() method
  235. *
  236. * @return string
  237. */
  238. protected function _getLoadEntityMethod()
  239. {
  240. $body = "\$entity = \$this->{$this->_getSourceFactoryPropertyName()}->create()->load(\$key);\n"
  241. . "return \$entity;";
  242. return [
  243. 'name' => 'loadEntity',
  244. 'parameters' => [
  245. [
  246. 'name' => 'key',
  247. ],
  248. ],
  249. 'body' => $body,
  250. 'docblock' => [
  251. 'shortDescription' => 'Load entity by key',
  252. 'tags' => [
  253. [
  254. 'name' => 'param',
  255. 'description' => "int \$key",
  256. ],
  257. [
  258. 'name' => 'return',
  259. 'description' => $this->_getResultClassName() . " \$entity"
  260. ],
  261. ],
  262. ]
  263. ];
  264. }
  265. /**
  266. * Returns registerDelete() method
  267. *
  268. * @return string
  269. */
  270. protected function _getRegisterDeletedMethod()
  271. {
  272. $body = "\$hash = spl_object_hash(\$entity);"
  273. . "array_push(\$this->stack, \$hash);"
  274. . "\$this->entitiesPool[\$hash] = ["
  275. . " 'entity' => \$entity,"
  276. . " 'action' => 'removed'"
  277. . "];";
  278. return [
  279. 'name' => 'registerDeleted',
  280. 'parameters' => [
  281. [
  282. 'name' => 'entity',
  283. 'type' => $this->getSourceClassName(),
  284. ],
  285. ],
  286. 'body' => $body,
  287. 'docblock' => [
  288. 'shortDescription' => 'Register entity to delete',
  289. 'tags' => [
  290. [
  291. 'name' => 'param',
  292. 'description' => $this->getSourceClassName() . " \$entity",
  293. ],
  294. ],
  295. ]
  296. ];
  297. }
  298. /**
  299. * @return array
  300. */
  301. protected function _getDoPersistMethod()
  302. {
  303. $body = "\$ids = [];\n"
  304. . "\$this->getConnection()->beginTransaction();\n"
  305. . "try {\n"
  306. . " do {\n"
  307. . " \$hash = array_pop(\$this->stack);\n"
  308. . " if (isset(\$this->entitiesPool[\$hash])) {\n"
  309. . " \$data = \$this->entitiesPool[\$hash];\n"
  310. . " if (\$data['action'] == 'created') {\n"
  311. . " \$this->{$this->_getSourceResourcePropertyName()}->save(\$data['entity']);\n"
  312. . " \$ids[] = \$data['entity']->getId();\n"
  313. . " } else {\n"
  314. . " \$ids[] = \$data['entity']->getId();\n"
  315. . " \$this->{$this->_getSourceResourcePropertyName()}->delete(\$data['removed']);\n"
  316. . " }\n"
  317. . " }\n"
  318. . " unset(\$this->entitiesPool[\$hash]);\n"
  319. . " \$items--;\n"
  320. . " } while (!empty(\$this->entitiesPool) || \$items === 0);\n"
  321. . " \$this->getConnection()->commit();\n"
  322. . " return \$ids;\n"
  323. . "} catch (\\Exception \$e) {\n"
  324. . " \$this->getConnection()->rollback();\n"
  325. . " throw \$e;\n"
  326. . "}";
  327. return [
  328. 'name' => 'doPersist',
  329. 'parameters' => [
  330. [
  331. 'name' => 'items',
  332. 'defaultValue' => 0,
  333. ],
  334. ],
  335. 'body' => $body,
  336. 'docblock' => [
  337. 'shortDescription' => 'Perform persist operation',
  338. 'tags' => [
  339. [
  340. 'name' => 'param',
  341. 'description' => "int \$items",
  342. ],
  343. [
  344. 'name' => 'return',
  345. 'description' => "array",
  346. ],
  347. ],
  348. ]
  349. ];
  350. }
  351. /**
  352. * Returns registerDelete() method
  353. *
  354. * @return string
  355. */
  356. protected function _getDoPersistEntityMethod()
  357. {
  358. $body = "\$hash = spl_object_hash(\$entity);\n"
  359. . "if (isset(\$this->entitiesPool[\$hash])) {\n"
  360. . "\$tempStack = \$this->stack;\n"
  361. . "array_flip(\$tempStack);\n"
  362. . "unset(\$tempStack[\$hash]);\n"
  363. . "\$this->stack = array_flip(\$tempStack);\n"
  364. . "unset(\$this->entitiesPool[\$hash]);\n"
  365. . "}\n"
  366. . "\$this->registerNew(\$entity);\n"
  367. . "return \$this->doPersist(1);";
  368. return [
  369. 'name' => 'doPersistEntity',
  370. 'parameters' => [
  371. [
  372. 'name' => 'entity',
  373. 'type' => $this->getSourceClassName(),
  374. ],
  375. ],
  376. 'body' => $body,
  377. 'docblock' => [
  378. 'shortDescription' => 'Persist entity',
  379. 'tags' => [
  380. [
  381. 'name' => 'param',
  382. 'description' => $this->getSourceClassName() . " \$entity",
  383. ],
  384. ],
  385. ]
  386. ];
  387. }
  388. /**
  389. * Returns registerDelete() method
  390. *
  391. * @return string
  392. */
  393. protected function _getRegisterFromArrayMethod()
  394. {
  395. $body = "\$entity = \$this->{$this->_getSourceFactoryPropertyName()}->create(['data' => \$data]);\n"
  396. . "\$this->registerNew(\$entity);\n"
  397. . "return \$entity;";
  398. return [
  399. 'name' => 'registerFromArray',
  400. 'parameters' => [
  401. [
  402. 'name' => 'data',
  403. 'type' => 'array',
  404. ],
  405. ],
  406. 'body' => $body,
  407. 'docblock' => [
  408. 'shortDescription' => 'Register entity to create',
  409. 'tags' => [
  410. [
  411. 'name' => 'param',
  412. 'description' => "array \$data",
  413. ],
  414. [
  415. 'name' => 'param',
  416. 'description' => $this->getSourceClassName() . " \$entity",
  417. ],
  418. ],
  419. ]
  420. ];
  421. }
  422. /**
  423. * Returns registerDelete() method
  424. *
  425. * @return string
  426. */
  427. protected function _getRegisterNewMethod()
  428. {
  429. $body = "\$hash = spl_object_hash(\$entity);\n"
  430. . "\$data = [\n"
  431. . "'entity' => \$entity,\n"
  432. . "'action' => 'created'\n"
  433. . "];\n"
  434. . "array_push(\$this->stack, \$hash);\n"
  435. . "\$this->entitiesPool[\$hash] = \$data;";
  436. return [
  437. 'name' => 'registerNew',
  438. 'parameters' => [
  439. [
  440. 'name' => 'entity',
  441. 'type' => $this->getSourceClassName(),
  442. ],
  443. ],
  444. 'body' => $body,
  445. 'docblock' => [
  446. 'shortDescription' => 'Register entity to create',
  447. 'tags' => [
  448. [
  449. 'name' => 'param',
  450. 'description' => $this->getSourceClassName() . " \$entity",
  451. ],
  452. ],
  453. ]
  454. ];
  455. }
  456. /**
  457. * {@inheritdoc}
  458. */
  459. protected function _validateData()
  460. {
  461. $result = parent::_validateData();
  462. if ($result) {
  463. $sourceClassName = $this->getSourceClassName();
  464. $resultClassName = $this->_getResultClassName();
  465. if ($resultClassName !== $sourceClassName . 'Persistor') {
  466. $this->_addError(
  467. 'Invalid Factory class name [' . $resultClassName . ']. Use ' . $sourceClassName . 'Persistor'
  468. );
  469. $result = false;
  470. }
  471. }
  472. return $result;
  473. }
  474. }