/** * Creates new channel * * @param UserInterface $creator * @param array $assignees * @param string $type * @param string $name * @return type */ public function create(UserInterface $creator, $assignees = [], $type = Channel::TYPE_PUBLIC, $name = null) { $channel = new Channel(); $channel->setName($name); $channel->setType($type); array_unshift($assignees, $creator); $names = []; foreach ($assignees as $assignee) { $a = new Assignee(); $a->setAssigneeClass(get_class($assignee)); $a->setAssigneeId($assignee->getId()); $channel->addAssignee($a); // Private chanel is only for two persons $names[] = $assignee->getUsername(); if ($type === Channel::TYPE_PRIVATE and $assignee === $assignees[1]) { break; } } $count = 0; if ($name === null) { $created = true; if (count($names) > 3) { $temp = []; $temp[] = array_shift($names); $temp[] = array_shift($names); $temp[] = array_shift($names); $count = count($names); $names = $temp; } sort($names); $name = implode(', ', $names); } else { // Validate and sanitize input: $name = trim($name); if (strlen($name) === 0) { $name = $this->getUser()->getUsername(); } } $name = substr($name, 0, Channel::NAME_MAX_LENGTH); $name = trim($name); if ($count > 0) { $name .= ' + ' . $count; } $result = $this->getRepo()->createQueryBuilder('o')->andWhere('o.name = :name')->setParameters(['name' => $name])->orderBy('o.createdAt', 'DESC')->getQuery()->getResult(); if (count($result) > 0) { $channel = $result[0]; if ($channel->isDeleted()) { $channel->restore(); $channel->setDeletedBy(null); } foreach ($channel->getAssignees(true) as $a) { $a->restore(); $a->setDeletedBy(null); } } else { $channel->setName($name); $this->em->persist($channel); } $this->em->flush(); return $this->getDetails($channel->getId()); }
/** * Add assignees * * @param \Siciarek\ChatBundle\Entity\ChatChannelAssignee $assignees * @return ChatChannel */ public function addAssignee(\Siciarek\ChatBundle\Entity\ChatChannelAssignee $assignees) { $assignees->setChannel($this); $this->assignees[] = $assignees; return $this; }