Passed
Push — develop ( 843492...60d531 )
by Nikolay
06:55 queued 12s
created

PostController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
eloc 53
c 2
b 0
f 0
dl 0
loc 87
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A callAction() 0 14 4
A getLogFromFileAction() 0 27 3
A downloadFileAction() 0 30 4
1
<?php
2
/**
3
 * Copyright (C) MIKO LLC - All Rights Reserved
4
 * Unauthorized copying of this file, via any medium is strictly prohibited
5
 * Proprietary and confidential
6
 * Written by Nikolay Beketov, 4 2020
7
 *
8
 */
9
10
namespace MikoPBX\PBXCoreREST\Controllers\Syslog;
11
12
use MikoPBX\PBXCoreREST\Controllers\BaseController;
13
use Phalcon\Di;
14
15
/**
16
 * /pbxcore/api/syslog/{name}' Get system logs (POST).
17
 *
18
 * Get logfiles strings partially and filtered
19
 *   curl -X POST -d '{"filename": "asterisk/messages","filter":"","lines":"500"}'
20
 *   http://127.0.0.1/pbxcore/api/syslog/getLogFromFile;
21
 *
22
 * Download logfile by name
23
 *  curl -X POST -d '{"filename": "asterisk/messages"}'
24
 *  http://127.0.0.1/pbxcore/api/syslog/downloadLogFile;
25
 *
26
 * Ask for zipped logs and PCAP file
27
 * curl -X POST -d '{"filename": "/tmp/file.zip"}'
28
 * http://127.0.0.1/pbxcore/api/syslog/downloadLogsArchive;
29
 *
30
 */
31
class PostController extends BaseController
32
{
33
    public function callAction($actionName): void
34
    {
35
        $data = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
36
        switch ($actionName) {
37
            case 'getLogFromFile':
38
                $this->getLogFromFileAction();
39
                break;
40
            case 'downloadLogFile':
41
            case 'downloadLogsArchive':
42
                $this->downloadFileAction($actionName);
43
                break;
44
            default:
45
                $data = $this->request->getPost();
46
                $this->sendRequestToBackendWorker('syslog', $actionName, $data);
47
        }
48
    }
49
50
    /**
51
     * Parses file content and puts it to answer
52
     */
53
    private function getLogFromFileAction(): void
54
    {
55
        $requestMessage = json_encode(
56
            [
57
                'processor' => 'syslog',
58
                'data'      => $this->request->getPost(),
59
                'action'    => 'getLogFromFile',
60
            ]
61
        );
62
        $connection     = $this->di->getShared('beanstalkConnection');
63
        $response       = $connection->request($requestMessage, 5, 0);
64
65
        if ($response !== false) {
66
            $response = json_decode($response, true);
67
68
            $filename = $response['data']['filename'] ?? '';
69
            if ( ! file_exists($filename)) {
70
                $response['messages'][] = 'Log file not found';
71
            } else {
72
                $response['data']['filename'] = $filename;
73
                $response['data']['content']  = '' . file_get_contents($filename);
74
                unlink($filename);
75
            }
76
77
            $this->response->setPayloadSuccess($response);
0 ignored issues
show
Bug introduced by
The method setPayloadSuccess() does not exist on Phalcon\Http\ResponseInterface. It seems like you code against a sub-type of Phalcon\Http\ResponseInterface such as MikoPBX\PBXCoreREST\Http\Response. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
            $this->response->/** @scrutinizer ignore-call */ 
78
                             setPayloadSuccess($response);
Loading history...
introduced by
The method setPayloadSuccess() does not exist on Phalcon\Http\Response. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
            $this->response->/** @scrutinizer ignore-call */ 
78
                             setPayloadSuccess($response);
Loading history...
78
        } else {
79
            $this->sendError(500);
80
        }
81
    }
82
83
    /**
84
     * Prepares downloadable link for log file or archive
85
     *
86
     * @param string $actionName
87
     */
88
    private function downloadFileAction(string $actionName): void
89
    {
90
        $requestMessage = json_encode(
91
            [
92
                'processor' => 'syslog',
93
                'data'      => $this->request->getPost(),
94
                'action'    => $actionName,
95
            ]
96
        );
97
        $connection     = $this->di->getShared('beanstalkConnection');
98
        $response       = $connection->request($requestMessage, 5, 0);
99
100
        if ($response !== false) {
101
            $response = json_decode($response, true);
102
            if (array_key_exists('filename', $response['data'])) {
103
                $di           = Di::getDefault();
104
                $downloadLink = $di->getShared('config')->path('www.downloadCacheDir');
105
                $filename     = $downloadLink . "/" . $response['data']['filename'] ?? '';
106
                if ( ! file_exists($filename)) {
107
                    $response['messages'][] = 'File not found';
108
                } else {
109
                    $scheme                       = $this->request->getScheme();
110
                    $host                         = $this->request->getHttpHost();
111
                    $port                         = $this->request->getPort();
112
                    $response['data']['filename'] = "{$scheme}://{$host}:{$port}/pbxcore/files/cache/{$response['data']['filename']}";
113
                }
114
            }
115
            $this->response->setPayloadSuccess($response);
116
        } else {
117
            $this->sendError(500);
118
        }
119
    }
120
}