Conditions | 11 |
Paths | 264 |
Total Lines | 50 |
Code Lines | 38 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
37 | public function execute() |
||
38 | { |
||
39 | try { |
||
40 | $response = array('status'=>null); |
||
41 | $tableName = $this->dbObject->getTableName(self::CONFIG_TABLE); |
||
42 | $secretKey = $this->getRequest()->getParam('secret'); |
||
43 | $privateKey = isset($this->config['pmt_private_key']) ? $this->config['pmt_private_key'] : null; |
||
44 | |||
45 | /** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */ |
||
46 | $dbConnection = $this->dbObject->getConnection(); |
||
47 | if ($privateKey != $secretKey) { |
||
48 | $response['status'] = 401; |
||
49 | $response['result'] = 'Unauthorized'; |
||
50 | } elseif ($_SERVER['REQUEST_METHOD'] == 'POST') { |
||
51 | if (count($_POST)) { |
||
52 | foreach ($_POST as $config => $value) { |
||
53 | if (isset($this->defaultConfigs[$config]) && $response['status']==null) { |
||
54 | $dbConnection->update( |
||
55 | $tableName, |
||
56 | array('value' => $value), |
||
57 | "config='$$config'" |
||
58 | ); |
||
59 | } else { |
||
60 | $response['status'] = 400; |
||
61 | $response['result'] = 'Bad request'; |
||
62 | } |
||
63 | } |
||
64 | } else { |
||
65 | $response['status'] = 422; |
||
66 | $response['result'] = 'Empty data'; |
||
67 | } |
||
68 | } |
||
69 | |||
70 | if ($response['status']==null) { |
||
71 | $dbResult = $dbConnection |
||
72 | ->select() |
||
73 | ->from($tableName, array('config', 'value')); |
||
74 | foreach ($dbResult as $value) { |
||
75 | $formattedResult[$value['config']] = $value['value']; |
||
76 | } |
||
77 | $response['result'] = $formattedResult; |
||
78 | } |
||
79 | $result = json_encode($response['result']); |
||
80 | header("HTTP/1.1 ".$response['status'], true, $response['status']); |
||
81 | header('Content-Type: application/json', true); |
||
82 | header('Content-Length: '.strlen($result)); |
||
83 | echo($result); |
||
84 | exit(); |
||
85 | } catch (\Exception $e) { |
||
86 | die($e->getMessage()); |
||
87 | } |
||
90 |