ReactionSubscriber::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GetStream\Doctrine;
4
5
use Doctrine\Common\EventSubscriber;
6
use Doctrine\ORM\Event\LifecycleEventArgs;
7
use Doctrine\ORM\Events;
8
use GetStream\Doctrine\ReactionInterface;
9
use GetStream\Stream\Client;
10
11
class ReactionSubscriber implements EventSubscriber
12
{
13
    /**
14
     * @var Client
15
     */
16
    private $client;
17
18 1
    public function __construct(Client $client)
19
    {
20 1
        $this->client = $client;
21 1
    }
22
23
    /**
24
     * Returns an array of events this subscriber wants to listen to.
25
     *
26
     * @return string[]
27
     */
28
    public function getSubscribedEvents()
29
    {
30
        return [
31
            Events::prePersist,
32
            Events::postUpdate,
33
            Events::preRemove,
34
        ];
35
    }
36
37
    public function prePersist(LifecycleEventArgs $args)
38
    {
39
        $entity = $args->getEntity();
40
41
        if ($entity instanceof ReactionInterface) {
42
            if (null === ($activityId = $entity->getReactionActivityId())) {
0 ignored issues
show
introduced by
The condition null === $activityId = $...getReactionActivityId() is always false.
Loading history...
43
                $activityId = $this->findActivityId($entity->getReactionActivityForeignId(), $entity->getReactionActivityTime());
44
            }
45
46
            if (null === $activityId) {
0 ignored issues
show
introduced by
The condition null === $activityId is always false.
Loading history...
47
                return;
48
            }
49
50
            $getStreamReaction = $this->client->reactions()
51
                ->add($entity->getReactionKind(), $activityId, $entity->getUserId(), $entity->getReactionData(), $entity->getReactionTargets());
52
53
            $entity->setReactionId($getStreamReaction['id']);
54
        }
55
    }
56
57
    public function postUpdate(LifecycleEventArgs $args)
58
    {
59
        $entity = $args->getEntity();
60
61
        if ($entity instanceof ReactionInterface && null !== $entity->getReactionId()) {
62
            $this->client->reactions()->update($entity->getReactionId(), $entity->getReactionData(), $entity->getReactionTargets());
63
        }
64
    }
65
66
    public function preRemove(LifecycleEventArgs $args)
67
    {
68
        $entity = $args->getEntity();
69
70
        if ($entity instanceof ReactionInterface && null !== $entity->getReactionId()) {
71
            $this->client->reactions()->delete($entity->getReactionId());
72
        }
73
    }
74
75
    private function findActivityId(string $activityForeignId, \DateTimeImmutable $activityTime)
76
    {
77
        $activities = $this->client->getActivities(null, [[$activityForeignId, $activityTime]]);
78
79
        if (empty($activities) || empty($activities['results'])) {
80
            return null;
81
        }
82
83
        return $activities['results'][0]['id'];
84
    }
85
}
86