vendor/pimcore/pimcore/models/Dependency.php line 112

Open in your IDE?
  1. <?php
  2. /**
  3. * Pimcore
  4. *
  5. * This source file is available under two different licenses:
  6. * - GNU General Public License version 3 (GPLv3)
  7. * - Pimcore Commercial License (PCL)
  8. * Full copyright and license information is available in
  9. * LICENSE.md which is distributed with this source code.
  10. *
  11. * @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12. * @license http://www.pimcore.org/license GPLv3 and PCL
  13. */
  14. namespace Pimcore\Model;
  15. /**
  16. * @internal
  17. *
  18. * @method Dependency\Dao getDao()
  19. * @method void save()
  20. */
  21. class Dependency extends AbstractModel
  22. {
  23. /**
  24. * The ID of the object to get dependencies for
  25. *
  26. * @var int
  27. */
  28. protected $sourceId;
  29. /**
  30. * The type of the object to get dependencies for
  31. *
  32. * @var string
  33. */
  34. protected $sourceType;
  35. /**
  36. * Contains the ID/type of objects which are required for the given source object (sourceId/sourceType)
  37. *
  38. * @var array
  39. */
  40. protected $requires = [];
  41. /**
  42. * Static helper to get the dependencies for the given sourceId & type
  43. *
  44. * @param int $id
  45. * @param string $type
  46. *
  47. * @return Dependency
  48. */
  49. public static function getBySourceId($id, $type)
  50. {
  51. $d = new self();
  52. $d->getDao()->getBySourceId($id, $type);
  53. return $d;
  54. }
  55. /**
  56. * Add a requirement to the source object
  57. *
  58. * @param int $id
  59. * @param string $type
  60. */
  61. public function addRequirement($id, $type)
  62. {
  63. $this->requires[] = [
  64. 'type' => $type,
  65. 'id' => $id,
  66. ];
  67. }
  68. /**
  69. * Used when element gets deleted. Removes entries (by source = element) and
  70. * schedules a sanity check for the affected targets.
  71. *
  72. * @param Element\ElementInterface $element
  73. */
  74. public function cleanAllForElement($element)
  75. {
  76. $this->getDao()->cleanAllForElement($element);
  77. }
  78. /**
  79. * Cleanup the dependencies for current source id.
  80. * Can be used for updating the dependencies.
  81. */
  82. public function clean()
  83. {
  84. $this->requires = [];
  85. $this->getDao()->clear();
  86. }
  87. /**
  88. * @return int
  89. */
  90. public function getSourceId()
  91. {
  92. return $this->sourceId;
  93. }
  94. /**
  95. * @param int|null $offset
  96. * @param int|null $limit
  97. *
  98. * @return array
  99. */
  100. public function getRequires($offset = null, $limit = null)
  101. {
  102. return array_slice($this->requires, $offset, $limit);
  103. }
  104. /**
  105. * @param int|null $offset
  106. * @param int|null $limit
  107. *
  108. * @return array
  109. */
  110. public function getRequiredBy($offset = null, $limit = null)
  111. {
  112. return $this->getDao()->getRequiredBy($offset, $limit);
  113. }
  114. /**
  115. * @param string|null $orderBy
  116. * @param string|null $orderDirection
  117. * @param int|null $offset
  118. * @param int|null $limit
  119. *
  120. * @return array
  121. */
  122. public function getRequiredByWithPath($offset = null, $limit = null, $orderBy = null, $orderDirection = null)
  123. {
  124. return $this->getDao()->getRequiredByWithPath($offset, $limit, $orderBy, $orderDirection);
  125. }
  126. /**
  127. * @param int $sourceId
  128. *
  129. * @return $this
  130. */
  131. public function setSourceId($sourceId)
  132. {
  133. $this->sourceId = (int) $sourceId;
  134. return $this;
  135. }
  136. /**
  137. * @param array $requires
  138. *
  139. * @return $this
  140. */
  141. public function setRequires($requires)
  142. {
  143. $this->requires = $requires;
  144. return $this;
  145. }
  146. /**
  147. * @return string
  148. */
  149. public function getSourceType()
  150. {
  151. return $this->sourceType;
  152. }
  153. /**
  154. * @param string $sourceType
  155. *
  156. * @return $this
  157. */
  158. public function setSourceType($sourceType)
  159. {
  160. $this->sourceType = $sourceType;
  161. return $this;
  162. }
  163. /**
  164. * @return int
  165. */
  166. public function getRequiresTotalCount()
  167. {
  168. return count($this->requires);
  169. }
  170. /**
  171. * @return int
  172. */
  173. public function getRequiredByTotalCount()
  174. {
  175. return $this->getDao()->getRequiredByTotalCount();
  176. }
  177. /**
  178. * Check if the source object is required by an other object (an other object depends on this object)
  179. *
  180. * @return bool
  181. */
  182. public function isRequired()
  183. {
  184. return $this->getRequiredByTotalCount() > 0;
  185. }
  186. }