Test Setup Failed
Push — master ( ca9897...8a7fbb )
by he
14:15 queued 06:25
created

UserDeletedObserver::cleanRelatedTopic()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
nc 4
nop 1
dl 0
loc 11
rs 10
c 1
b 0
f 0
1
<?php
2
3
4
namespace App\Listeners;
5
6
7
use App\Entities\Topic;
8
use App\Entities\User;
9
use App\Repositories\TopicRepository;
10
11
class UserDeletedObserver
12
{
13
    /**
14
     * @var TopicRepository
15
     */
16
    private $topicRepository;
17
18
    public function __construct()
19
    {
20
        $this->topicRepository = app(TopicRepository::class);
21
    }
22
23
    public function handle(User $user) {
24
        $this->cleanRelatedTopic($user);
25
    }
26
27
    /**
28
     * @param User $user
29
     */
30
    public function cleanRelatedTopic(User $user): void
31
    {
32
        /** @var Topic[] $topics */
33
        app('log')->info("user {$user->id} deleted, remove related topics");
34
        $topics = $this->topicRepository->findAllBy('user_id', $user->id);
35
        foreach ($topics as $topic) {
36
            try {
37
                $topic->replies()->delete();
38
                $topic->delete();
39
            } catch (\Exception $e) {
40
                app('log')->error("remove topic failed! {$e->getMessage()}");
41
            }
42
        }
43
    }
44
}
45