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
|
|
|
* @var TenantContextInterface |
34
|
|
|
*/ |
35
|
|
|
private $tenantContext; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* TenantToCodeTransformer constructor. |
39
|
|
|
* |
40
|
|
|
* @param TenantRepositoryInterface $tenantRepository |
41
|
|
|
* @param TenantContextInterface $tenantContext |
42
|
|
|
*/ |
43
|
|
|
public function __construct(TenantRepositoryInterface $tenantRepository, TenantContextInterface $tenantContext) |
44
|
|
|
{ |
45
|
|
|
$this->tenantRepository = $tenantRepository; |
46
|
|
|
$this->tenantContext = $tenantContext; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function transform($value) |
53
|
|
|
{ |
54
|
|
|
if (null === $value) { |
55
|
|
|
return; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (!$value instanceof TenantInterface) { |
59
|
|
|
throw new UnexpectedTypeException($value, TenantInterface::class); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $value->getId(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public function reverseTransform($value) |
69
|
|
|
{ |
70
|
|
|
if (null === $value) { |
71
|
|
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$tenant = $this->tenantRepository->findOneByCode($value); |
75
|
|
|
|
76
|
|
|
if (null === $tenant) { |
77
|
|
|
throw new TransformationFailedException(sprintf( |
78
|
|
|
'Tenant with identifier "%s" equals "%s" does not exist.', |
79
|
|
|
'code', |
80
|
|
|
$value |
81
|
|
|
)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$this->tenantContext->setTenant($tenant); |
85
|
|
|
|
86
|
|
|
return $tenant; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|