3131
3232class GroupHelper {
3333 /** @var IEvent[] */
34- protected $ event = [];
35- /** @var int */
36- protected $ lastEvent = 0 ;
37-
38- /** @var bool */
39- protected $ allowGrouping ;
34+ protected array $ event = [];
35+ protected int $ lastEvent = 0 ;
36+ protected bool $ allowGrouping = true ;
4037
4138 public function __construct (
4239 protected IL10N $ l ,
4340 protected IManager $ activityManager ,
4441 protected IValidator $ richObjectValidator ,
45- protected LoggerInterface $ logger) {
46- $ this -> allowGrouping = true ;
42+ protected LoggerInterface $ logger
43+ ) {
4744 }
4845
49- /**
50- * @param IL10N $l
51- */
52- public function setL10n (IL10N $ l ) {
46+ public function resetEvents (): void {
47+ $ this ->event = [];
48+ $ this ->lastEvent = 0 ;
49+ }
50+
51+ public function setL10n (IL10N $ l ): void {
5352 $ this ->l = $ l ;
5453 }
5554
5655 /**
5756 * Add an activity to the internal array
58- *
59- * @param array $activity
6057 */
61- public function addActivity ($ activity ) {
58+ public function addActivity (array $ activity ): void {
6259 $ id = (int ) $ activity ['activity_id ' ];
6360 $ event = $ this ->arrayToEvent ($ activity );
61+ $ this ->addEvent ($ id , $ event );
62+ }
63+
64+ /**
65+ * Add an event to the internal array
66+ */
67+ public function addEvent (int $ id , IEvent $ event ): void {
6468 $ language = $ this ->l ->getLanguageCode ();
6569
6670 foreach ($ this ->activityManager ->getProviders () as $ provider ) {
@@ -71,46 +75,49 @@ public function addActivity($activity) {
7175 } else {
7276 $ event = $ provider ->parse ($ language , $ event );
7377 }
74- try {
75- $ this ->richObjectValidator ->validate ($ event ->getRichSubject (), $ event ->getRichSubjectParameters ());
76- } catch (InvalidObjectExeption $ e ) {
77- $ this ->logger ->error (
78- $ e ->getMessage (),
79- [
80- 'app ' => 'activity ' ,
81- 'exception ' => $ e
82- ],
83- );
84- $ event ->setRichSubject ('Rich subject or a parameter for " ' . $ event ->getRichSubject () . '" is malformed ' , []);
85- $ event ->setParsedSubject ('Rich subject or a parameter for " ' . $ event ->getRichSubject () . '" is malformed ' );
86- }
87-
88- if ($ event ->getRichMessage ()) {
89- try {
90- $ this ->richObjectValidator ->validate ($ event ->getRichMessage (), $ event ->getRichMessageParameters ());
91- } catch (InvalidObjectExeption $ e ) {
92- $ this ->logger ->error (
93- $ e ->getMessage (),
94- [
95- 'app ' => 'activity ' ,
96- 'exception ' => $ e
97- ],
98- );
99- $ event ->setRichMessage ('Rich message or a parameter is malformed ' , []);
100- $ event ->setParsedMessage ('Rich message or a parameter is malformed ' );
101- }
102- }
78+ } catch (\InvalidArgumentException $ e ) {
79+ } catch (\Throwable $ e ) {
80+ $ this ->logger ->error ('Error while parsing activity event ' , ['exception ' => $ e ]);
81+ }
82+ }
10383
104- $ this ->activityManager ->setFormattingObject ('' , 0 );
84+ try {
85+ $ this ->richObjectValidator ->validate ($ event ->getRichSubject (), $ event ->getRichSubjectParameters ());
86+ } catch (InvalidObjectExeption $ e ) {
87+ $ this ->logger ->error (
88+ $ e ->getMessage (),
89+ [
90+ 'app ' => 'activity ' ,
91+ 'exception ' => $ e
92+ ],
93+ );
94+ $ event ->setRichSubject ('Rich subject or a parameter for " ' . $ event ->getRichSubject () . '" is malformed ' , []);
95+ $ event ->setParsedSubject ('Rich subject or a parameter for " ' . $ event ->getRichSubject () . '" is malformed ' );
96+ }
10597
106- $ child = $ event ->getChildEvent ();
107- if ($ child instanceof IEvent) {
108- unset($ this ->event [$ this ->lastEvent ]);
109- }
110- } catch (\InvalidArgumentException $ e ) {
98+ if ($ event ->getRichMessage ()) {
99+ try {
100+ $ this ->richObjectValidator ->validate ($ event ->getRichMessage (), $ event ->getRichMessageParameters ());
101+ } catch (InvalidObjectExeption $ e ) {
102+ $ this ->logger ->error (
103+ $ e ->getMessage (),
104+ [
105+ 'app ' => 'activity ' ,
106+ 'exception ' => $ e
107+ ],
108+ );
109+ $ event ->setRichMessage ('Rich message or a parameter is malformed ' , []);
110+ $ event ->setParsedMessage ('Rich message or a parameter is malformed ' );
111111 }
112112 }
113113
114+ $ this ->activityManager ->setFormattingObject ('' , 0 );
115+
116+ $ child = $ event ->getChildEvent ();
117+ if ($ child instanceof IEvent) {
118+ unset($ this ->event [$ this ->lastEvent ]);
119+ }
120+
114121 if (!$ event ->getParsedSubject ()) {
115122 $ this ->logger ->debug ('Activity " ' . $ event ->getRichSubject () . '" was not parsed by any provider ' );
116123 return ;
@@ -125,7 +132,7 @@ public function addActivity($activity) {
125132 *
126133 * @return array translated activities ready for use
127134 */
128- public function getActivities () {
135+ public function getActivities (): array {
129136 $ return = [];
130137 foreach ($ this ->event as $ id => $ event ) {
131138 $ return [] = $ this ->eventToArray ($ event , $ id );
@@ -144,11 +151,7 @@ public function getEvents(): array {
144151 return $ return ;
145152 }
146153
147- /**
148- * @param array $row
149- * @return IEvent
150- */
151- protected function arrayToEvent (array $ row ) {
154+ protected function arrayToEvent (array $ row ): IEvent {
152155 $ event = $ this ->activityManager ->generateEvent ();
153156 $ event ->setApp ((string ) $ row ['app ' ])
154157 ->setType ((string ) $ row ['type ' ])
@@ -164,14 +167,10 @@ protected function arrayToEvent(array $row) {
164167 }
165168
166169 /**
167- * @param IEvent $event
168170 * @param (int|string) $id
169- *
170- * @return array
171- *
172171 * @psalm-param array-key $id
173172 */
174- protected function eventToArray (IEvent $ event , $ id ) {
173+ protected function eventToArray (IEvent $ event , $ id ): array {
175174 return [
176175 'activity_id ' => $ id ,
177176 'app ' => $ event ->getApp (),
@@ -198,10 +197,6 @@ protected function eventToArray(IEvent $event, $id) {
198197 ];
199198 }
200199
201- /**
202- * @param IEvent $event
203- * @return array
204- */
205200 protected function getObjectsFromChildren (IEvent $ event ): array {
206201 $ child = $ event ->getChildEvent ();
207202 $ objects = [];
0 commit comments