4
4
5
5
use FoF \Masquerade \Answer ;
6
6
use FoF \Masquerade \Field ;
7
+ use FoF \Masquerade \Events \FieldCreated ;
8
+ use FoF \Masquerade \Events \FieldUpdated ;
9
+ use FoF \Masquerade \Events \FieldDeleted ;
7
10
use FoF \Masquerade \FieldType \TypeFactory ;
11
+ use FoF \Masquerade \Validators \FieldValidator ;
8
12
use Flarum \User \User ;
9
13
use Illuminate \Cache \Repository ;
14
+ use Illuminate \Contracts \Events \Dispatcher ;
10
15
11
16
class FieldRepository
12
17
{
13
18
const CACHE_KEY_ALL_FIELDS = 'fof.masquerade.fields.all ' ;
14
19
const CACHE_KEY_UNCOMPLETED = 'fof.masquerade.uncompleted.u.%d ' ;
15
20
16
- /**
17
- * @var Field
18
- */
19
- protected $ field ;
20
- /**
21
- * @var Repository
22
- */
23
- protected $ cache ;
24
-
25
- /**
26
- * FieldRepository constructor.
27
- * @param Field $field
28
- * @param Repository $cache
29
- */
30
- public function __construct (Field $ field , Repository $ cache )
21
+ public function __construct (
22
+ protected Field $ field ,
23
+ protected FieldValidator $ validator ,
24
+ protected Repository $ cache ,
25
+ protected Dispatcher $ events
26
+ )
31
27
{
32
- $ this ->field = $ field ;
33
- $ this ->cache = $ cache ;
34
28
}
35
29
36
30
/**
@@ -43,39 +37,49 @@ public function all()
43
37
});
44
38
}
45
39
46
- public function store ( array $ attributes ): Field
40
+ public function clearCacheAllFields ()
47
41
{
48
- $ field = $ this ->field -> newInstance ( );
49
- $ field -> sort = $ this -> highestSort ();
42
+ $ this ->cache -> forget ( static :: CACHE_KEY_ALL_FIELDS );
43
+ }
50
44
51
- $ type = TypeFactory::typeForField ($ attributes );
45
+ public function findOrFail (string $ id )
46
+ {
47
+ return $ this ->field ->newQuery ()->findOrFail ($ id );
48
+ }
49
+
50
+ public function store (User $ actor , array $ attributes ): Field
51
+ {
52
+ $ this ->validator ->assertValid ($ attributes );
52
53
54
+ $ type = TypeFactory::typeForField ($ attributes );
53
55
$ attributes = array_merge ($ attributes , $ type ->overrideAttributes ());
54
56
55
- $ field-> fill ($ attributes );
57
+ $ field = new Field ($ attributes );
56
58
$ field ->save ();
57
59
58
- $ this ->cache ->forget (static ::CACHE_KEY_ALL_FIELDS );
60
+ $ this ->events ->dispatch (new FieldCreated ($ field , $ actor , $ attributes ));
61
+
62
+ $ this ->clearCacheAllFields ();
59
63
60
64
return $ field ;
61
65
}
62
66
63
- public function update ($ id , array $ attributes ): Field
67
+ public function update (User $ actor , Field $ field , array $ attributes ): Field
64
68
{
65
- /** @var Field */
66
- $ field = $ this ->field ->findOrFail ($ id );
69
+ $ this ->validator ->assertValid ($ attributes );
67
70
68
71
$ type = TypeFactory::typeForField ($ attributes );
69
-
70
72
$ attributes = array_merge ($ attributes , $ type ->overrideAttributes ());
71
73
72
74
$ field ->fill ($ attributes );
73
75
74
76
if ($ field ->isDirty ()) {
75
77
$ field ->save ();
76
- }
77
78
78
- $ this ->cache ->forget (static ::CACHE_KEY_ALL_FIELDS );
79
+ $ this ->events ->dispatch (new FieldUpdated ($ field , $ actor , $ attributes ));
80
+
81
+ $ this ->clearCacheAllFields ();
82
+ }
79
83
80
84
return $ field ;
81
85
}
@@ -92,19 +96,15 @@ public function sorting(array $sorting)
92
96
$ this ->cache ->forget (static ::CACHE_KEY_ALL_FIELDS );
93
97
}
94
98
95
- /**
96
- * @param $id
97
- * @return bool|Field
98
- */
99
- public function delete ($ id )
99
+ public function delete (User $ actor , Field $ field )
100
100
{
101
- $ field = $ this -> field ->findOrFail ( $ id );
101
+ $ response = $ field ->delete ( );
102
102
103
- $ field -> delete ( );
103
+ $ this -> events -> dispatch ( new FieldDeleted ( $ field , $ actor , []) );
104
104
105
- $ this ->cache -> forget ( static :: CACHE_KEY_ALL_FIELDS );
105
+ $ this ->clearCacheAllFields ( );
106
106
107
- return $ field ;
107
+ return $ response ;
108
108
}
109
109
110
110
/**
0 commit comments