Passed
Pull Request — master (#5)
by
unknown
08:34
created

ModelListener::activityUpdated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GetStream\Doctrine;
4
5
use Doctrine\ORM\Mapping as ORM;
6
use GetStream\Doctrine\Activity;
7
8
class ModelListener
9
{
10
    /**
11
     * @var FeedManagerInterface
12
     */
13
    private $feedManager;
14
15
    /**
16
     * @param FeedManagerInterface $feedManager
17
     */
18 6
    public function __construct(FeedManagerInterface $feedManager)
19
    {
20 6
        $this->feedManager = $feedManager;
21 6
    }
22
23
    /**
24
     * @ORM\PostPersist
25
     *
26
     * @param ActivityInterface $instance
27
     */
28 3
    public function activityCreated(ActivityInterface $instance)
29
    {
30 3
        $activity = $this->createActivity($instance);
31
32 3
        $this->feedManager
33 3
            ->getUserFeed($instance->activityActorId())
34 3
            ->addActivity($activity);
35 3
    }
36
37
    /**
38
     * @ORM\PreRemove
39
     *
40
     * @param ActivityInterface $instance
41
     */
42 2
    public function activityDeleting(ActivityInterface $instance)
43
    {
44 2
        $this->feedManager
45 2
            ->getUserFeed($instance->activityActorId())
46 2
            ->removeActivity($instance->activityForeignId(), true);
0 ignored issues
show
Bug introduced by
$instance->activityForeignId() of type string is incompatible with the type integer expected by parameter $activity_id of GetStream\Stream\BaseFeed::removeActivity(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
            ->removeActivity(/** @scrutinizer ignore-type */ $instance->activityForeignId(), true);
Loading history...
47 2
    }
48
49
    /**
50
     * @ORM\PostUpdate
51
     *
52
     * @param ActivityInterface $instance
53
     */
54 1
    public function activityUpdated(ActivityInterface $instance)
55
    {
56 1
        $activity = $this->createActivity($instance);
57 1
        $this->feedManager->getClient()->updateActivities([$activity]);
58 1
    }
59
60
    /**
61
     * @param ActivityInterface $instance
62
     *
63
     * @return array
64
     */
65 3
    private function createActivity(ActivityInterface $instance)
66
    {
67 3
        return array_merge([
68 3
            'actor' => $instance->activityActor(),
69 3
            'verb' => $instance->activityVerb(),
70 3
            'object' => $instance->activityObject(),
71 3
            'foreign_id' => $instance->activityForeignId(),
72 3
            'time' => $instance->activityTime()->format(DATE_ISO8601),
73 3
            'to' => $instance->activityNotify(),
74 3
        ], $instance->activityExtraData());
75
    }
76
}
77