vendor/roothirsch/core-bundle/Translation/Entity/TranslationScope.php line 31

Open in your IDE?
  1. <?php
  2. namespace Roothirsch\CoreBundle\Translation\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Symfony\Component\Validator\Constraints as Assert;
  7. use ApiPlatform\Core\Annotation\ApiResource as ApiResource;
  8. use ApiPlatform\Core\Annotation\ApiSubresource;
  9. use Gedmo\Mapping\Annotation as Gedmo;
  10. /**
  11. * @ORM\Entity(repositoryClass="Roothirsch\CoreBundle\Translation\Repository\TranslationScopeRepository")
  12. * @ORM\Table(name="translation_scope")
  13. * @ApiResource(
  14. * shortName="Translation/Scope",
  15. * itemOperations={
  16. * "get"={"requirements"={"id"=".+"}},
  17. * "put",
  18. * "delete",
  19. * "push_translations"={
  20. * "route_name": "api_translation_scopes_push_translations_item",
  21. * "method"="put",
  22. * "requirements"={"id"=".+"}
  23. * }
  24. * }
  25. * )
  26. */
  27. class TranslationScope
  28. {
  29. /**
  30. * @ORM\Id
  31. * @ORM\GeneratedValue
  32. * @ORM\Column(type="integer")
  33. */
  34. private $id;
  35. /**
  36. * @ORM\Column(name="namespace",type="string", length=255)
  37. * @Assert\NotBlank()
  38. * @var string
  39. */
  40. private $namespace;
  41. /**
  42. * @ORM\Column(type="string", length=255)
  43. * @Assert\NotBlank()
  44. * @var string
  45. */
  46. private $pushToken;
  47. /**
  48. * The DateTime of the creation date
  49. * @ORM\Column(type="datetime")
  50. * @var \DateTime $created
  51. * @Gedmo\Timestampable(on="create")
  52. */
  53. private $created;
  54. /**
  55. * The DateTime of the creation date
  56. * @ORM\Column(type="datetime", nullable=true)
  57. * @var \DateTime $created
  58. * @Gedmo\Timestampable(on="update")
  59. */
  60. private $updated;
  61. /**
  62. * The actual translations
  63. * @var ArrayCollection
  64. * @ORM\OneToMany(targetEntity="Roothirsch\CoreBundle\Translation\Entity\TranslationUnit", mappedBy="scope", cascade={"persist", "remove"}, orphanRemoval=true)
  65. * @ApiSubresource
  66. */
  67. private $units;
  68. /**
  69. * @var ArrayCollection
  70. * @ORM\OneToMany(targetEntity="Roothirsch\CoreBundle\Translation\Entity\TranslationUnit", mappedBy="scope", orphanRemoval=true)
  71. */
  72. private $translations;
  73. public function __construct()
  74. {
  75. $this->generatePushToken();
  76. }
  77. public function __toString()
  78. {
  79. return $this->namespace;
  80. }
  81. /**
  82. * @return int
  83. */
  84. public function getId()
  85. {
  86. return $this->id;
  87. }
  88. /**
  89. * @return string
  90. */
  91. public function getNamespace()
  92. {
  93. return $this->namespace;
  94. }
  95. /**
  96. * @param string $namespace
  97. */
  98. public function setNamespace($namespace)
  99. {
  100. $this->namespace = $namespace;
  101. }
  102. /**
  103. * @return \DateTime
  104. */
  105. public function getCreated()
  106. {
  107. return $this->created;
  108. }
  109. /**
  110. * @param \DateTime $created
  111. */
  112. public function setCreated($created)
  113. {
  114. $this->created = $created;
  115. }
  116. /**
  117. * @return \DateTime
  118. */
  119. public function getUpdated()
  120. {
  121. return $this->updated;
  122. }
  123. /**
  124. * @param \DateTime $updated
  125. */
  126. public function setUpdated($updated)
  127. {
  128. $this->updated = $updated;
  129. }
  130. /**
  131. * @return mixed
  132. */
  133. public function getUnits()
  134. {
  135. return $this->units;
  136. }
  137. /**
  138. * @param mixed $units
  139. */
  140. public function setUnits($units)
  141. {
  142. $this->units = $units;
  143. }
  144. /**
  145. * @return mixed
  146. */
  147. public function getPushToken()
  148. {
  149. return $this->pushToken;
  150. }
  151. /**
  152. * @param mixed $pushToken
  153. */
  154. public function setPushToken($pushToken)
  155. {
  156. $this->pushToken = $pushToken;
  157. }
  158. public function generatePushToken($length = 32)
  159. {
  160. $this->pushToken = substr(bin2hex(random_bytes($length)), 0, $length);
  161. }
  162. /**
  163. * @return ArrayCollection
  164. */
  165. public function getTranslations()
  166. {
  167. return $this->translations;
  168. }
  169. /**
  170. * @param ArrayCollection $translations
  171. */
  172. public function setTranslations(ArrayCollection $translations): void
  173. {
  174. $this->translations = $translations;
  175. }
  176. public function getCompleteness()
  177. {
  178. $status = [
  179. 'complete' => 0,
  180. 'total' => 0,
  181. ];
  182. foreach ($this->getTranslations() as $translation) {
  183. if ($translation->getSource() != null) {
  184. continue;
  185. }
  186. if ($translation->isComplete() == true) {
  187. $status['complete']++;
  188. }
  189. $status['total']++;
  190. }
  191. return $status;
  192. }
  193. }