Skip to content

Commit 60a3a8b

Browse files
committed
Adds a trait 'UserAssignable' to mark models as user-assignable. Refactored from SceneTemplates, and applied to SceneAttachments, too.
1 parent 147692d commit 60a3a8b

File tree

5 files changed

+48
-7
lines changed

5 files changed

+48
-7
lines changed

src/Models/SceneAttachment.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use LotGD\Core\Attachment;
1414
use LotGD\Core\AttachmentInterface;
1515
use LotGD\Core\Exceptions\ArgumentException;
16+
use LotGD\Core\Tools\Model\UserAssignable;
1617

1718
/**
1819
* A SceneAttachment is a registered Attachment class to keep track of
@@ -23,6 +24,8 @@
2324
*/
2425
class SceneAttachment
2526
{
27+
use UserAssignable;
28+
2629
/**
2730
* @Id
2831
* @Column(type="string", length=36, unique=True, name="class", options={"fixed"=true})
@@ -39,15 +42,17 @@ class SceneAttachment
3942
* SceneAttachment constructor.
4043
* @param string $class A class inheriting from AttachmentInterface.
4144
* @param string $title
45+
* @param bool $userAssignable
4246
* @throws ArgumentException if $class does not implement AttachmentInterface
4347
*/
44-
public function __construct(string $class, string $title) {
48+
public function __construct(string $class, string $title, bool $userAssignable = true) {
4549
if (!is_subclass_of($class, AttachmentInterface::class)) {
4650
throw new ArgumentException("The class '{$class}' must inherit from " . AttachmentInterface::class);
4751
}
4852

4953
$this->class = $class;
5054
$this->title = $title;
55+
$this->setUserAssignable($userAssignable);
5156

5257
$this->scenes = new ArrayCollection();
5358
}

src/Models/SceneTemplate.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
use Doctrine\ORM\Mapping\Column;
88
use Doctrine\ORM\Mapping\Entity;
99
use Doctrine\ORM\Mapping\Id;
10+
use Doctrine\ORM\Mapping\OneToMany;
1011
use Doctrine\ORM\Mapping\Table;
1112
use LotGD\Core\Exceptions\ArgumentException;
1213
use LotGD\Core\Exceptions\ClassNotFoundException;
1314
use LotGD\Core\SceneTemplates\SceneTemplateInterface;
15+
use LotGD\Core\Tools\Model\UserAssignable;
1416

1517
/**
1618
* Class SceneTemplates.
@@ -19,15 +21,14 @@
1921
*/
2022
class SceneTemplate
2123
{
24+
use UserAssignable;
25+
2226
/** @Id @Column(type="string", length=255, unique=True, name="class") */
2327
protected string $class;
2428

2529
/** @Column(type="string", length=255, name="module") */
2630
protected string $module;
2731

28-
/** @Column(type="boolean", options={"default"=true}) */
29-
protected bool $userAssignable = true;
30-
3132
/**
3233
* @OneToMany(targetEntity="Scene", mappedBy="template")
3334
* @var Collection<Scene>
@@ -44,10 +45,11 @@ class SceneTemplate
4445
* SceneTemplates constructor.
4546
* @param string $class FQCN of the scene handling class.
4647
* @param string $module Module from where the class is from.
47-
* @throws ClassNotFoundException
48+
* @param bool $userAssignable Set to false to flag the scene as not-assignable for the user.
4849
* @throws ArgumentException
50+
* @throws ClassNotFoundException
4951
*/
50-
public function __construct(string $class, string $module)
52+
public function __construct(string $class, string $module, bool $userAssignable = true)
5153
{
5254
if (!\class_exists($class)) {
5355
throw new ClassNotFoundException("The class {$class} cannot be found.");
@@ -57,6 +59,7 @@ public function __construct(string $class, string $module)
5759

5860
$this->class = $class;
5961
$this->module = $module;
62+
$this->setUserAssignable($userAssignable);
6063
}
6164

6265
/**

src/Tools/Model/UserAssignable.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace LotGD\Core\Tools\Model;
5+
6+
use Doctrine\ORM\Mapping\Column;
7+
8+
trait UserAssignable
9+
{
10+
/** @Column(type="boolean", options={"default"=true}) */
11+
protected bool $userAssignable = true;
12+
13+
/**
14+
* Changes whether the template should be able to get manually assigned to a template or not.
15+
* @param bool $flag
16+
*/
17+
public function setUserAssignable(bool $flag = true)
18+
{
19+
$this->userAssignable = $flag;
20+
}
21+
22+
/**
23+
* @return bool
24+
*/
25+
public function isUserAssignable(): bool
26+
{
27+
return $this->userAssignable;
28+
}
29+
}

tests/Models/SceneAttachmentTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,13 @@ public function testIfPersistingASceneAlsoPersistsASceneAttachment()
111111
$em = $this->getEntityManager();
112112

113113
$scene = new Scene("Test scene", "A test scene");
114-
$sceneAttachment = new SceneAttachment(TestAttachment::class, "Test Attachment");
114+
$sceneAttachment = new SceneAttachment(TestAttachment::class, "Test Attachment", userAssignable: false);
115115

116116
$scene->addSceneAttachment($sceneAttachment);
117117
$sceneId = $scene->getId();
118118

119+
$this->assertFalse($sceneAttachment->isUserAssignable());
120+
119121
// persist
120122
$em->persist($scene);
121123
$em->flush();
@@ -127,6 +129,7 @@ public function testIfPersistingASceneAlsoPersistsASceneAttachment()
127129
// assert
128130
$this->assertInstanceOf(SceneAttachment::class, $retrievedSceneAttachment);
129131
$this->assertCount(1, $retrievedSceneAttachment->getScenes());
132+
$this->assertFalse($sceneAttachment->isUserAssignable());
130133

131134
// remove scene
132135
$scene = $retrievedSceneAttachment->getScenes()[0];

tests/datasets/scene-attachment.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ scene_attachments:
2121
-
2222
class: "LotGD\\Core\\Tests\\Models\\TestAttachmentWithActions"
2323
title: "Test Attachment With Actions"
24+
userAssignable: false
2425
scenes:
2526
-
2627
id: "30000000-0000-0000-0000-000000000001"

0 commit comments

Comments
 (0)