forked from oroinc/OroAkeneoBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidateConnectionController.php
120 lines (105 loc) · 4.56 KB
/
ValidateConnectionController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
namespace Oro\Bundle\AkeneoBundle\Controller;
use Akeneo\Pim\ApiClient\Exception\ExceptionInterface;
use Oro\Bundle\AkeneoBundle\Entity\AkeneoSettings;
use Oro\Bundle\AkeneoBundle\Integration\AkeneoTransportInterface;
use Oro\Bundle\CurrencyBundle\Provider\CurrencyProviderInterface;
use Oro\Bundle\IntegrationBundle\Entity\Channel;
use Oro\Bundle\IntegrationBundle\Form\Type\ChannelType;
use Oro\Bundle\SecurityBundle\Annotation\Acl;
use Psr\Http\Client\ClientExceptionInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
class ValidateConnectionController extends AbstractController
{
const CONNECTION_SUCCESSFUL_MESSAGE = 'oro.akeneo.connection.successfull';
const CONNECTION_ERROR_MESSAGE = 'oro.akeneo.connection.error';
/** @var CurrencyProviderInterface */
private $currencyProvider;
/** @var TranslatorInterface */
private $translator;
/** @var AkeneoTransportInterface */
private $akeneoTransport;
public function __construct(
CurrencyProviderInterface $currencyProvider,
TranslatorInterface $translator,
AkeneoTransportInterface $akeneoTransport
) {
$this->currencyProvider = $currencyProvider;
$this->translator = $translator;
$this->akeneoTransport = $akeneoTransport;
}
/**
* @Route(path="/validate-akeneo-connection/{channelId}/", name="oro_akeneo_validate_connection", methods={"POST"})
* @ParamConverter("channel", class="OroIntegrationBundle:Channel", options={"id"="channelId"})
*
* @Acl(
* id="oro_integration_channel",
* type="entity",
* class="OroIntegrationBundle:Channel",
* permission="VIEW"
* )
*
* @throws \InvalidArgumentException
*/
public function validateConnectionAction(Request $request, Channel $channel = null): JsonResponse
{
if (!$channel) {
$channel = new Channel();
}
$form = $this->createForm(ChannelType::class, $channel);
$form->handleRequest($request);
/** @var AkeneoSettings $akeneoSettings */
$akeneoSettings = $channel->getTransport();
$channelId = $channel->getTransport()->getId();
if ($channelId && null == $akeneoSettings->getPassword()) {
$entityManager = $this->container->get('doctrine')->getManagerForClass(AkeneoSettings::class);
$repository = $entityManager->getRepository(AkeneoSettings::class);
$akeneoSettingsEntity = $repository->findOneBy(['id' => $channelId]);
$akeneoSettings->setPassword($akeneoSettingsEntity->getPassword());
}
$akeneoChannelNames = [];
$akeneoCurrencies = [];
$akeneoLocales = [];
try {
$this->akeneoTransport->init($akeneoSettings, false);
$success = true;
$message = $this->translator->trans(self::CONNECTION_SUCCESSFUL_MESSAGE);
switch ($request->get('synctype', 'all')) {
case 'channels':
$akeneoChannelNames = $this->akeneoTransport->getChannels();
break;
case 'currencies':
$akeneoCurrencies = $this->akeneoTransport->getMergedCurrencies();
break;
case 'locales':
$akeneoLocales = $this->akeneoTransport->getLocales();
break;
default:
$akeneoChannelNames = $this->akeneoTransport->getChannels();
$akeneoCurrencies = $this->akeneoTransport->getMergedCurrencies();
$akeneoLocales = $this->akeneoTransport->getLocales();
}
} catch (ClientExceptionInterface | ExceptionInterface $e) {
$success = false;
$message = $e->getMessage();
} catch (\Exception $e) {
$success = false;
$message = $this->translator->trans(self::CONNECTION_ERROR_MESSAGE);
}
return new JsonResponse(
[
'channels' => $akeneoChannelNames,
'akeneoCurrencies' => $akeneoCurrencies,
'akeneoLocales' => $akeneoLocales,
'success' => $success,
'message' => $message,
'currencyList' => $this->currencyProvider->getCurrencyList(),
]
);
}
}