Completed
Push — master ( c3ce72...f2565d )
by Paweł
20s
created

AnalyticsEventConsumer::execute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Core Bundle.
7
 *
8
 * Copyright 2017 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2017 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\CoreBundle\Consumer;
18
19
use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface;
20
use PhpAmqpLib\Message\AMQPMessage;
21
use SWP\Bundle\AnalyticsBundle\Model\ArticleEventInterface;
22
use SWP\Bundle\AnalyticsBundle\Services\ArticleStatisticsServiceInterface;
23
use SWP\Component\MultiTenancy\Context\TenantContextInterface;
24
use SWP\Component\MultiTenancy\Resolver\TenantResolver;
25
use Symfony\Component\HttpFoundation\Request;
26
27
/**
28
 * Class AnalyticsEventConsumer.
29
 */
30
class AnalyticsEventConsumer implements ConsumerInterface
31
{
32
    /**
33
     * @var ArticleStatisticsServiceInterface
34
     */
35
    protected $articleStatisticsService;
36
37
    /**
38
     * @var TenantResolver
39
     */
40
    protected $tenantResolver;
41
42
    /**
43
     * @var TenantContextInterface
44
     */
45
    protected $tenantContext;
46
47
    /**
48
     * AnalyticsEventConsumer constructor.
49
     *
50
     * @param ArticleStatisticsServiceInterface $articleStatisticsService
51
     * @param TenantResolver                    $tenantResolver
52
     * @param TenantContextInterface            $tenantContext
53
     */
54
    public function __construct(ArticleStatisticsServiceInterface $articleStatisticsService, TenantResolver $tenantResolver, TenantContextInterface $tenantContext)
55
    {
56
        $this->articleStatisticsService = $articleStatisticsService;
57
        $this->tenantResolver = $tenantResolver;
58
        $this->tenantContext = $tenantContext;
59
    }
60
61
    /**
62
     * @param AMQPMessage $message
63
     *
64
     * @return bool|mixed
65
     */
66
    public function execute(AMQPMessage $message)
67
    {
68
        /** @var Request $request */
69
        $request = unserialize($message->getBody());
70
        if (!$request instanceof Request) {
71
            return ConsumerInterface::MSG_REJECT;
72
        }
73
74
        $this->setTenant($request);
75
        $articleId = $request->query->get('articleId', null);
76
        $action = $request->query->get('action', ArticleEventInterface::ACTION_PAGEVIEW);
77
78
        if (null !== $articleId) {
79
            $this->articleStatisticsService->addArticleEvent((int) $articleId, $action);
80
        }
81
82
        return ConsumerInterface::MSG_ACK;
83
    }
84
85
    /**
86
     * @param Request $request
87
     */
88
    private function setTenant(Request $request)
89
    {
90
        $this->tenantContext->setTenant($this->tenantResolver->resolve($request->getHost()));
91
    }
92
}
93