-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathTestConnection.php
98 lines (81 loc) · 2.9 KB
/
TestConnection.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
<?php
/**
* Copyright © Bazaarvoice, Inc. All rights reserved.
* See LICENSE.md for license details.
*/
namespace Bazaarvoice\Connector\Controller\Adminhtml\Config\Sftp;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Filter\StripTags;
use Bazaarvoice\Connector\Api\ConfigProviderInterface;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\Filesystem\Io\Sftp;
/**
* Class TestConnection
*
* @package Bazaarvoice\Connector\Controller\Adminhtml\Config\Sftp\TestConnection
*/
class TestConnection extends Action
{
/**
* TestConnection constructor.
*
* @param Context $context
* @param PurchaseFeed $purchaseFeed
*/
public function __construct(
Context $context,
Sftp $sftp,
ConfigProviderInterface $configProvider,
StoreInterface $store,
StoreManagerInterface $storeManager,
JsonFactory $resultJsonFactory,
StripTags $tagFilter
) {
$this->sftp = $sftp;
$this->storeManager = $storeManager;
$this->configProvider = $configProvider;
$this->resultJsonFactory = $resultJsonFactory;
$this->tagFilter = $tagFilter;
parent::__construct($context);
}
public function execute()
{
$result = [
'success' => false,
'errorMessage' => '',
];
$options = $this->getRequest()->getParams();
try {
if(empty($options['username']) || empty($options['password']) || empty($options['host'])) {
throw new \Magento\Framework\Exception\LocalizedException(
__('Missing SFTP credentials.')
);
}
$params = [
'host' => $this->configProvider->getSftpHost($this->storeManager->getStore()->getId(), ScopeInterface::SCOPE_STORE, $options['host']),
'username' => $options['username'],
'password' => $this->configProvider->getSftpPassword($this->storeManager->getStore()->getId())
];
if($options['password'] != '******') {
$params['password'] = $options['password'];
}
$this->sftp->open($params);
$this->sftp->close();
$result['success'] = true;
}catch (\Magento\Framework\Exception\LocalizedException $e) {
$result['errorMessage'] = $e->getMessage();
} catch (\Exception $e) {
$message = __($e->getMessage());
$result['errorMessage'] = $this->tagFilter->filter($message);
}
/**
* @var \Magento\Framework\Controller\Result\Json $resultJson
*/
$resultJson = $this->resultJsonFactory->create();
return $resultJson->setData($result);
}
}