@@ -118,6 +118,7 @@ public function add(array $payload): SponsoredProject
118118 public function update (int $ project_id , array $ payload ): SponsoredProject
119119 {
120120 return $ this ->tx_service ->transaction (function () use ($ project_id , $ payload ){
121+
121122 if (isset ($ payload ['name ' ])) {
122123 $ name = trim ($ payload ['name ' ]);
123124 $ formerProject = $ this ->repository ->getByName ($ name );
@@ -128,20 +129,30 @@ public function update(int $project_id, array $payload): SponsoredProject
128129
129130 $ sponsoredProject = $ this ->repository ->getById ($ project_id );
130131
131- if (is_null ( $ sponsoredProject ) || !$ sponsoredProject instanceof SponsoredProject)
132+ if (!$ sponsoredProject instanceof SponsoredProject)
132133 throw new EntityNotFoundException (sprintf ("sponsored project %s not found. " , $ project_id ));
133134
134135 SponsoredProjectFactory::populate ($ sponsoredProject , $ payload );
135136
136- if (array_key_exists ('parent_project_id ' , $ payload ) && is_null ($ payload ['parent_project_id ' ])) {
137- $ sponsoredProject ->clearParentProject ();
138- } else if (isset ($ payload ['parent_project_id ' ])) {
139- $ parentProject = $ this ->repository ->getById (intval ($ payload ['parent_project_id ' ]));
140-
141- if (is_null ($ parentProject ) || !$ parentProject instanceof SponsoredProject)
142- throw new EntityNotFoundException (sprintf ("sponsored project parent %s not found. " , $ project_id ));
137+ if (isset ($ payload ['parent_project_id ' ])) {
143138
144- $ sponsoredProject ->setParentProject ($ parentProject );
139+ $ parent_project_id = intval ($ payload ['parent_project_id ' ]);
140+ $ sponsoredProject ->clearParentProject ();
141+ if ($ parent_project_id > 0 ) {
142+ $ parentProject = $ this ->repository ->getById (intval ($ payload ['parent_project_id ' ]));
143+
144+ if (!$ parentProject instanceof SponsoredProject)
145+ throw new EntityNotFoundException
146+ (
147+ sprintf
148+ (
149+ "sponsored project parent %s not found. " ,
150+ $ parent_project_id
151+ )
152+ );
153+
154+ $ sponsoredProject ->setParentProject ($ parentProject );
155+ }
145156 }
146157
147158 return $ sponsoredProject ;
0 commit comments