SaveConfig::afterExecute()   A
last analyzed

Complexity

Conditions 3
Paths 5

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 11
c 2
b 1
f 0
dl 0
loc 18
rs 9.9
cc 3
nc 5
nop 1
1
<?php
2
3
namespace Firegento\DevDashboard\Plugin;
4
5
class SaveConfig
6
{
7
    /**
8
     * Form values from admin user save action, that are passed to the user specific config model
9
     *
10
     * @var array
11
     */
12
    protected $_whitelist =[
13
       'user_id',
14
       'configuration',
15
       'use_devdashboard'
16
    ];
17
18
    protected $_configRepository;
19
20
    protected $_messageManager;
21
22
    /**
23
     * SaveConfig constructor.
24
     * @param \Firegento\DevDashboard\Model\ConfigRepository $configRepository
25
     * @param \Magento\Framework\Message\ManagerInterface $messageManager
26
     */
27
    public function __construct(
28
        \Firegento\DevDashboard\Model\ConfigRepository $configRepository,
29
        \Magento\Framework\Message\ManagerInterface $messageManager
30
    ) {
31
        $this->_configRepository = $configRepository;
32
        $this->_messageManager = $messageManager;
33
    }
34
35
    /**
36
     * @param \Magento\User\Controller\Adminhtml\User\Save $subject
37
     * @return array
38
     * @throws \Exception
39
     */
40
    public function afterExecute(\Magento\User\Controller\Adminhtml\User\Save $subject)
41
    {
42
        $userId = (int)$subject->getRequest()->getParam('user_id');
43
        if ($userId === 0) {
44
            return [];
45
        }
46
        $data = $this->_filterData($subject->getRequest()->getParams());
47
48
        try {
49
50
            /** @var \Firegento\DevDashboard\Model\Config $model */
51
            $model = $this->_configRepository->getByUserId($userId);
52
            $model->setData($data);
53
            $this->_configRepository->save($model);
54
        } catch (\Exception $e) {
55
            $this->_messageManager->addErrorMessage($e->getMessage());
56
        }
57
        return [];
58
    }
59
60
    /**
61
     * @param $data
62
     * @return array
63
     */
64
    protected function _filterData($data)
65
    {
66
        return array_intersect_key($data, array_flip($this->_whitelist));
67
    }
68
}
69