]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Entity/Entry.php
Adding more tests to cover different scenario
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Entity / Entry.php
CommitLineData
9d50517c
NL
1<?php
2
ad4d1caa 3namespace Wallabag\CoreBundle\Entity;
9d50517c 4
0a018fe0 5use Doctrine\Common\Collections\ArrayCollection;
9d50517c 6use Doctrine\ORM\Mapping as ORM;
0f006880 7use Hateoas\Configuration\Annotation as Hateoas;
7d1fdab2 8use JMS\Serializer\Annotation\Exclude;
f808b016 9use JMS\Serializer\Annotation\Groups;
7d1fdab2 10use JMS\Serializer\Annotation\SerializedName;
f808b016
JB
11use JMS\Serializer\Annotation\VirtualProperty;
12use JMS\Serializer\Annotation\XmlRoot;
619cc453 13use Symfony\Component\Validator\Constraints as Assert;
4dc87223 14use Wallabag\AnnotationBundle\Entity\Annotation;
927c9e79 15use Wallabag\CoreBundle\Helper\EntityTimestampsTrait;
f808b016 16use Wallabag\UserBundle\Entity\User;
9d50517c
NL
17
18/**
4346a860 19 * Entry.
9d50517c 20 *
0f006880 21 * @XmlRoot("entry")
be463487 22 * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\EntryRepository")
7e9c1d65
JB
23 * @ORM\Table(
24 * name="`entry`",
25 * options={"collate"="utf8mb4_unicode_ci", "charset"="utf8mb4"},
81375151
NL
26 * indexes={
27 * @ORM\Index(name="created_at", columns={"created_at"}),
7239082a 28 * @ORM\Index(name="uid", columns={"uid"})
81375151 29 * }
7e9c1d65 30 * )
34d15eb4 31 * @ORM\HasLifecycleCallbacks()
0f006880 32 * @Hateoas\Relation("self", href = "expr('/api/entries/' ~ object.getId())")
9d50517c 33 */
be463487 34class Entry
9d50517c 35{
927c9e79
JB
36 use EntityTimestampsTrait;
37
0f006880 38 /** @Serializer\XmlAttribute */
9d50517c 39 /**
4346a860 40 * @var int
9d50517c 41 *
be463487 42 * @ORM\Column(name="id", type="integer")
9d50517c 43 * @ORM\Id
34d15eb4 44 * @ORM\GeneratedValue(strategy="AUTO")
5b7da076
TC
45 *
46 * @Groups({"entries_for_user", "export_all"})
9d50517c 47 */
be463487 48 private $id;
9d50517c 49
f3d0cb91 50 /**
78b3c31d 51 * @var string
f3d0cb91 52 *
7239082a 53 * @ORM\Column(name="uid", type="string", length=23, nullable=true)
f3d0cb91
NL
54 *
55 * @Groups({"entries_for_user", "export_all"})
56 */
7239082a 57 private $uid;
f3d0cb91 58
9d50517c
NL
59 /**
60 * @var string
61 *
62 * @ORM\Column(name="title", type="text", nullable=true)
5b7da076
TC
63 *
64 * @Groups({"entries_for_user", "export_all"})
9d50517c
NL
65 */
66 private $title;
67
68 /**
69 * @var string
70 *
b84a8055 71 * @Assert\NotBlank()
9d50517c 72 * @ORM\Column(name="url", type="text", nullable=true)
5b7da076
TC
73 *
74 * @Groups({"entries_for_user", "export_all"})
9d50517c
NL
75 */
76 private $url;
77
78 /**
4346a860 79 * @var bool
9d50517c 80 *
189ef634
TC
81 * @Exclude
82 *
be463487 83 * @ORM\Column(name="is_archived", type="boolean")
5b7da076
TC
84 *
85 * @Groups({"entries_for_user", "export_all"})
9d50517c 86 */
905ae369 87 private $isArchived = false;
9d50517c
NL
88
89 /**
4346a860 90 * @var bool
9d50517c 91 *
189ef634
TC
92 * @Exclude
93 *
be463487 94 * @ORM\Column(name="is_starred", type="boolean")
5b7da076
TC
95 *
96 * @Groups({"entries_for_user", "export_all"})
9d50517c 97 */
905ae369 98 private $isStarred = false;
9d50517c 99
9d50517c
NL
100 /**
101 * @var string
102 *
eacaf7f8 103 * @ORM\Column(name="content", type="text", nullable=true)
5b7da076
TC
104 *
105 * @Groups({"entries_for_user", "export_all"})
9d50517c 106 */
eacaf7f8 107 private $content;
9d50517c 108
34d15eb4 109 /**
8664069e 110 * @var \DateTime
34d15eb4 111 *
be463487 112 * @ORM\Column(name="created_at", type="datetime")
5b7da076 113 *
9401696f 114 * @Groups({"entries_for_user", "export_all"})
34d15eb4
NL
115 */
116 private $createdAt;
117
118 /**
8664069e 119 * @var \DateTime
34d15eb4 120 *
be463487 121 * @ORM\Column(name="updated_at", type="datetime")
5b7da076 122 *
9401696f 123 * @Groups({"entries_for_user", "export_all"})
34d15eb4
NL
124 */
125 private $updatedAt;
126
5e9009ce
NL
127 /**
128 * @var \DateTime
129 *
130 * @ORM\Column(name="published_at", type="datetime", nullable=true)
131 *
132 * @Groups({"entries_for_user", "export_all"})
133 */
134 private $publishedAt;
135
7b0b3622
NL
136 /**
137 * @var array
138 *
1517d577 139 * @ORM\Column(name="published_by", type="array", nullable=true)
7b0b3622
NL
140 *
141 * @Groups({"entries_for_user", "export_all"})
142 */
143 private $publishedBy;
144
a991c46e
F
145 /**
146 * @var \DateTime
147 *
148 * @ORM\Column(name="starred_at", type="datetime", nullable=true)
149 *
150 * @Groups({"entries_for_user", "export_all"})
151 */
152 private $starredAt = null;
153
34d15eb4 154 /**
4dc87223 155 * @ORM\OneToMany(targetEntity="Wallabag\AnnotationBundle\Entity\Annotation", mappedBy="entry", cascade={"persist", "remove"})
f38e03dc 156 * @ORM\JoinTable
5b7da076 157 *
f38e03dc 158 * @Groups({"entries_for_user", "export_all"})
34d15eb4 159 */
4dc87223 160 private $annotations;
34d15eb4
NL
161
162 /**
163 * @var string
164 *
165 * @ORM\Column(name="mimetype", type="text", nullable=true)
5b7da076
TC
166 *
167 * @Groups({"entries_for_user", "export_all"})
34d15eb4
NL
168 */
169 private $mimetype;
170
98f0929f
JB
171 /**
172 * @var string
173 *
174 * @ORM\Column(name="language", type="text", nullable=true)
5b7da076
TC
175 *
176 * @Groups({"entries_for_user", "export_all"})
98f0929f
JB
177 */
178 private $language;
179
34d15eb4 180 /**
4346a860 181 * @var int
34d15eb4 182 *
88bac4a3 183 * @ORM\Column(name="reading_time", type="integer", nullable=false)
5b7da076
TC
184 *
185 * @Groups({"entries_for_user", "export_all"})
34d15eb4 186 */
88bac4a3 187 private $readingTime = 0;
34d15eb4
NL
188
189 /**
190 * @var string
191 *
192 * @ORM\Column(name="domain_name", type="text", nullable=true)
5b7da076
TC
193 *
194 * @Groups({"entries_for_user", "export_all"})
34d15eb4
NL
195 */
196 private $domainName;
197
fad31615
JB
198 /**
199 * @var string
200 *
201 * @ORM\Column(name="preview_picture", type="text", nullable=true)
5b7da076
TC
202 *
203 * @Groups({"entries_for_user", "export_all"})
fad31615
JB
204 */
205 private $previewPicture;
206
10b35097 207 /**
e10e6ab3 208 * @var string
10b35097 209 *
3ef75cc4 210 * @ORM\Column(name="http_status", type="string", length=3, nullable=true)
10b35097
NL
211 *
212 * @Groups({"entries_for_user", "export_all"})
213 */
214 private $httpStatus;
215
dda6a6ad
NL
216 /**
217 * @var array
218 *
1517d577 219 * @ORM\Column(name="headers", type="array", nullable=true)
dda6a6ad
NL
220 *
221 * @Groups({"entries_for_user", "export_all"})
222 */
223 private $headers;
224
5f09650e 225 /**
7d1fdab2
TC
226 * @Exclude
227 *
1210dae1 228 * @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User", inversedBy="entries")
5b7da076
TC
229 *
230 * @Groups({"export_all"})
5f09650e
NL
231 */
232 private $user;
233
0a018fe0 234 /**
af95c09c 235 * @ORM\ManyToMany(targetEntity="Tag", inversedBy="entries", cascade={"persist"})
e42b13bc
JB
236 * @ORM\JoinTable(
237 * name="entry_tag",
238 * joinColumns={
206bade5 239 * @ORM\JoinColumn(name="entry_id", referencedColumnName="id", onDelete="cascade")
e42b13bc
JB
240 * },
241 * inverseJoinColumns={
206bade5 242 * @ORM\JoinColumn(name="tag_id", referencedColumnName="id", onDelete="cascade")
e42b13bc
JB
243 * }
244 * )
0a018fe0
NL
245 */
246 private $tags;
247
e0ef1a1c
KD
248 /**
249 * @var string
250 *
251 * @ORM\Column(name="origin_url", type="text", nullable=true)
252 *
253 * @Groups({"entries_for_user", "export_all"})
254 */
255 private $originUrl;
256
5f09650e
NL
257 /*
258 * @param User $user
259 */
5d6f6f56 260 public function __construct(User $user)
5f09650e
NL
261 {
262 $this->user = $user;
0a018fe0 263 $this->tags = new ArrayCollection();
5f09650e
NL
264 }
265
9d50517c 266 /**
4346a860 267 * Get id.
9d50517c 268 *
4346a860 269 * @return int
9d50517c
NL
270 */
271 public function getId()
272 {
273 return $this->id;
274 }
275
276 /**
4346a860
JB
277 * Set title.
278 *
279 * @param string $title
9d50517c 280 *
be463487 281 * @return Entry
9d50517c
NL
282 */
283 public function setTitle($title)
284 {
285 $this->title = $title;
286
287 return $this;
288 }
289
290 /**
4346a860 291 * Get title.
9d50517c 292 *
7df80cb3 293 * @return string
9d50517c
NL
294 */
295 public function getTitle()
296 {
297 return $this->title;
298 }
299
300 /**
4346a860
JB
301 * Set url.
302 *
303 * @param string $url
9d50517c 304 *
be463487 305 * @return Entry
9d50517c
NL
306 */
307 public function setUrl($url)
308 {
309 $this->url = $url;
310
311 return $this;
312 }
313
314 /**
4346a860 315 * Get url.
9d50517c 316 *
7df80cb3 317 * @return string
9d50517c
NL
318 */
319 public function getUrl()
320 {
321 return $this->url;
322 }
323
324 /**
4346a860
JB
325 * Set isArchived.
326 *
8eedc8cf 327 * @param bool $isArchived
9d50517c 328 *
be463487 329 * @return Entry
9d50517c 330 */
905ae369 331 public function setArchived($isArchived)
9d50517c 332 {
905ae369 333 $this->isArchived = $isArchived;
9d50517c
NL
334
335 return $this;
336 }
337
338 /**
4346a860 339 * Get isArchived.
9d50517c 340 *
8eedc8cf 341 * @return bool
9d50517c 342 */
905ae369 343 public function isArchived()
9d50517c 344 {
905ae369 345 return $this->isArchived;
9d50517c
NL
346 }
347
189ef634
TC
348 /**
349 * @VirtualProperty
350 * @SerializedName("is_archived")
351 * @Groups({"entries_for_user", "export_all"})
352 */
353 public function is_Archived()
354 {
355 return (int) $this->isArchived();
356 }
357
163eae0b
NL
358 public function toggleArchive()
359 {
905ae369 360 $this->isArchived = $this->isArchived() ^ 1;
7df80cb3 361
163eae0b
NL
362 return $this;
363 }
364
9d50517c 365 /**
4346a860
JB
366 * Set isStarred.
367 *
8eedc8cf 368 * @param bool $isStarred
9d50517c 369 *
be463487 370 * @return Entry
9d50517c 371 */
905ae369 372 public function setStarred($isStarred)
9d50517c 373 {
905ae369 374 $this->isStarred = $isStarred;
9d50517c
NL
375
376 return $this;
377 }
378
379 /**
4346a860 380 * Get isStarred.
9d50517c 381 *
8eedc8cf 382 * @return bool
9d50517c 383 */
905ae369 384 public function isStarred()
9d50517c 385 {
905ae369 386 return $this->isStarred;
9d50517c
NL
387 }
388
189ef634
TC
389 /**
390 * @VirtualProperty
391 * @SerializedName("is_starred")
392 * @Groups({"entries_for_user", "export_all"})
393 */
394 public function is_Starred()
395 {
396 return (int) $this->isStarred();
397 }
398
163eae0b
NL
399 public function toggleStar()
400 {
905ae369 401 $this->isStarred = $this->isStarred() ^ 1;
163eae0b
NL
402
403 return $this;
404 }
405
9d50517c 406 /**
4346a860
JB
407 * Set content.
408 *
409 * @param string $content
9d50517c 410 *
be463487 411 * @return Entry
9d50517c
NL
412 */
413 public function setContent($content)
414 {
415 $this->content = $content;
416
417 return $this;
418 }
419
420 /**
4346a860 421 * Get content.
9d50517c 422 *
7df80cb3 423 * @return string
9d50517c
NL
424 */
425 public function getContent()
426 {
427 return $this->content;
428 }
429
430 /**
5f09650e 431 * @return User
9d50517c 432 */
5f09650e 433 public function getUser()
9d50517c 434 {
5f09650e 435 return $this->user;
9d50517c 436 }
42a90646 437
7d1fdab2
TC
438 /**
439 * @VirtualProperty
440 * @SerializedName("user_name")
441 */
442 public function getUserName()
443 {
444 return $this->user->getUserName();
445 }
446
447 /**
448 * @VirtualProperty
449 * @SerializedName("user_email")
450 */
451 public function getUserEmail()
452 {
453 return $this->user->getEmail();
454 }
455
456 /**
457 * @VirtualProperty
458 * @SerializedName("user_id")
459 */
460 public function getUserId()
461 {
462 return $this->user->getId();
463 }
464
34d15eb4 465 /**
6d65c0a8
JB
466 * Set created_at.
467 * Only used when importing data from an other service.
468 *
8664069e 469 * @param \DateTime $createdAt
6d65c0a8
JB
470 *
471 * @return Entry
472 */
473 public function setCreatedAt(\DateTime $createdAt)
474 {
475 $this->createdAt = $createdAt;
476
477 return $this;
478 }
479
480 /**
8664069e 481 * @return \DateTime
34d15eb4
NL
482 */
483 public function getCreatedAt()
484 {
485 return $this->createdAt;
486 }
487
34d15eb4 488 /**
8664069e 489 * @return \DateTime
34d15eb4
NL
490 */
491 public function getUpdatedAt()
492 {
493 return $this->updatedAt;
494 }
495
a991c46e
F
496 /**
497 * @return \DateTime|null
498 */
499 public function getStarredAt()
500 {
501 return $this->starredAt;
502 }
503
504 /**
505 * @param \DateTime|null $starredAt
506 *
507 * @return Entry
508 */
509 public function setStarredAt($starredAt = null)
510 {
511 $this->starredAt = $starredAt;
512
513 return $this;
514 }
515
516 /**
517 * update isStarred and starred_at fields.
518 *
519 * @param bool $isStarred
520 *
521 * @return Entry
522 */
523 public function updateStar($isStarred = false)
524 {
525 $this->setStarred($isStarred);
526 $this->setStarredAt(null);
527 if ($this->isStarred()) {
528 $this->setStarredAt(new \DateTime());
529 }
530
531 return $this;
532 }
533
34d15eb4 534 /**
4dc87223 535 * @return ArrayCollection<Annotation>
34d15eb4 536 */
4dc87223 537 public function getAnnotations()
34d15eb4 538 {
4dc87223 539 return $this->annotations;
34d15eb4
NL
540 }
541
542 /**
4dc87223 543 * @param Annotation $annotation
34d15eb4 544 */
4dc87223 545 public function setAnnotation(Annotation $annotation)
34d15eb4 546 {
4dc87223 547 $this->annotations[] = $annotation;
34d15eb4
NL
548 }
549
550 /**
551 * @return string
552 */
553 public function getMimetype()
554 {
555 return $this->mimetype;
556 }
557
558 /**
559 * @param string $mimetype
560 */
561 public function setMimetype($mimetype)
562 {
563 $this->mimetype = $mimetype;
564 }
565
566 /**
567 * @return int
568 */
569 public function getReadingTime()
570 {
571 return $this->readingTime;
572 }
573
574 /**
575 * @param int $readingTime
576 */
577 public function setReadingTime($readingTime)
578 {
579 $this->readingTime = $readingTime;
580 }
581
582 /**
583 * @return string
584 */
585 public function getDomainName()
586 {
587 return $this->domainName;
588 }
589
590 /**
591 * @param string $domainName
592 */
593 public function setDomainName($domainName)
594 {
595 $this->domainName = $domainName;
596 }
597
0a018fe0 598 /**
4ec53ab7 599 * @return ArrayCollection
0a018fe0
NL
600 */
601 public function getTags()
602 {
603 return $this->tags;
604 }
605
b0458874
JB
606 /**
607 * @VirtualProperty
608 * @SerializedName("tags")
609 * @Groups({"entries_for_user", "export_all"})
610 */
611 public function getSerializedTags()
612 {
613 $data = [];
614 foreach ($this->tags as $tag) {
615 $data[] = $tag->getLabel();
616 }
617
618 return $data;
619 }
620
0a018fe0
NL
621 /**
622 * @param Tag $tag
623 */
624 public function addTag(Tag $tag)
625 {
625acf33
KG
626 if ($this->tags->contains($tag)) {
627 return;
628 }
629
fc031e57
JB
630 // check if tag already exist but has not yet be persisted
631 // it seems that the previous condition with `contains()` doesn't check that case
632 foreach ($this->tags as $existingTag) {
fc732227 633 if ($existingTag->getLabel() === $tag->getLabel()) {
fc031e57
JB
634 return;
635 }
636 }
637
e42b13bc 638 $this->tags->add($tag);
46bbd8d3 639 $tag->addEntry($this);
0a018fe0 640 }
092ca707 641
a05b6115
JB
642 /**
643 * Remove the given tag from the entry (if the tag is associated).
644 *
645 * @param Tag $tag
646 */
092ca707
NL
647 public function removeTag(Tag $tag)
648 {
e42b13bc
JB
649 if (!$this->tags->contains($tag)) {
650 return;
651 }
652
092ca707 653 $this->tags->removeElement($tag);
e42b13bc 654 $tag->removeEntry($this);
092ca707 655 }
fad31615 656
a05b6115
JB
657 /**
658 * Remove all assigned tags from the entry.
659 */
660 public function removeAllTags()
661 {
662 foreach ($this->tags as $tag) {
663 $this->tags->removeElement($tag);
664 $tag->removeEntry($this);
665 }
666 }
667
fad31615 668 /**
a1413a3d 669 * Set previewPicture.
fad31615
JB
670 *
671 * @param string $previewPicture
672 *
673 * @return Entry
674 */
675 public function setPreviewPicture($previewPicture)
676 {
677 $this->previewPicture = $previewPicture;
678
679 return $this;
680 }
681
682 /**
a1413a3d 683 * Get previewPicture.
fad31615
JB
684 *
685 * @return string
686 */
687 public function getPreviewPicture()
688 {
689 return $this->previewPicture;
690 }
98f0929f
JB
691
692 /**
693 * Set language.
694 *
695 * @param string $language
696 *
697 * @return Entry
698 */
699 public function setLanguage($language)
700 {
701 $this->language = $language;
702
703 return $this;
704 }
705
706 /**
707 * Get language.
708 *
709 * @return string
710 */
711 public function getLanguage()
712 {
713 return $this->language;
714 }
f3d0cb91
NL
715
716 /**
78b3c31d 717 * @return string
f3d0cb91 718 */
7239082a 719 public function getUid()
f3d0cb91 720 {
7239082a 721 return $this->uid;
f3d0cb91
NL
722 }
723
724 /**
7239082a 725 * @param string $uid
f3d0cb91
NL
726 *
727 * @return Entry
728 */
7239082a 729 public function setUid($uid)
f3d0cb91 730 {
7239082a 731 $this->uid = $uid;
f3d0cb91
NL
732
733 return $this;
734 }
735
7239082a 736 public function generateUid()
f3d0cb91 737 {
7239082a 738 if (null === $this->uid) {
a7e2218e 739 // @see http://blog.kevingomez.fr/til/2015/07/26/why-is-uniqid-slow/ for true parameter
7239082a 740 $this->uid = uniqid('', true);
f3d0cb91
NL
741 }
742 }
f1be7af4 743
7239082a 744 public function cleanUid()
f1be7af4 745 {
7239082a 746 $this->uid = null;
f1be7af4 747 }
10b35097 748
e8911f7c
JB
749 /**
750 * Used in the entries filter so it's more explicit for the end user than the uid.
a9c6577f 751 * Also used in the API.
e8911f7c 752 *
1112e547
JB
753 * @VirtualProperty
754 * @SerializedName("is_public")
755 * @Groups({"entries_for_user"})
756 *
e8911f7c
JB
757 * @return bool
758 */
759 public function isPublic()
760 {
761 return null !== $this->uid;
762 }
763
10b35097 764 /**
5fe65bae 765 * @return string
10b35097
NL
766 */
767 public function getHttpStatus()
768 {
769 return $this->httpStatus;
770 }
771
772 /**
5fe65bae 773 * @param string $httpStatus
10b35097
NL
774 *
775 * @return Entry
776 */
777 public function setHttpStatus($httpStatus)
778 {
779 $this->httpStatus = $httpStatus;
780
781 return $this;
782 }
5e9009ce
NL
783
784 /**
785 * @return \Datetime
786 */
787 public function getPublishedAt()
788 {
789 return $this->publishedAt;
790 }
791
792 /**
793 * @param \Datetime $publishedAt
794 *
795 * @return Entry
796 */
797 public function setPublishedAt(\Datetime $publishedAt)
798 {
799 $this->publishedAt = $publishedAt;
800
801 return $this;
802 }
7b0b3622
NL
803
804 /**
dda6a6ad 805 * @return array
7b0b3622
NL
806 */
807 public function getPublishedBy()
808 {
809 return $this->publishedBy;
810 }
811
812 /**
1517d577 813 * @param array $publishedBy
7b0b3622
NL
814 *
815 * @return Entry
816 */
817 public function setPublishedBy($publishedBy)
818 {
819 $this->publishedBy = $publishedBy;
820
821 return $this;
822 }
dda6a6ad
NL
823
824 /**
825 * @return array
826 */
827 public function getHeaders()
828 {
829 return $this->headers;
830 }
831
832 /**
1517d577 833 * @param array $headers
dda6a6ad
NL
834 *
835 * @return Entry
836 */
837 public function setHeaders($headers)
838 {
839 $this->headers = $headers;
840
841 return $this;
842 }
eae8138b 843
e0ef1a1c
KD
844 /**
845 * Set origin url.
846 *
847 * @param string $originUrl
848 *
849 * @return Entry
850 */
851 public function setOriginUrl($originUrl)
852 {
853 $this->originUrl = $originUrl;
854
855 return $this;
856 }
eae8138b 857
e0ef1a1c
KD
858 /**
859 * Get origin url.
860 *
861 * @return string
862 */
863 public function getOriginUrl()
864 {
865 return $this->originUrl;
866 }
9d50517c 867}