Completed
Push — master ( 692aa1...25e5a7 )
by Paweł
60:22
created

TenantHandler::getSubscribingMethods()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
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\Serializer;
18
19
use JMS\Serializer\GraphNavigator;
20
use JMS\Serializer\Handler\SubscribingHandlerInterface;
21
use JMS\Serializer\JsonSerializationVisitor;
22
use SWP\Bundle\CoreBundle\Model\TenantInterface;
23
use SWP\Component\MultiTenancy\Repository\TenantRepositoryInterface;
24
use Symfony\Component\Routing\RouterInterface;
25
26
final class TenantHandler implements SubscribingHandlerInterface
27
{
28
    /**
29
     * @var TenantRepositoryInterface
30
     */
31
    private $tenantRepository;
32
33
    /**
34
     * @var RouterInterface
35
     */
36
    private $router;
37
38
    /**
39
     * TenantHandler constructor.
40
     *
41
     * @param TenantRepositoryInterface $tenantRepository
42
     * @param RouterInterface           $router
43
     */
44
    public function __construct(TenantRepositoryInterface $tenantRepository, RouterInterface $router)
45
    {
46
        $this->tenantRepository = $tenantRepository;
47
        $this->router = $router;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public static function getSubscribingMethods()
54
    {
55
        return array(
56
            array(
57
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
58
                'format' => 'json',
59
                'type' => TenantInterface::class,
60
                'method' => 'serializeToJson',
61
            ),
62
        );
63
    }
64
65
    public function serializeToJson(
66
        JsonSerializationVisitor $visitor,
0 ignored issues
show
Unused Code introduced by
The parameter $visitor is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
67
        string $tenantCode
68
    ) {
69
        /** @var TenantInterface $tenant */
70
        $tenant = $this->tenantRepository->findOneByCode($tenantCode);
71
72
        if (null === $tenant) {
73
            return;
74
        }
75
76
        return [
77
           'id' => $tenant->getId(),
78
           'subdomain' => $tenant->getSubdomain(),
79
           'domainName' => $tenant->getDomainName(),
80
           'name' => $tenant->getName(),
81
           'ampEnabled' => $tenant->isAmpEnabled(),
82
            '_links' => [
83
                'self' => [
84
                    'href' => $this->router->generate('swp_api_core_get_tenant', ['code' => $tenantCode]),
85
                ],
86
            ],
87
       ];
88
    }
89
}
90