store::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 41 and the first side effect is on line 28.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
17
/**
18
 * Template log writer
19
 *
20
 * @package    logstore_fluentd
21
 * @copyright  2015 Daniel Neis (based on standard log store from Petr Skoda)
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
25
26
namespace logstore_fluentd\log;
27
28
global $CFG;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
29
30
require_once($CFG->dirroot.'/admin/tool/log/store/fluentd/vendor/autoload.php');
31
require_once($CFG->dirroot.'/lib/filelib.php');
32
33
use Fluent\Autoloader,
34
    Fluent\Logger\FluentLogger;
35
36
Autoloader::register();
37
38
defined('MOODLE_INTERNAL') || die();
39
40
41
class store implements \tool_log\log\writer  {
42
    use \tool_log\helper\store,
43
        \tool_log\helper\reader;
44
45
    /** @var string $logguests true if logging guest access */
46
    protected $logguests;
47
48
    private $logger;
49
50
    public function __construct(\tool_log\log\manager $manager) {
51
        $this->helper_setup($manager);
52
        // Log everything before setting is saved for the first time.
53
        $this->logguests = $this->get_config('logguests', 1);
54
        $this->buffersize = 0;
55
        $this->logger = new FluentLogger($this->get_config('fluentd_url', 'localhost'), $this->get_config('fluentd_port', 24224));
56
    }
57
58
    /**
59
     * Should the event be ignored (== not logged)?
60
     * @param \core\event\base $event
61
     * @return bool
62
     */
63
    protected function is_event_ignored(\core\event\base $event) {
64
        if ((!CLI_SCRIPT or PHPUNIT_TEST) and !$this->logguests) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
65
            // Always log inside CLI scripts because we do not login there.
66
            if (!isloggedin() or isguestuser()) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
67
                return true;
68
            }
69
        }
70
        return false;
71
    }
72
    public function write(\core\event\base $event) {
73
        $this->logger->post($this->get_config('fluentd_tag', 'fluentd.moodle'), $event->get_data());
74
    }
75
76
    /**
77
     * Write one event to the store.
78
     *
79
     * @param \core\event\base $evententries
80
     * @return void
81
     */
82
    public function insert_event_entries($evententries) {
83
        foreach ($evententries as $e) {
84
            $this->write($e);
85
        }
86
    }
87
88
    /**
89
     * Fetch records using given criteria returning a Traversable object.
90
     *
91
     * Note that the traversable object contains a moodle_recordset, so
92
     * remember that is important that you call close() once you finish
93
     * using it.
94
     *
95
     * @param string $selectwhere
96
     * @param array $params
97
     * @param string $sort
98
     * @param int $limitfrom
99
     * @param int $limitnum
100
     * @return \core\dml\recordset_walk|\core\event\base[]
101
     */
102
    public function get_events_select_iterator($selectwhere, array $params, $sort, $limitfrom, $limitnum) {
103
        return null;
104
    }
105
106
    /**
107
     * Returns an event from the log data.
108
     *
109
     * @param \stdClass $data Log data
110
     * @return \core\event\base
111
     */
112
    public function get_log_event($data) {
113
        return null;
114
    }
115
116
    public function get_events_select_count($selectwhere, array $params) {
117
        return null;
118
    }
119
120
    public function get_internal_log_table_name() {
121
        return false;
122
    }
123
124
    /**
125
     * Are the new events appearing in the reader?
126
     *
127
     * @return bool true means new log events are being added, false means no new data will be added
128
     */
129
    public function is_logging() {
130
        // Only enabled stpres are queried,
131
        // this means we can return true here unless store has some extra switch.
132
        return true;
133
    }
134
135
    public function dispose() {
136
    }
137
}
138