<?php
namespace Products\NotificationsBundle\Entity\Lists;
use Cms\CoreBundle\Entity\OneRoster\OneRosterUser;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Mapping as ORM;
use Products\NotificationsBundle\Entity\AbstractList;
use Products\NotificationsBundle\Entity\Lists\Components\ListSubscription;
/**
* Class StaticList
* @package Products\NotificationsBundle\Entity\Lists
*
* @ORM\Entity(
* repositoryClass = "Products\NotificationsBundle\Doctrine\Repository\Lists\StaticListRepository",
* )
*/
class StaticList extends AbstractList
{
const DISCR = 'static';
/**
* @var Collection
*
* @ORM\OneToMany(
* targetEntity = "Products\NotificationsBundle\Entity\Lists\Components\ListSubscription",
* mappedBy = "list",
* )
*/
protected Collection $subscriptions;
/**
*
*/
public function __construct()
{
$this->subscriptions = new ArrayCollection();
}
/**
* @param Criteria|null $criteria
* @return Collection|ListSubscription[]
*/
public function getSubscriptions(?Criteria $criteria = null): Collection
{
if ( ! empty($criteria)) {
return $this->subscriptions->matching($criteria);
}
return $this->subscriptions;
}
/**
* @param Criteria|null $criteria
* @return array|ListSubscription[]
*/
public function getSubscriptionsAsArray(?Criteria $criteria = null): array
{
return $this->getSubscriptions($criteria)->toArray();
}
/**
* @param ListSubscription $subscription
* @return $this
*/
public function addSubscription(ListSubscription $subscription): self
{
$this->subscriptions->add($subscription);
//$subscription->setList($this);
return $this;
}
/**
* @return array|string[]
*/
public function getRoles(): array
{
$roles = [];
foreach (OneRosterUser::TYPES as $type) {
$roles = array_merge(
$roles,
OneRosterUser::TYPES_MAPPING[$type]
);
}
return array_values(array_unique($roles));
}
/**
* @param StaticList $list
* @return $this
*/
public function merge(StaticList $list): self
{
$this->setTypes($list->getTypes());
$this->subscriptions->clear();
foreach ($list->getSubscriptionsAsArray() as $subscription) {
$this->subscriptions->add(
(new ListSubscription())
->setList($this)
->setProfile($subscription->getProfile())
);
}
return $this;
}
}