Completed
Push — master ( bd6b3a...936121 )
by Aleksander
02:34
created

Event.php (4 issues)

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 array  $output
0 ignored issues
show
There is no parameter named $output. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
22
     * @param int    $result
0 ignored issues
show
There is no parameter named $result. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
23
     * @param  string $password
24
     * @access public
25
     * @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...
26
     */
27
    public function __construct($command, $password)
28
    {
29
        $this->_command = $command;
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
         print_r($this->_output);
52
         echo '</pre>';
53
    }
54
55
    /**
56
     * logResult
57
     *
58
     * @param  string $file
59
     * @access public
60
     * @return void
61
     */
62
    public function logResult($file)
63
    {
64
        $this->writeToFile(
65
            $file,
66
            $this->_output
67
        );
68
    }
69
70
    /**
71
     * logRequest
72
     *
73
     * @param  string $file
74
     * @access public
75
     * @return void
76
     */
77
    public function logRequest($file)
0 ignored issues
show
logRequest uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
78
    {
79
        if (strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') != 0) {
80
            throw new Exception('Request method must be POST!');
81
        }
82
        $contentType = isset($_SERVER['CONTENT_TYPE'])
83
            ? trim($_SERVER['CONTENT_TYPE'])
84
            : '';
85
        if (strcasecmp($contentType, 'application/json') !=0 ) {
86
            throw new Exception('Content type must be: application/json');
87
        }
88
        $jsonRequest = trim(file_get_contents('php://input'));
89
        $jsonRequest = json_decode($jsonRequest, true);
90
        if (!is_array($jsonRequest)) {
91
            throw new Exception('Received content conteined invalid JSON');
92
        }
93
        $this->writeToFile(
94
            $file,
95
            $jsonRequest
96
        );
97
    }
98
99
    /**
100
     * writeToFile
101
     *
102
     * Write to file
103
     *
104
     * @param string $file    relative path to file for write the content
105
     *                        Example: `bashexec.log`
106
     *
107
     * @param  mixed  $content String or array
108
     * @access public
109
     * @return void
110
     */
111
    public function writeToFile($file, $content)
112
    {
113
        file_put_contents(
114
            __DIR__ . DIRECTORY_SEPARATOR . $file,
115
            '--------------' . date('Y-m-d H:m:s') . '------------' . PHP_EOL
116
            . print_r($content, true),
117
            FILE_APPEND | LOCK_EX
118
        );
119
    }
120
}
121