Getconfig::execute()   A
last analyzed

Complexity

Conditions 3
Paths 5

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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