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