src/Products/NotificationsBundle/Entity/AbstractList.php line 55

Open in your IDE?
  1. <?php
  2. namespace Products\NotificationsBundle\Entity;
  3. use App\Entity\Shared\AliasableInterface;
  4. use App\Entity\Shared\AliasableTrait;
  5. use App\Entity\Shared\FlagsInterface;
  6. use App\Entity\Shared\FlagsTrait;
  7. use App\Entity\System\School;
  8. use Cms\CoreBundle\Entity\OneRoster\OneRosterUser;
  9. use Cms\CoreBundle\Model\Interfaces\Loggable\LoggableInterface;
  10. use Cms\CoreBundle\Model\Interfaces\OneRosterable\OneRosterableInterface;
  11. use Cms\CoreBundle\Model\Interfaces\OneRosterable\OneRosterableTrait;
  12. use Cms\TenantBundle\Entity\TenantedEntity;
  13. use Doctrine\ORM\Mapping as ORM;
  14. use Reinder83\BinaryFlags\Bits;
  15. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  16. use Symfony\Component\Serializer\Annotation\Groups;
  17. /**
  18.  * Class AbstractList
  19.  * @package Products\NotificationsBundle\Entity
  20.  *
  21.  * @ORM\Entity(
  22.  *     repositoryClass = "Products\NotificationsBundle\Doctrine\Repository\ListRepository",
  23.  * )
  24.  * @ORM\InheritanceType("SINGLE_TABLE")
  25.  * @ORM\DiscriminatorColumn(
  26.  *     name = "discr",
  27.  *     type = "string",
  28.  * )
  29.  * @ORM\DiscriminatorMap({
  30.  *     Lists\DistrictList::DISCR = "Products\NotificationsBundle\Entity\Lists\DistrictList",
  31.  *     Lists\StaticList::DISCR = "Products\NotificationsBundle\Entity\Lists\StaticList",
  32.  *     Lists\SchoolList::DISCR = "Products\NotificationsBundle\Entity\Lists\SchoolList",
  33.  *     Lists\ConditionList::DISCR = "Products\NotificationsBundle\Entity\Lists\ConditionList",
  34.  * })
  35.  * @ORM\Table(
  36.  *     name = "notis__list",
  37.  *     uniqueConstraints = {
  38.  *         @ORM\UniqueConstraint(
  39.  *             name = "uidx__tenant__name",
  40.  *             columns = {
  41.  *                 "tenant",
  42.  *                 "name",
  43.  *             },
  44.  *         ),
  45.  *     },
  46.  * )
  47.  * @UniqueEntity(
  48.  *     fields = {"tenant", "name"},
  49.  *     message = "List name is already used",
  50.  * )
  51.  */
  52. abstract class AbstractList extends TenantedEntity
  53.     implements
  54.         OneRosterableInterface,
  55.         FlagsInterface,
  56.         AliasableInterface,
  57.         LoggableInterface
  58. {
  59.     use OneRosterableTrait;
  60.     use FlagsTrait;
  61.     use AliasableTrait;
  62.     const DISCRS = [
  63.         Lists\DistrictList::DISCR => Lists\DistrictList::class,
  64.         Lists\SchoolList::DISCR => Lists\SchoolList::class,
  65.         Lists\StaticList::DISCR => Lists\StaticList::class,
  66.         Lists\ConditionList::DISCR => Lists\ConditionList::class,
  67.     ];
  68.     const DISCR null;
  69.     const FLAGS = [
  70.         'fixed' => self::FLAGS__FIXED,
  71.     ];
  72.     const FLAGS__FIXED Bits::BIT_1;
  73.     /**
  74.      * @var string|null
  75.      *
  76.      * @ORM\Column(
  77.      *     type = "string",
  78.      *     nullable = false,
  79.      * )
  80.      *
  81.      * @Groups("list")
  82.      */
  83.     protected ?string $name null;
  84.     /**
  85.      * @var int
  86.      *
  87.      * @ORM\Column(
  88.      *     type = "integer",
  89.      *     nullable = false,
  90.      *     options = {
  91.      *         "unsigned" = true,
  92.      *         "default" = 0,
  93.      *     },
  94.      * )
  95.      *
  96.      * @Groups("list")
  97.      */
  98.     protected int $types 0;
  99.     /**
  100.      * @var int|null
  101.      */
  102.     protected ?int $_count null;
  103.     /**
  104.      * @var School|null
  105.      *
  106.      * @ORM\ManyToOne(
  107.      *     targetEntity = School::class,
  108.      * )
  109.      * @ORM\JoinColumn(
  110.      *     name = "school",
  111.      *     referencedColumnName = "id",
  112.      *     nullable = true,
  113.      *     onDelete = "SET NULL",
  114.      * )
  115.      */
  116.     protected ?School $school null;
  117.     /**
  118.      * @return string
  119.      */
  120.     public function discr(): string
  121.     {
  122.         return static::DISCR;
  123.     }
  124.     /**
  125.      * @param string|null $variant
  126.      * @return AbstractList
  127.      */
  128.     public static function factory(?string $variant null): AbstractList
  129.     {
  130.         return new static();
  131.     }
  132.     /**
  133.      * @return string|null
  134.      */
  135.     public function getName(): ?string
  136.     {
  137.         return $this->name;
  138.     }
  139.     /**
  140.      * @param string|null $name
  141.      * @return $this
  142.      */
  143.     public function setName(?string $name): self
  144.     {
  145.         $this->name $name;
  146.         return $this;
  147.     }
  148.     /**
  149.      * @return int
  150.      */
  151.     public function getTypes(): int
  152.     {
  153.         return $this->types;
  154.     }
  155.     /**
  156.      * @return array|int[]
  157.      */
  158.     public function getTypesAsArray(): array
  159.     {
  160.         if ( ! $this->types) {
  161.             return [];
  162.         }
  163.         $result = [];
  164.         foreach (OneRosterUser::TYPES as $mask) {
  165.             if ($this->hasType($mask)) {
  166.                 $result[] = $mask;
  167.             }
  168.         }
  169.         return $result;
  170.     }
  171.     /**
  172.      * @return array|string[]
  173.      */
  174.     public function getTypeNames(): array
  175.     {
  176.         if ( ! $this->types) {
  177.             return [];
  178.         }
  179.         $result = [];
  180.         foreach (OneRosterUser::TYPES as $name => $mask) {
  181.             if ($this->hasType($mask)) {
  182.                 $result[] = $name;
  183.             }
  184.         }
  185.         return $result;
  186.     }
  187.     /**
  188.      * @param int $types
  189.      * @return $this
  190.      */
  191.     public function setTypes(int $types): self
  192.     {
  193.         $this->types $types;
  194.         return $this;
  195.     }
  196.     /**
  197.      * @param int $type
  198.      * @return bool
  199.      */
  200.     public function hasType(int $type): bool
  201.     {
  202.         return (($this->types $type) > 0);
  203.     }
  204.     /**
  205.      * @param int $type
  206.      * @return $this
  207.      */
  208.     public function markType(int $type): self
  209.     {
  210.         $this->types |= $type;
  211.         return $this;
  212.     }
  213.     /**
  214.      * @param int $type
  215.      * @return $this
  216.      */
  217.     public function unmarkType(int $type): self
  218.     {
  219.         $this->types &= ~$type;
  220.         return $this;
  221.     }
  222.     /**
  223.      * @param int $type
  224.      * @param bool|null $toggle
  225.      * @return $this
  226.      */
  227.     public function toggleType(int $type, ?bool $toggle null): self
  228.     {
  229.         if ($toggle === null) {
  230.             return ($this->hasType($type)) ? $this->unmarkType($type) : $this->markType($type);
  231.         }
  232.         return ($toggle) ? $this->markType($type) : $this->unmarkType($type);
  233.     }
  234.     /**
  235.      * @return array|string[]
  236.      */
  237.     public function getRoles(): array
  238.     {
  239.         $roles = [];
  240.         foreach ($this->getTypesAsArray() as $type) {
  241.             $roles = [
  242.                 ...$roles,
  243.                 ...OneRosterUser::TYPES_MAPPING[$type],
  244.             ];
  245.         }
  246.         return array_values(array_unique($roles));
  247.     }
  248.     /**
  249.      * @return int|null
  250.      */
  251.     public function getCount(): ?int
  252.     {
  253.         return $this->_count;
  254.     }
  255.     /**
  256.      * @param int|null $count
  257.      * @return $this
  258.      */
  259.     public function setCount(?int $count): self
  260.     {
  261.         $this->_count $count;
  262.         return $this;
  263.     }
  264.     /**
  265.      * @return School|null
  266.      */
  267.     public function getSchool(): ?School
  268.     {
  269.         return $this->school;
  270.     }
  271.     /**
  272.      * @param School|null $school
  273.      * @return self
  274.      */
  275.     public function setSchool(?School $school): self
  276.     {
  277.         $this->school $school;
  278.         return $this;
  279.     }
  280.     /**
  281.      * @return bool
  282.      */
  283.     public function isFixed(): bool
  284.     {
  285.         return $this->hasFlag(self::FLAGS__FIXED);
  286.     }
  287.     /**
  288.      * @return string
  289.      */
  290.     public function getEntityClass(): string
  291.     {
  292.         return Profile::class;
  293.     }
  294.     /**
  295.      * {@inheritDoc}
  296.      */
  297.     public function getLoggableDetails(): array
  298.     {
  299.         return [
  300.             'id' => (string) $this->getId(),
  301.             'title' => $this->getName(),
  302.         ];
  303.     }
  304. }