Adds a trait 'UserAssignable' to mark models as user-assignable. Refactored from SceneTemplates, and applied to SceneAttachments, too.

This commit is contained in:
Vassyli
2021-01-29 19:57:46 +01:00
committed by Basilius Sauter
parent 147692d722
commit 60a3a8bd2b
5 changed files with 48 additions and 7 deletions
+6 -1
View File
@@ -13,6 +13,7 @@ use Doctrine\ORM\Mapping\Table;
use LotGD\Core\Attachment;
use LotGD\Core\AttachmentInterface;
use LotGD\Core\Exceptions\ArgumentException;
use LotGD\Core\Tools\Model\UserAssignable;
/**
* A SceneAttachment is a registered Attachment class to keep track of
@@ -23,6 +24,8 @@ use LotGD\Core\Exceptions\ArgumentException;
*/
class SceneAttachment
{
use UserAssignable;
/**
* @Id
* @Column(type="string", length=36, unique=True, name="class", options={"fixed"=true})
@@ -39,15 +42,17 @@ class SceneAttachment
* SceneAttachment constructor.
* @param string $class A class inheriting from AttachmentInterface.
* @param string $title
* @param bool $userAssignable
* @throws ArgumentException if $class does not implement AttachmentInterface
*/
public function __construct(string $class, string $title) {
public function __construct(string $class, string $title, bool $userAssignable = true) {
if (!is_subclass_of($class, AttachmentInterface::class)) {
throw new ArgumentException("The class '{$class}' must inherit from " . AttachmentInterface::class);
}
$this->class = $class;
$this->title = $title;
$this->setUserAssignable($userAssignable);
$this->scenes = new ArrayCollection();
}
+8 -5
View File
@@ -7,10 +7,12 @@ use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\ORM\Mapping\Table;
use LotGD\Core\Exceptions\ArgumentException;
use LotGD\Core\Exceptions\ClassNotFoundException;
use LotGD\Core\SceneTemplates\SceneTemplateInterface;
use LotGD\Core\Tools\Model\UserAssignable;
/**
* Class SceneTemplates.
@@ -19,15 +21,14 @@ use LotGD\Core\SceneTemplates\SceneTemplateInterface;
*/
class SceneTemplate
{
use UserAssignable;
/** @Id @Column(type="string", length=255, unique=True, name="class") */
protected string $class;
/** @Column(type="string", length=255, name="module") */
protected string $module;
/** @Column(type="boolean", options={"default"=true}) */
protected bool $userAssignable = true;
/**
* @OneToMany(targetEntity="Scene", mappedBy="template")
* @var Collection<Scene>
@@ -44,10 +45,11 @@ class SceneTemplate
* SceneTemplates constructor.
* @param string $class FQCN of the scene handling class.
* @param string $module Module from where the class is from.
* @throws ClassNotFoundException
* @param bool $userAssignable Set to false to flag the scene as not-assignable for the user.
* @throws ArgumentException
* @throws ClassNotFoundException
*/
public function __construct(string $class, string $module)
public function __construct(string $class, string $module, bool $userAssignable = true)
{
if (!\class_exists($class)) {
throw new ClassNotFoundException("The class {$class} cannot be found.");
@@ -57,6 +59,7 @@ class SceneTemplate
$this->class = $class;
$this->module = $module;
$this->setUserAssignable($userAssignable);
}
/**
+29
View File
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace LotGD\Core\Tools\Model;
use Doctrine\ORM\Mapping\Column;
trait UserAssignable
{
/** @Column(type="boolean", options={"default"=true}) */
protected bool $userAssignable = true;
/**
* Changes whether the template should be able to get manually assigned to a template or not.
* @param bool $flag
*/
public function setUserAssignable(bool $flag = true)
{
$this->userAssignable = $flag;
}
/**
* @return bool
*/
public function isUserAssignable(): bool
{
return $this->userAssignable;
}
}
+4 -1
View File
@@ -111,11 +111,13 @@ class SceneAttachmentTest extends CoreModelTestCase
$em = $this->getEntityManager();
$scene = new Scene("Test scene", "A test scene");
$sceneAttachment = new SceneAttachment(TestAttachment::class, "Test Attachment");
$sceneAttachment = new SceneAttachment(TestAttachment::class, "Test Attachment", userAssignable: false);
$scene->addSceneAttachment($sceneAttachment);
$sceneId = $scene->getId();
$this->assertFalse($sceneAttachment->isUserAssignable());
// persist
$em->persist($scene);
$em->flush();
@@ -127,6 +129,7 @@ class SceneAttachmentTest extends CoreModelTestCase
// assert
$this->assertInstanceOf(SceneAttachment::class, $retrievedSceneAttachment);
$this->assertCount(1, $retrievedSceneAttachment->getScenes());
$this->assertFalse($sceneAttachment->isUserAssignable());
// remove scene
$scene = $retrievedSceneAttachment->getScenes()[0];
+1
View File
@@ -21,6 +21,7 @@ scene_attachments:
-
class: "LotGD\\Core\\Tests\\Models\\TestAttachmentWithActions"
title: "Test Attachment With Actions"
userAssignable: false
scenes:
-
id: "30000000-0000-0000-0000-000000000001"