Passed
Push — master ( ef2bc9...940364 )
by
unknown
04:03
created

Setconfig::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 4
1
<?php
2
3
namespace Firegento\DevDashboard\Controller\Adminhtml\Ajax;
4
5
use Firegento\DevDashboard\Model\Config;
6
use Magento\Backend\App\Action;
7
8
class Setconfig extends \Magento\Backend\App\Action
9
{
10
    /**
11
     * @var \Magento\Framework\Controller\Result\JsonFactory
12
     */
13
    protected  $_resultJsonFactory;
14
15
    /**
16
     * @var \Magento\Backend\Model\Auth\Session
17
     */
18
    protected $_authSession;
19
20
    /**
21
     * @var \Firegento\DevDashboard\Api\ConfigRepositoryInterface
22
     */
23
    protected $_configRepository;
24
25
    const ADMIN_RESOURCE = 'Firegento_DevDashboard::devdashboard';
26
27
    /**
28
     * @param \Magento\Backend\App\Action\Context $context
29
     * @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
30
     * @param \Magento\Backend\Model\Auth\Session $authSession
31
     * @param \Firegento\DevDashboard\Api\ConfigRepositoryInterface $configRepository
32
     */
33
    public function __construct(
34
        Action\Context $context,
35
        \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
36
        \Magento\Backend\Model\Auth\Session $authSession,
37
        \Firegento\DevDashboard\Api\ConfigRepositoryInterface $configRepository
38
    ) {
39
        $this->_resultJsonFactory = $resultJsonFactory;
40
        $this->_authSession = $authSession;
41
        $this->_configRepository = $configRepository;
42
        parent::__construct($context);
43
    }
44
45
    public function execute()
46
    {
47
        $configuration = $this->getRequest()->getParam('configuration');
48
        $useDevdashboard = $this->getRequest()->getParam('use_devdashboard');
49
50
        if($configuration === null || $useDevdashboard === null) {
51
            $message = [
52
                'success' => false,
53
                'message' => 'You should at least give configuration or dev-dashboard'
54
            ];
55
        }
56
        else {
57
            $userId = $this->_authSession->getUser()->getId();
58
            try {
59
60
                /** @var Config $config */
61
                $config = $this->_configRepository->getByUserId($userId);
62
63
                if($configuration !== null) {
64
                    $config->setData('configuration', $configuration);
65
                }
66
67
                if($useDevdashboard !== null) {
68
                    $config->setData('use_devdashboard', $useDevdashboard);
69
                }
70
71
                $this->_configRepository->save($config);
72
73
                $message = [
74
                    'success' => true,
75
                    'user_id' => $config->getData('user_id'),
76
                    'configuration' => $config->getData('configuration'),
77
                    'use_devdashboard' => $config->getData('use_devdashboard')
78
                ];
79
80
            } catch (\Exception $e) {
81
                $message = [
82
                    'success' => false,
83
                    'message' => $e->getMessage()
84
                ];
85
            }
86
        }
87
88
89
        /** @var \Magento\Framework\Controller\Result\Json $result */
90
        $result = $this->_resultJsonFactory->create();
91
        return $result->setData($message);
92
    }
93
}