src/Domain/Blog/Model/Article.php line 22

Open in your IDE?
  1. <?php
  2. namespace Whater\Domain\Blog\Model;
  3. use BornFree\TacticianDomainEvent\Recorder\ContainsRecordedEvents;
  4. use BornFree\TacticianDomainEvent\Recorder\EventRecorderCapabilities;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Ramsey\Uuid\Uuid;
  8. use Assert\Assertion;
  9. use Whater\Domain\Blog\Exception\InvalidArticleStatusException;
  10. use Whater\Domain\Blog\Model\Translation\TranslatableInterface;
  11. use Whater\Domain\Blog\Model\Translation\TranslatableTrait;
  12. use Whater\Domain\Blog\Model\Translation\TranslationInterface;
  13. use Whater\Domain\Common\Exception\InvalidUUIDException;
  14. /**
  15. * Class Article
  16. *
  17. * @package Whater\Domain\Blog\Model
  18. */
  19. class Article implements ContainsRecordedEvents
  20. {
  21. use EventRecorderCapabilities;
  22. public const DEFAULT_LOCALE = 'es';
  23. public const ARTICLE_DRAFT = "AS_DRAFT";
  24. public const ARTICLE_PUBLIC = "AS_PUBLIC";
  25. /**
  26. * @var string
  27. */
  28. private $uuid;
  29. /**
  30. * @var string
  31. */
  32. private $status;
  33. /**
  34. * @var Collection
  35. */
  36. private $categories;
  37. /**
  38. * @var Collection
  39. */
  40. private $translations;
  41. /**
  42. * @var string
  43. */
  44. private $headerImageUrl;
  45. /**
  46. * @var \DateTime
  47. */
  48. private $publishAt;
  49. /**
  50. * @var string
  51. */
  52. private $defaultLocale;
  53. /**
  54. * @var null|\DateTime
  55. */
  56. private $createdAt;
  57. /**
  58. * @var null|\DateTime
  59. */
  60. private $updatedAt;
  61. /**
  62. * @var bool
  63. */
  64. private $showInBlog;
  65. /**
  66. * @var bool
  67. */
  68. private $showInWhaterpoints;
  69. /**
  70. * @var Collection
  71. */
  72. private $linkedWhaterpoints;
  73. /**
  74. * @var bool
  75. */
  76. private $showInDistributionNetworks;
  77. /**
  78. * @var Collection
  79. */
  80. private $linkedDistributionNetworks;
  81. public function __construct(
  82. string $articleId = null,
  83. string $title,
  84. string $slug,
  85. string $content,
  86. \DateTime $publishAt = null,
  87. string $status = Article::ARTICLE_DRAFT,
  88. string $locale = Article::DEFAULT_LOCALE,
  89. string $headerImageUrl = null
  90. ) {
  91. try {
  92. $this->uuid = Uuid::fromString($articleId ?: Uuid::uuid4())->toString();
  93. } catch (\InvalidArgumentException $e) {
  94. throw new InvalidUUIDException();
  95. }
  96. $this->categories = new ArrayCollection();
  97. switch ($status) {
  98. case Article::ARTICLE_DRAFT:
  99. case Article::ARTICLE_PUBLIC:
  100. $this->status = $status;
  101. break;
  102. default:
  103. throw new InvalidArticleStatusException();
  104. }
  105. $this->defaultLocale = $locale;
  106. $articleTranslation = new ArticleTranslation(
  107. null,
  108. $this,
  109. $locale,
  110. $title,
  111. $slug,
  112. $content
  113. );
  114. $this->translations = new ArrayCollection();
  115. $this->translations->add($articleTranslation);
  116. $this->publishAt = $publishAt;
  117. if (is_null($this->publishAt)) {
  118. $this->publishAt = new \DateTime();
  119. }
  120. $this->headerImageUrl = $headerImageUrl;
  121. $this->showInBlog = true;
  122. $this->showInWhaterpoints = false;
  123. $this->linkedWhaterpoints = new ArrayCollection();
  124. $this->showInDistributionNetworks = false;
  125. $this->linkedDistributionNetworks = new ArrayCollection();
  126. $this->createdAt = new \DateTime();
  127. $this->updatedAt = $this->createdAt();
  128. }
  129. /**
  130. * @return string
  131. */
  132. public function id(): string
  133. {
  134. return $this->uuid;
  135. }
  136. /**
  137. * @return Collection
  138. */
  139. public function categories(): Collection
  140. {
  141. return $this->categories;
  142. }
  143. /**
  144. * @param string|null $locale
  145. * @return ArticleTranslation
  146. */
  147. public function getTranslation(?string $locale = null): ArticleTranslation
  148. {
  149. $articleTranslation = null;
  150. $locale = $locale ?: $this->defaultLocale();
  151. foreach ($this->translations as $translation) {
  152. if ($translation->locale() == $locale) {
  153. $articleTranslation = $translation;
  154. break;
  155. }
  156. }
  157. if (is_null($articleTranslation)) {
  158. $articleTranslation = new ArticleTranslation(
  159. null,
  160. $this,
  161. $locale,
  162. $this->getTranslation(Article::DEFAULT_LOCALE)->title(),
  163. $this->getTranslation(Article::DEFAULT_LOCALE)->slug(),
  164. $this->getTranslation(Article::DEFAULT_LOCALE)->content()
  165. );
  166. $this->translations->add($articleTranslation);
  167. }
  168. return $articleTranslation;
  169. }
  170. /**
  171. * @param string|null $locale
  172. * @return bool
  173. */
  174. public function hasTranslation(string $locale): bool
  175. {
  176. foreach ($this->translations as $translation) {
  177. if ($translation->locale() == $locale) {
  178. return true;
  179. }
  180. }
  181. return false;
  182. }
  183. public function updateArticle(
  184. string $status,
  185. string $headerImageUrl = null,
  186. \DateTime $publishAt = null
  187. ) {
  188. if ($this->status != $status) {
  189. switch ($status) {
  190. case Article::ARTICLE_DRAFT:
  191. case Article::ARTICLE_PUBLIC:
  192. $this->status = $status;
  193. $this->updatedAt = new \DateTime();
  194. break;
  195. default:
  196. throw new InvalidArticleStatusException();
  197. }
  198. }
  199. if (!is_null($publishAt)) {
  200. $this->publishAt = $publishAt;
  201. $this->updatedAt = new \DateTime();
  202. }
  203. if (!is_null($headerImageUrl)) {
  204. $this->headerImageUrl = $headerImageUrl;
  205. $this->updatedAt = new \DateTime();
  206. }
  207. return $this;
  208. }
  209. public function updateTranslation(
  210. string $locale,
  211. string $title,
  212. string $slug,
  213. string $content,
  214. \DateTime $publishAt = null,
  215. array $metaKeywords = [],
  216. string $metaDescription = null
  217. ) {
  218. $articleTranslation = $this->getTranslation($locale);
  219. if (is_null($articleTranslation)) {
  220. $articleTranslation = new ArticleTranslation(
  221. null,
  222. $this,
  223. $locale,
  224. $title,
  225. $slug,
  226. $content,
  227. $metaKeywords,
  228. $metaDescription
  229. );
  230. $this->translations->add($articleTranslation);
  231. } else {
  232. $articleTranslation->updateArticleTranslation(
  233. $title,
  234. $slug,
  235. $content,
  236. $metaKeywords,
  237. $metaDescription
  238. );
  239. }
  240. if (!is_null($publishAt)) {
  241. $this->publishAt = $publishAt;
  242. }
  243. $this->updatedAt = new \DateTime();
  244. return $this;
  245. }
  246. public function updateShowIn(
  247. bool $showInBlog,
  248. bool $showInWhaterpoints,
  249. bool $showInDistributionNetworks
  250. ) {
  251. $this->showInBlog = $showInBlog;
  252. $this->showInWhaterpoints = $showInWhaterpoints;
  253. $this->showInDistributionNetworks = $showInDistributionNetworks;
  254. }
  255. /**
  256. * @return string
  257. */
  258. public function status(): string
  259. {
  260. return $this->status;
  261. }
  262. /**
  263. * @return string
  264. */
  265. public function headerImageUrl(): ?string
  266. {
  267. return $this->headerImageUrl;
  268. }
  269. /**
  270. * @return string
  271. */
  272. public function defaultLocale(): string
  273. {
  274. return $this->defaultLocale;
  275. }
  276. /**
  277. * @return string
  278. */
  279. public function slug(string $locale = null): string
  280. {
  281. return $this->getTranslation($locale)->slug();
  282. }
  283. /**
  284. * @return string
  285. */
  286. public function title(string $locale = null): string
  287. {
  288. return $this->getTranslation($locale)->title();
  289. }
  290. /**
  291. * @return string
  292. */
  293. public function content(string $locale = null): string
  294. {
  295. return $this->getTranslation($locale)->content();
  296. }
  297. /**
  298. * @return array
  299. */
  300. public function metaKeywords(string $locale = null): array
  301. {
  302. return $this->getTranslation($locale)->metaKeywords();
  303. }
  304. /**
  305. * @return string
  306. */
  307. public function metaDescription(string $locale = null): ?string
  308. {
  309. return $this->getTranslation()->metaDescription();
  310. }
  311. /**
  312. * @return \DateTime
  313. */
  314. public function publishAt(): \DateTime
  315. {
  316. return $this->publishAt;
  317. }
  318. /**
  319. * @return bool
  320. */
  321. public function showInBlog(): bool
  322. {
  323. return $this->showInBlog;
  324. }
  325. /**
  326. * @return bool
  327. */
  328. public function showInWhaterpoints(): bool
  329. {
  330. return $this->showInWhaterpoints;
  331. }
  332. /**
  333. * @return bool
  334. */
  335. public function showInDistributionNetworks(): bool
  336. {
  337. return $this->showInDistributionNetworks;
  338. }
  339. /**
  340. * @return Collection
  341. */
  342. public function linkedWhaterpoints(): Collection
  343. {
  344. return $this->linkedWhaterpoints;
  345. }
  346. /**
  347. * @return Collection
  348. */
  349. public function linkedDistributionNetworks(): Collection
  350. {
  351. return $this->linkedDistributionNetworks;
  352. }
  353. /**
  354. * @return \DateTime
  355. */
  356. public function createdAt(): \DateTime
  357. {
  358. return $this->createdAt;
  359. }
  360. /**
  361. * @return \DateTime
  362. */
  363. public function updatedAt(): \DateTime
  364. {
  365. return $this->updatedAt;
  366. }
  367. }