1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Superdesk Web Publisher MultiTenancy 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\MultiTenancyBundle\Form\DataTransformer; |
18
|
|
|
|
19
|
|
|
use SWP\Component\MultiTenancy\Context\TenantContextInterface; |
20
|
|
|
use SWP\Component\MultiTenancy\Model\TenantInterface; |
21
|
|
|
use SWP\Component\MultiTenancy\Repository\TenantRepositoryInterface; |
22
|
|
|
use Symfony\Component\Form\DataTransformerInterface; |
23
|
|
|
use Symfony\Component\Form\Exception\TransformationFailedException; |
24
|
|
|
use Symfony\Component\Form\Exception\UnexpectedTypeException; |
25
|
|
|
|
26
|
|
|
final class TenantToCodeTransformer implements DataTransformerInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var TenantRepositoryInterface |
30
|
|
|
*/ |
31
|
|
|
private $tenantRepository; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var TenantContextInterface |
35
|
|
|
*/ |
36
|
|
|
private $tenantContext; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* TenantToCodeTransformer constructor. |
40
|
|
|
* |
41
|
|
|
* @param TenantRepositoryInterface $tenantRepository |
42
|
|
|
* @param TenantContextInterface $tenantContext |
43
|
|
|
*/ |
44
|
|
|
public function __construct(TenantRepositoryInterface $tenantRepository, TenantContextInterface $tenantContext) |
45
|
|
|
{ |
46
|
|
|
$this->tenantRepository = $tenantRepository; |
47
|
|
|
$this->tenantContext = $tenantContext; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function transform($value) |
54
|
|
|
{ |
55
|
|
|
if (null === $value) { |
56
|
|
|
return; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (!$value instanceof TenantInterface) { |
60
|
|
|
throw new UnexpectedTypeException($value, TenantInterface::class); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $value->getId(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function reverseTransform($value) |
70
|
|
|
{ |
71
|
|
|
if (null === $value) { |
72
|
|
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$tenant = $this->tenantRepository->findOneByCode($value); |
76
|
|
|
|
77
|
|
|
if (null === $tenant) { |
78
|
|
|
throw new TransformationFailedException(sprintf( |
79
|
|
|
'Tenant with identifier "%s" equals "%s" does not exist.', |
80
|
|
|
'code', |
81
|
|
|
$value |
82
|
|
|
)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->tenantContext->setTenant($tenant); |
86
|
|
|
|
87
|
|
|
return $tenant; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|