Passed
Branch master (530ffd)
by Aleksander
02:46 queued 54s
created

Event.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
    const TOKEN = 'b2b pushed';
16
17
    /**
18
     * __construct
19
     *
20
     * @param string $command
21
     * @param  string $password
22
     * @access public
23
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
24
     */
25
    public function __construct($command, $password)
26
    {
27
        $this->_command = $command;
28
29
        if ($password == self::TOKEN) {
30
            exec(
31
                $this->_command,
32
                $this->_output,
33
                $this->_result
34
            );
35
        }
36
    }
37
38
    /**
39
     * getResult
40
     *
41
     * @access public
42
     * @return void
43
     */
44
    public function getResult()
45
    {
46
        echo 'Result execution: ' . $this->_command . PHP_EOL
47
        . $this->_result . PHP_EOL;
48
        echo '<pre>';
49
            print_r($this->_output);
50
            echo '</pre>';
51
    }
52
53
    /**
54
     * logResult
55
     *
56
     * @param  string $file
57
     * @access public
58
     * @return void
59
     */
60
    public function logResult($file)
61
    {
62
        $this->writeToFile(
63
            $file,
64
            $this->_output
65
        );
66
    }
67
68
    /**
69
     * logRequest
70
     *
71
     * @param  string $file
72
     * @access public
73
     * @return void
74
     */
75
    public function logRequest($file)
76
    {
77
        if (strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') != 0) {
78
            throw new Exception('Request method must be POST!');
79
        }
80
        $contentType = isset($_SERVER['CONTENT_TYPE'])
81
            ? trim($_SERVER['CONTENT_TYPE'])
82
            : '';
83
        if (strcasecmp($contentType, 'application/json') != 0) {
84
            throw new Exception('Content type must be: application/json');
85
        }
86
        $jsonRequest = trim(file_get_contents('php://input'));
87
        $jsonRequest = json_decode($jsonRequest, true);
88
        if (!is_array($jsonRequest)) {
89
            throw new Exception('Received content conteined invalid JSON');
90
        }
91
        $this->writeToFile(
92
            $file,
93
            $jsonRequest
94
        );
95
    }
96
97
    /**
98
     * writeToFile
99
     *
100
     * Write to file
101
     *
102
     * @param string $file    relative path to file for write the content
103
     *                        Example: `bashexec.log`
104
     *
105
     * @param  mixed  $content String or array
106
     * @access public
107
     * @return void
108
     */
109
    public function writeToFile($file, $content)
110
    {
111
        file_put_contents(
112
            __DIR__ . DIRECTORY_SEPARATOR . $file,
113
            '--------------' . date('Y-m-d H:m:s') . '------------' . PHP_EOL
114
            . print_r($content, true),
115
            FILE_APPEND | LOCK_EX
116
        );
117
    }
118
}
119