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

https://gitlab.com/yousafsyed/easternglamor · PHP · 579 lines · 512 code · 11 blank · 56 comment · 2 complexity · 637627788a65e2cecb5981ec9a3a6d54 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 Repository
  10. */
  11. class Repository extends \Magento\Framework\Code\Generator\EntityAbstract
  12. {
  13. /**
  14. * Entity type
  15. */
  16. const ENTITY_TYPE = 'repository';
  17. /**
  18. * No Such Entity Exception
  19. */
  20. const NO_SUCH_ENTITY_EXCEPTION = '\\Magento\Framework\Exception\NoSuchEntityException';
  21. const INPUT_EXCEPTION = '\\Magento\Framework\Exception\InputException';
  22. const SEARCH_CRITERIA = '\\Magento\Framework\Api\SearchCriteria';
  23. /**
  24. * Retrieve class properties
  25. *
  26. * @return array
  27. */
  28. protected function _getClassProperties()
  29. {
  30. $properties = [
  31. [
  32. 'name' => $this->_getSourcePersistorPropertyName(),
  33. 'visibility' => 'protected',
  34. 'docblock' => [
  35. 'shortDescription' => $this->_getSourcePersistorPropertyName(),
  36. 'tags' => [
  37. [
  38. 'name' => 'var',
  39. 'description' => $this->_getPersistorClassName(),
  40. ],
  41. ],
  42. ],
  43. ],
  44. [
  45. 'name' => $this->_getSourceCollectionFactoryPropertyName(),
  46. 'visibility' => 'protected',
  47. 'docblock' => [
  48. 'shortDescription' => 'Collection Factory',
  49. 'tags' => [
  50. [
  51. 'name' => 'var',
  52. 'description' => $this->_getCollectionFactoryClassName(),
  53. ],
  54. ],
  55. ]
  56. ],
  57. [
  58. 'name' => 'registry',
  59. 'visibility' => 'protected',
  60. 'defaultValue' => [],
  61. 'docblock' => [
  62. 'shortDescription' => $this->getSourceClassName() . '[]',
  63. 'tags' => [
  64. [
  65. 'name' => 'var',
  66. 'description' => 'array',
  67. ],
  68. ],
  69. ]
  70. ],
  71. [
  72. 'name' => 'extensionAttributesJoinProcessor',
  73. 'visibility' => 'protected',
  74. 'docblock' => [
  75. 'shortDescription' => 'Extension attributes join processor.',
  76. 'tags' => [
  77. [
  78. 'name' => 'var',
  79. 'description' => '\Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface',
  80. ],
  81. ],
  82. ]
  83. ],
  84. ];
  85. return $properties;
  86. }
  87. /**
  88. * Returns source factory property name
  89. *
  90. * @return string
  91. */
  92. protected function _getSourcePersistorPropertyName()
  93. {
  94. return lcfirst($this->getSourceClassNameWithoutNamespace()) . 'Persistor';
  95. }
  96. /**
  97. * Returns source collection factory property name
  98. * @return string
  99. */
  100. protected function _getSourceCollectionFactoryPropertyName()
  101. {
  102. return lcfirst($this->getSourceClassNameWithoutNamespace()) . 'SearchResultFactory';
  103. }
  104. /**
  105. * Returns collection factory class name
  106. *
  107. * @return string
  108. */
  109. protected function _getCollectionFactoryClassName()
  110. {
  111. return
  112. str_replace('Interface', '', $this->getSourceClassName()) . 'SearchResultInterfaceFactory';
  113. }
  114. /**
  115. * Returns source persistor class name
  116. *
  117. * @return string
  118. */
  119. protected function _getPersistorClassName()
  120. {
  121. $target = $this->getSourceClassName();
  122. // if (substr($target, -9) == 'Interface') {
  123. // $target = substr($target, 1, strlen($target) -9);
  124. // }
  125. return $target . 'Persistor';
  126. }
  127. /**
  128. * Get default constructor definition for generated class
  129. *
  130. * @return array
  131. */
  132. protected function _getDefaultConstructorDefinition()
  133. {
  134. return [
  135. 'name' => '__construct',
  136. 'parameters' => [
  137. [
  138. 'name' => $this->_getSourcePersistorPropertyName(),
  139. 'type' => $this->_getPersistorClassName(),
  140. ],
  141. [
  142. 'name' => $this->_getSourceCollectionFactoryPropertyName(),
  143. 'type' => $this->_getCollectionFactoryClassName(),
  144. ],
  145. [
  146. 'name' => 'extensionAttributesJoinProcessor',
  147. 'type' => '\Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface',
  148. ],
  149. ],
  150. 'body' => "\$this->"
  151. . $this->_getSourcePersistorPropertyName()
  152. . " = \$" . $this->_getSourcePersistorPropertyName() . ";\n"
  153. . "\$this->"
  154. . $this->_getSourceCollectionFactoryPropertyName()
  155. . " = \$" . $this->_getSourceCollectionFactoryPropertyName() . ";\n"
  156. . "\$this->extensionAttributesJoinProcessor = \$extensionAttributesJoinProcessor;"
  157. ,
  158. 'docblock' => [
  159. 'shortDescription' => ucfirst(static::ENTITY_TYPE) . ' constructor',
  160. 'tags' => [
  161. [
  162. 'name' => 'param',
  163. 'description' => $this->getSourceClassName() . " \$" . $this->_getSourcePersistorPropertyName(),
  164. ],
  165. [
  166. 'name' => 'param',
  167. 'description' => $this->_getCollectionFactoryClassName()
  168. . " \$" . $this->_getSourceCollectionFactoryPropertyName()
  169. ],
  170. [
  171. 'name' => 'param',
  172. 'description' => '\Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface'
  173. . " \$extensionAttributesJoinProcessor"
  174. ],
  175. ],
  176. ]
  177. ];
  178. }
  179. /**
  180. * Returns get() method
  181. *
  182. * @return string
  183. */
  184. protected function _getGetMethod()
  185. {
  186. $body = "if (!\$id) {\n"
  187. . " throw new " . self::INPUT_EXCEPTION . "('ID required');\n"
  188. . "}\n"
  189. . "if (!isset(\$this->registry[\$id])) {\n"
  190. . " \$entity = \$this->" . $this->_getSourcePersistorPropertyName()
  191. . "->loadEntity(\$id);\n"
  192. . " if (!\$entity->getId()) {\n"
  193. . " throw new " . self::NO_SUCH_ENTITY_EXCEPTION . "('Requested entity doesn\\'t exist');\n"
  194. . " }\n"
  195. . " \$this->registry[\$id] = \$entity;\n"
  196. . "}\n"
  197. . "return \$this->registry[\$id];";
  198. return [
  199. 'name' => 'get',
  200. 'parameters' => [
  201. [
  202. 'name' => 'id',
  203. 'type' => 'int',
  204. ],
  205. ],
  206. 'body' => $body,
  207. 'docblock' => [
  208. 'shortDescription' => 'load entity',
  209. 'tags' => [
  210. [
  211. 'name' => 'param',
  212. 'description' => 'int $id',
  213. ],
  214. [
  215. 'name' => 'return',
  216. 'description' => $this->getSourceClassName(),
  217. ],
  218. [
  219. 'name' => 'throws',
  220. 'description' => self::INPUT_EXCEPTION,
  221. ],
  222. [
  223. 'name' => 'throws',
  224. 'description' => self::NO_SUCH_ENTITY_EXCEPTION,
  225. ],
  226. ],
  227. ]
  228. ];
  229. }
  230. /**
  231. * Returns register() method
  232. *
  233. * @return string
  234. */
  235. protected function _getCreateFromArrayMethod()
  236. {
  237. $body = "return \$this->{$this->_getSourcePersistorPropertyName()}->registerFromArray(\$data);";
  238. return [
  239. 'name' => 'createFromArray',
  240. 'parameters' => [
  241. [
  242. 'name' => 'data',
  243. 'type' => 'array',
  244. ],
  245. ],
  246. 'body' => $body,
  247. 'docblock' => [
  248. 'shortDescription' => 'Register entity to create',
  249. 'tags' => [
  250. [
  251. 'name' => 'param',
  252. 'description' => 'array $data',
  253. ],
  254. [
  255. 'name' => 'return',
  256. 'description' => $this->_getResultClassName(),
  257. ],
  258. ],
  259. ]
  260. ];
  261. }
  262. /**
  263. * Returns register() method
  264. *
  265. * @return string
  266. */
  267. protected function _getCreateMethod()
  268. {
  269. $body = "return \$this->{$this->_getSourcePersistorPropertyName()}->registerNew(\$entity);";
  270. return [
  271. 'name' => 'create',
  272. 'parameters' => [
  273. [
  274. 'name' => 'entity',
  275. 'type' => $this->getSourceClassName(),
  276. ],
  277. ],
  278. 'body' => $body,
  279. 'docblock' => [
  280. 'shortDescription' => 'Register entity to create',
  281. 'tags' => [
  282. [
  283. 'name' => 'param',
  284. 'description' => 'array $data',
  285. ],
  286. [
  287. 'name' => 'return',
  288. 'description' => $this->getSourceClassName(),
  289. ],
  290. ],
  291. ]
  292. ];
  293. }
  294. /**
  295. * Returns register() method
  296. *
  297. * @return string
  298. */
  299. protected function _getFlushMethod()
  300. {
  301. $body = "\$ids = \$this->{$this->_getSourcePersistorPropertyName()}->doPersist();\n"
  302. . "foreach (\$ids as \$id) {\n"
  303. . "unset(\$this->registry[\$id]);\n"
  304. . "}";
  305. return [
  306. 'name' => 'flush',
  307. 'parameters' => [],
  308. 'body' => $body,
  309. 'docblock' => [
  310. 'shortDescription' => 'Perform persist operations',
  311. 'tags' => [],
  312. ]
  313. ];
  314. }
  315. /**
  316. * Returns persist() method
  317. *
  318. * @return string
  319. */
  320. protected function _getSaveMethod()
  321. {
  322. $body = "\$this->{$this->_getSourcePersistorPropertyName()}->doPersistEntity(\$entity);\n"
  323. . "return \$entity;";
  324. return [
  325. 'name' => 'save',
  326. 'parameters' => [
  327. [
  328. 'name' => 'entity',
  329. 'type' => $this->getSourceClassName(),
  330. ],
  331. ],
  332. 'body' => $body,
  333. 'docblock' => [
  334. 'shortDescription' => 'Perform persist operations for one entity',
  335. 'tags' => [
  336. [
  337. 'name' => 'param',
  338. 'description' => $this->getSourceClassName() . " \$entity",
  339. ],
  340. [
  341. 'name' => 'return',
  342. 'description' => $this->getSourceClassName(),
  343. ],
  344. ],
  345. ]
  346. ];
  347. }
  348. /**
  349. * Return remove() method
  350. *
  351. * @return array
  352. */
  353. protected function _getDeleteMethod()
  354. {
  355. $body = "\$this->{$this->_getSourcePersistorPropertyName()}->registerDeleted(\$entity);\n"
  356. . "return \$this->{$this->_getSourcePersistorPropertyName()}->doPersistEntity(\$entity);";
  357. return [
  358. 'name' => 'delete',
  359. 'parameters' => [
  360. [
  361. 'name' => 'entity',
  362. 'type' => $this->getSourceClassName(),
  363. ],
  364. ],
  365. 'body' => $body,
  366. 'docblock' => [
  367. 'shortDescription' => 'Register entity to delete',
  368. 'tags' => [
  369. [
  370. 'name' => 'param',
  371. 'description' => $this->getSourceClassName() . ' $entity',
  372. ],
  373. [
  374. 'name' => 'return',
  375. 'description' => 'bool',
  376. ],
  377. ],
  378. ]
  379. ];
  380. }
  381. /**
  382. * Return remove() method
  383. *
  384. * @return array
  385. */
  386. protected function _getDeleteByIdMethod()
  387. {
  388. $body = "\$entity = \$this->get(\$id);\n"
  389. . "\$this->{$this->_getSourcePersistorPropertyName()}->registerDeleted(\$entity);\n"
  390. . "return \$this->{$this->_getSourcePersistorPropertyName()}->doPersistEntity(\$entity);";
  391. return [
  392. 'name' => 'deleteById',
  393. 'parameters' => [
  394. [
  395. 'name' => 'id',
  396. 'type' => 'int',
  397. ],
  398. ],
  399. 'body' => $body,
  400. 'docblock' => [
  401. 'shortDescription' => 'Delete entity by Id',
  402. 'tags' => [
  403. [
  404. 'name' => 'param',
  405. 'description' => 'int $id',
  406. ],
  407. [
  408. 'name' => 'return',
  409. 'description' => 'bool',
  410. ],
  411. ],
  412. ]
  413. ];
  414. }
  415. /**
  416. * Return remove() method
  417. *
  418. * @return array
  419. */
  420. protected function _getRemoveMethod()
  421. {
  422. $body = "\$this->{$this->_getSourcePersistorPropertyName()}->registerDeleted(\$entity);";
  423. return [
  424. 'name' => 'remove',
  425. 'parameters' => [
  426. [
  427. 'name' => 'entity',
  428. 'type' => $this->getSourceClassName(),
  429. ],
  430. ],
  431. 'body' => $body,
  432. 'docblock' => [
  433. 'shortDescription' => 'Register entity to delete',
  434. 'tags' => [
  435. [
  436. 'name' => 'param',
  437. 'description' => $this->getSourceClassName() . ' $entity',
  438. ],
  439. ],
  440. ]
  441. ];
  442. }
  443. /**
  444. * Returns getList() method
  445. *
  446. * @return string
  447. */
  448. protected function _getGetListMethod()
  449. {
  450. $body = "\$collection = \$this->" . $this->_getSourceCollectionFactoryPropertyName() . "->create();\n"
  451. . "\$this->extensionAttributesJoinProcessor->process(\$collection);\n"
  452. . "foreach (\$searchCriteria->getFilterGroups() as \$filterGroup) {\n"
  453. . " foreach (\$filterGroup->getFilters() as \$filter) {\n"
  454. . " \$condition = \$filter->getConditionType() ? \$filter->getConditionType() : 'eq';\n"
  455. . " \$collection->addFieldToFilter(\$filter->getField(), [\$condition => \$filter->getValue()]);\n"
  456. . " }\n"
  457. . "}\n"
  458. . "\$collection->setCurPage(\$searchCriteria->getCurrentPage());\n"
  459. . "\$collection->setPageSize(\$searchCriteria->getPageSize());\n"
  460. . "return \$collection;\n";
  461. return [
  462. 'name' => 'getList',
  463. 'parameters' => [
  464. [
  465. 'name' => 'searchCriteria',
  466. 'type' => self::SEARCH_CRITERIA,
  467. ],
  468. ],
  469. 'body' => $body,
  470. 'docblock' => [
  471. 'shortDescription' => 'Find entities by criteria',
  472. 'tags' => [
  473. [
  474. 'name' => 'param',
  475. 'description' => self::SEARCH_CRITERIA . ' $searchCriteria',
  476. ],
  477. [
  478. 'name' => 'return',
  479. 'description' => $this->getSourceClassName() . '[]',
  480. ],
  481. ],
  482. ]
  483. ];
  484. }
  485. /**
  486. * Returns list of methods for class generator
  487. *
  488. * @return array
  489. */
  490. protected function _getClassMethods()
  491. {
  492. return [
  493. $this->_getDefaultConstructorDefinition(),
  494. $this->_getGetMethod(),
  495. $this->_getCreateMethod(),
  496. $this->_getCreateFromArrayMethod(),
  497. $this->_getGetListMethod(),
  498. $this->_getRemoveMethod(),
  499. $this->_getDeleteMethod(),
  500. $this->_getDeleteByIdMethod(),
  501. $this->_getFlushMethod(),
  502. $this->_getSaveMethod()
  503. ];
  504. }
  505. /**
  506. * {@inheritdoc}
  507. */
  508. protected function _validateData()
  509. {
  510. $result = parent::_validateData();
  511. if ($result) {
  512. $sourceClassName = $this->getSourceClassName();
  513. $resultClassName = $this->_getResultClassName();
  514. if ($resultClassName !== str_replace('Interface', '', $sourceClassName) . '\\Repository') {
  515. $this->_addError(
  516. 'Invalid Factory class name [' . $resultClassName . ']. Use ' . $sourceClassName . 'Repository'
  517. );
  518. $result = false;
  519. }
  520. }
  521. return $result;
  522. }
  523. /**
  524. * Generate code
  525. *
  526. * @return string
  527. */
  528. protected function _generateCode()
  529. {
  530. $className = str_replace('Interface', '', str_replace('Data\\', '', $this->getSourceClassName()));
  531. $this->_classGenerator->setName(
  532. $this->_getResultClassName()
  533. )->addProperties(
  534. $this->_getClassProperties()
  535. )->addMethods(
  536. $this->_getClassMethods()
  537. )->setClassDocBlock(
  538. $this->_getClassDocBlock()
  539. )->setImplementedInterfaces(
  540. [
  541. $className . 'RepositoryInterface',
  542. ]
  543. );
  544. return $this->_getGeneratedCode();
  545. }
  546. /**
  547. * Get source class name
  548. *
  549. * @return string
  550. */
  551. public function getSourceClassName()
  552. {
  553. return parent::getSourceClassName() . 'Interface';
  554. }
  555. }