angelov /
donut
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * Donut Social Network - Yet another experimental social network. |
||
| 5 | * Copyright (C) 2016-2017, Dejan Angelov <[email protected]> |
||
| 6 | * |
||
| 7 | * This file is part of Donut Social Network. |
||
| 8 | * |
||
| 9 | * Donut Social Network is free software: you can redistribute it and/or modify |
||
| 10 | * it under the terms of the GNU General Public License as published by |
||
| 11 | * the Free Software Foundation, either version 3 of the License, or |
||
| 12 | * (at your option) any later version. |
||
| 13 | * |
||
| 14 | * Donut Social Network is distributed in the hope that it will be useful, |
||
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 17 | * GNU General Public License for more details. |
||
| 18 | * |
||
| 19 | * You should have received a copy of the GNU General Public License |
||
| 20 | * along with Donut Social Network. If not, see <http://www.gnu.org/licenses/>. |
||
| 21 | * |
||
| 22 | * @package Donut Social Network |
||
| 23 | * @copyright Copyright (C) 2016-2017, Dejan Angelov <[email protected]> |
||
| 24 | * @license https://github.com/angelov/donut/blob/master/LICENSE |
||
| 25 | * @author Dejan Angelov <[email protected]> |
||
| 26 | */ |
||
| 27 | |||
| 28 | namespace Angelov\Donut\Communities; |
||
| 29 | |||
| 30 | use DateTime; |
||
| 31 | use Angelov\Donut\Users\User; |
||
| 32 | use Doctrine\Common\Collections\ArrayCollection; |
||
| 33 | use Doctrine\ORM\Mapping as ORM; |
||
| 34 | use Angelov\Donut\Communities\Exceptions\CommunityMemberNotFoundException; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @ORM\Entity |
||
| 38 | * @ORM\Table(name="community") |
||
| 39 | */ |
||
| 40 | class Community |
||
| 41 | { |
||
| 42 | /** |
||
| 43 | * @ORM\Id |
||
| 44 | * @ORM\GeneratedValue(strategy="NONE") |
||
| 45 | * @ORM\Column(type="guid") |
||
| 46 | */ |
||
| 47 | private $id; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @ORM\Column(type="string") |
||
| 51 | */ |
||
| 52 | private $name; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @ORM\Column(type="string", nullable=true) |
||
| 56 | */ |
||
| 57 | private $description; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @ORM\ManyToOne(targetEntity="Angelov\Donut\Users\User", cascade={"remove", "persist"}, fetch="EAGER") |
||
| 61 | * @ORM\JoinColumn(name="author_id", referencedColumnName="id", nullable=false) |
||
| 62 | */ |
||
| 63 | private $author; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @ORM\ManyToMany(targetEntity="Angelov\Donut\Users\User") |
||
| 67 | * @ORM\JoinTable(name="community_member") |
||
| 68 | */ |
||
| 69 | private $members; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @ORM\Column(type="datetime") |
||
| 73 | */ |
||
| 74 | private $createdAt; |
||
| 75 | |||
| 76 | public function __construct(string $id, string $name, User $author, string $description = '') |
||
| 77 | { |
||
| 78 | $this->id = $id; |
||
| 79 | $this->name = $name; |
||
| 80 | $this->author = $author; |
||
| 81 | $this->description = $description; |
||
| 82 | $this->setCreatedAt(new DateTime()); |
||
| 83 | $this->members = new ArrayCollection(); |
||
| 84 | } |
||
| 85 | |||
| 86 | public function getId() : string |
||
| 87 | { |
||
| 88 | return $this->id; |
||
| 89 | } |
||
| 90 | |||
| 91 | public function setId(string $id) : void |
||
| 92 | { |
||
| 93 | $this->id = $id; |
||
| 94 | } |
||
| 95 | |||
| 96 | public function getName() : string |
||
| 97 | { |
||
| 98 | return $this->name; |
||
| 99 | } |
||
| 100 | |||
| 101 | public function setName(string $name) : void |
||
| 102 | { |
||
| 103 | $this->name = $name; |
||
| 104 | } |
||
| 105 | |||
| 106 | public function getDescription() : string |
||
| 107 | { |
||
| 108 | return $this->description; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 109 | } |
||
| 110 | |||
| 111 | public function setDescription(string $description = '') : void |
||
| 112 | { |
||
| 113 | $this->description = $description; |
||
| 114 | } |
||
| 115 | |||
| 116 | public function getAuthor() : User |
||
| 117 | { |
||
| 118 | return $this->author; |
||
| 119 | } |
||
| 120 | |||
| 121 | public function setAuthor(User $author) : void |
||
| 122 | { |
||
| 123 | $this->author = $author; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @return User[] |
||
| 128 | */ |
||
| 129 | public function getMembers() : array |
||
| 130 | { |
||
| 131 | return $this->members->getValues(); |
||
| 132 | } |
||
| 133 | |||
| 134 | public function addMember(User $user) : void |
||
| 135 | { |
||
| 136 | if ($this->hasMember($user)) { |
||
| 137 | return; |
||
| 138 | } |
||
| 139 | |||
| 140 | $this->members[] = $user; |
||
| 141 | } |
||
| 142 | |||
| 143 | public function hasMember(User $user) : bool |
||
| 144 | { |
||
| 145 | return $this->members->contains($user); |
||
| 146 | } |
||
| 147 | |||
| 148 | public function getCreatedAt() : DateTime |
||
| 149 | { |
||
| 150 | return $this->createdAt; |
||
| 151 | } |
||
| 152 | |||
| 153 | public function setCreatedAt(DateTime $createdAt) : void |
||
| 154 | { |
||
| 155 | $this->createdAt = $createdAt; |
||
| 156 | } |
||
| 157 | |||
| 158 | public function removeMember(User $user) : void |
||
| 159 | { |
||
| 160 | if (!$this->hasMember($user)) { |
||
| 161 | throw new CommunityMemberNotFoundException($user, $this); |
||
| 162 | } |
||
| 163 | |||
| 164 | $this->members->removeElement($user); |
||
| 165 | } |
||
| 166 | } |
||
| 167 |