<?php
/**
* @license Silk Software House Sp. Z O. O.
*/
declare(strict_types=1);
namespace App\Entity;
use App\Entity\Traits\IdTrait;
use App\Entity\Traits\TimePreUpdateTrait;
use App\Entity\Traits\TimeTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Class UserSetting.
*
* @ORM\Entity(repositoryClass="App\Repository\UserSettingRepository")
* @ORM\Table(name="user_settings")
* @ORM\HasLifecycleCallbacks()
*/
class UserSetting
{
use IdTrait;
use TimeTrait;
use TimePreUpdateTrait;
public const DEFAULT_TOKEN_DURATION_TIME = 24;
/**
* User.
*
* @var User|null
*
* @ORM\OneToOne(targetEntity="App\Entity\User", inversedBy="setting", cascade={"persist","remove"})
* @ORM\JoinColumn(name="user_id", nullable=false, onDelete="CASCADE")
*/
private ?User $user = null;
/**
* Recipe.
*
* @var Recipe|null
*
* @Assert\NotBlank()
*
* @ORM\ManyToOne(targetEntity="App\Entity\Recipe")
* @ORM\JoinColumn(name="standard_recipe_id", nullable=true)
*/
private ?Recipe $standardRecipe = null;
/**
* Language.
*
* @var string
*
* @Assert\NotBlank()
* @Assert\Choice({"en", "pl", "sv"})
*
* @ORM\Column(name="language", type="text", nullable=false, options={"default": "en", "comment": "Application language."})
*/
private string $language = 'en';
/**
* Used devices.
*
* @ORM\ManyToMany(targetEntity="App\Entity\Device", inversedBy="userSetting")
* @ORM\JoinTable(
* name="devices_user_settings",
* joinColumns={@ORM\JoinColumn(name="user_setting_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="device_id", referencedColumnName="id")}
* )
*/
private $usedDevices;
/**
* @ORM\Column(type="integer", nullable=false, options={"default" : 7})
*/
private int $fontSizeCoefficient = 0;
/**
* @ORM\Column(type="integer", length=10, options={"default" : 24})
*/
private ?int $tokenTimeDuration = self::DEFAULT_TOKEN_DURATION_TIME;
/**
* UserSetting constructor.
*/
public function __construct()
{
$this->usedDevices = new ArrayCollection();
}
/**
* Get user.
*
* @return User
*/
public function getUser(): User
{
if (!isset($this->user)) {
throw new \LogicException('The User is not set, not expected.');
}
return $this->user;
}
/**
* Set user.
*
* @param User $user
*
* @return UserSetting
*/
public function setUser(User $user): self
{
$this->user = $user;
return $this;
}
/**
* Get a standard recipe.
*
* @return Recipe
*/
public function getStandardRecipe(): ?Recipe
{
return $this->standardRecipe;
}
/**
* Set a standard recipe.
*
* @param Recipe $standardRecipe
*
* @return UserSetting
*/
public function setStandardRecipe(Recipe $standardRecipe): self
{
$this->standardRecipe = $standardRecipe;
return $this;
}
/**
* Get language.
*
* @return string
*/
public function getLanguage(): string
{
return $this->language;
}
/**
* Set language.
*
* @param string $language
*
* @return UserSetting
*/
public function setLanguage(string $language = 'en'): self
{
if ('' !== $language) {
$this->language = $language;
}
return $this;
}
/**
* Set active devices.
*
* @param Collection $activeDevices
*
* @return UserSetting
*/
public function setUsedDevices(Collection $activeDevices): self
{
$this->usedDevices = $activeDevices;
return $this;
}
/**
* Get active devices.
*
* @return Collection
*/
public function getUsedDevices(): Collection
{
return $this->usedDevices;
}
/**
* Add a used device.
*
* @param Device $device
*
* @return UserSetting
*/
public function addUsedDevice(Device $device): self
{
if (!$this->usedDevices->contains($device)) {
$this->usedDevices->add($device);
}
return $this;
}
/**
* Remove used device.
*
* @param Device $device
*
* @return UserSetting
*/
public function removeUsedDevice(Device $device): self
{
if ($this->usedDevices->contains($device)) {
$this->usedDevices->removeElement($device);
}
return $this;
}
/**
* @return int|null
*/
public function getFontSizeCoefficient(): ?int
{
return $this->fontSizeCoefficient;
}
/**
* @param int $fontSizeCoefficient
*
* @return UserSetting
*/
public function setFontSizeCoefficient(int $fontSizeCoefficient): self
{
$this->fontSizeCoefficient = $fontSizeCoefficient;
return $this;
}
/**
* @return int|null
*/
public function getTokenTimeDuration(): ?int
{
return $this->tokenTimeDuration;
}
/**
* @param int $tokenTimeDuration
*
* @return UserSetting
*/
public function setTokenTimeDuration(int $tokenTimeDuration): self
{
$this->tokenTimeDuration = $tokenTimeDuration;
return $this;
}
}