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

GetController::getInfoAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 23
rs 9.7
cc 3
nc 3
nop 0
1
<?php
2
/**
3
 * Copyright © MIKO LLC - All Rights Reserved
4
 * Unauthorized copying of this file, via any medium is strictly prohibited
5
 * Proprietary and confidential
6
 * Written by Alexey Portnov, 8 2020
7
 */
8
9
namespace MikoPBX\PBXCoreREST\Controllers\Sysinfo;
10
11
use MikoPBX\PBXCoreREST\Controllers\BaseController;
12
13
/**
14
 * /pbxcore/api/sysinfo/{name} Get system logs (GET)
15
 *
16
 * Get system information
17
 *   curl http://172.16.156.223/pbxcore/api/sysinfo/getInfo;
18
 *
19
 * Get external IP address:
20
 *   curl http://172.16.156.212/pbxcore/api/sysinfo/getExternalIpInfo
21
 *
22
 */
23
class GetController extends BaseController
24
{
25
    public function callAction($actionName): void
26
    {
27
        $data = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $data is dead and can be removed.
Loading history...
28
        switch ($actionName) {
29
            case 'getInfo':
30
                $this->getInfoAction();
31
                break;
32
            default:
33
                $data = $this->request->getPost();
34
                $this->sendRequestToBackendWorker('sysinfo', $actionName, $data);
35
        }
36
    }
37
38
    /**
39
     * Parses content of file and puts it to answer
40
     *
41
     */
42
    private function getInfoAction(): void
43
    {
44
        $requestMessage = json_encode(
45
            [
46
                'processor' => 'sysinfo',
47
                'data'      => $this->request->getPost(),
48
                'action'    => 'getInfo',
49
            ]
50
        );
51
        $connection     = $this->di->getShared('beanstalkConnection');
52
        $response       = $connection->request($requestMessage, 30, 0);
53
        if ($response !== false) {
54
            $response = json_decode($response, true);
55
            $filename = $response['data']['filename'] ?? '';
56
            if ( ! file_exists($filename)) {
57
                $response['messages'][] = 'System information collected file not found';
58
            } else {
59
                $response['data']['content']  = '' . file_get_contents($filename);
60
                unlink($filename);
61
            }
62
            $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

62
            $this->response->/** @scrutinizer ignore-call */ 
63
                             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

62
            $this->response->/** @scrutinizer ignore-call */ 
63
                             setPayloadSuccess($response);
Loading history...
63
        } else {
64
            $this->sendError(500);
65
        }
66
    }
67
}