Setconfig   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 7
eloc 35
c 2
b 1
f 0
dl 0
loc 81
rs 10

2 Methods

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