Conditions | 11 |
Paths | 264 |
Total Lines | 49 |
Code Lines | 37 |
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 |
||
72 | public function execute() |
||
73 | { |
||
74 | try { |
||
75 | $response = array('status'=>null); |
||
76 | $tableName = $this->dbObject->getTableName(self::CONFIG_TABLE); |
||
77 | $secretKey = $this->getRequest()->getParam('secret'); |
||
78 | $privateKey = isset($this->config['pmt_private_key']) ? $this->config['pmt_private_key'] : null; |
||
79 | |||
80 | /** @var \Magento\Framework\DB\Adapter\AdapterInterface $dbConnection */ |
||
81 | $dbConnection = $this->dbObject->getConnection(); |
||
82 | if ($privateKey != $secretKey) { |
||
83 | $response['status'] = 401; |
||
84 | $response['result'] = 'Unauthorized'; |
||
85 | } elseif ($_SERVER['REQUEST_METHOD'] == 'POST') { |
||
86 | if (count($_POST)) { |
||
87 | foreach ($_POST as $config => $value) { |
||
88 | if (isset($this->defaultConfigs[$config]) && $response['status']==null) { |
||
89 | $dbConnection->update( |
||
90 | $tableName, |
||
91 | array('value' => $value), |
||
92 | "config='$config'" |
||
93 | ); |
||
94 | } else { |
||
95 | $response['status'] = 400; |
||
96 | $response['result'] = 'Bad request'; |
||
97 | } |
||
98 | } |
||
99 | } else { |
||
100 | $response['status'] = 422; |
||
101 | $response['result'] = 'Empty data'; |
||
102 | } |
||
103 | } |
||
104 | |||
105 | $formattedResult = array(); |
||
106 | if ($response['status']==null) { |
||
107 | $dbResult = $dbConnection->fetchAll("select * from $tableName"); |
||
108 | foreach ($dbResult as $value) { |
||
109 | $formattedResult[$value['config']] = $value['value']; |
||
110 | } |
||
111 | $response['result'] = $formattedResult; |
||
112 | } |
||
113 | $result = json_encode($response['result']); |
||
114 | header("HTTP/1.1 ".$response['status'], true, $response['status']); |
||
115 | header('Content-Type: application/json', true); |
||
116 | header('Content-Length: '.strlen($result)); |
||
117 | echo($result); |
||
118 | exit(); |
||
119 | } catch (\Exception $e) { |
||
120 | die($e->getMessage()); |
||
121 | } |
||
144 |