Test Failed
Push — master ( 82258d...0075c9 )
by Aleksander
02:30 queued 45s
created

Event::logResult()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
    namespace Eustatos\gitlab\webhook;
3
4
    /**
5
     * Event
6
     *
7
     * @package Eustatos\gitlab\webhook
8
     * @author  Alexander Astashkin <[email protected]>
9
     */
10
class Event
11
{
12
    private $_command;
13
    private $_output = [];
14
    private $_result;
15
    private $_server;
16
    const TOKEN = 'b2b pushed';
17
18
    /**
19
     * __construct
20
     *
21
     * @param string $command
22
     * @param  string $password
23
     * $param array $server
24
     * @access public
25
     */
26
    public function __construct($command, $password, $server)
27
    {
28
        $this->_command = $command;
29
        $this->_server = $server;
30
31
        if ($password == self::TOKEN) {
32
            exec(
33
                $this->_command,
34
                $this->_output,
35
                $this->_result
36
            );
37
        }
38
    }
39
40
    /**
41
     * getResult
42
     *
43
     * @access public
44
     * @return void
45
     */
46
    public function getResult()
47
    {
48
        echo 'Result execution: ' . $this->_command . PHP_EOL
49
        . $this->_result . PHP_EOL;
50
        echo '<pre>';
51
            foreach($outputString as $this->_ouput) {
52
                echo $outputString . '<br>';
0 ignored issues
show
Bug introduced by
The variable $outputString does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
53
            }
54
        echo '</pre>';
55
    }
56
57
    /**
58
     * logResult
59
     *
60
     * @param  string $file
61
     * @access public
62
     * @return void
63
     */
64
    public function logResult($file)
65
    {
66
        $this->writeToFile(
67
            $file,
68
            $this->_output
69
        );
70
    }
71
72
    /**
73
     * logRequest
74
     *
75
     * @param  string $file
76
     * @access public
77
     * @return void
78
     */
79
    public function logRequest($file)
80
    {
81
        if (strcasecmp($this->_server['REQUEST_METHOD'], 'POST') != 0) {
82
            throw new \Exception('Request method must be POST!');
83
        }
84
        $contentType = isset($this->_server['CONTENT_TYPE'])
85
            ? trim($this->_server['CONTENT_TYPE'])
86
            : '';
87
        if (strcasecmp($contentType, 'application/json') != 0) {
88
            throw new \Exception('Content type must be: application/json');
89
        }
90
        $jsonRequest = trim(file_get_contents('php://input'));
91
        $jsonRequest = json_decode($jsonRequest, true);
92
        if (!is_array($jsonRequest)) {
93
            throw new \Exception('Received content conteined invalid JSON');
94
        }
95
        $this->writeToFile(
96
            $file,
97
            $jsonRequest
98
        );
99
    }
100
101
    /**
102
     * writeToFile
103
     *
104
     * Write to file
105
     *
106
     * @param string $file    relative path to file for write the content
107
     *                        Example: `bashexec.log`
108
     *
109
     * @param  mixed  $content String or array
110
     * @access public
111
     * @return void
112
     */
113
    public function writeToFile($file, $content)
114
    {
115
        file_put_contents(
116
            __DIR__ . DIRECTORY_SEPARATOR . $file,
117
            '--------------' . date('Y-m-d H:m:s') . '------------' . PHP_EOL
118
            . print_r($content, true),
119
            FILE_APPEND | LOCK_EX
120
        );
121
    }
122
}
123