Completed
Branch develop (7b6053)
by Ventimiglia
03:28
created

FirebaseLogs   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 51.52 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 34
loc 66
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A writeEventLogs() 13 13 3
A createLogger() 21 21 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
declare(strict_types = 1);
3
4
namespace ZendFirebase;
5
6
use Monolog\Logger;
7
use Monolog\Handler\StreamHandler;
8
use Monolog\Handler\FirePHPHandler;
9
use Monolog\Formatter\LineFormatter;
10
11
/**
12
 * PHP7 FIREBASE LIBRARY (http://samuelventimiglia.it/)
13
 *
14
 *
15
 * @link https://github.com/samuel20miglia/zend_Firebase
16
 * @copyright Copyright (c) 2016-now Ventimiglia Samuel - Biasin Davide
17
 * @license BSD 3-Clause License
18
 *
19
 */
20
21
22
/**
23
 * @author sviluppo
24
 *
25
 */
26
class FirebaseLogs
27
{
28
29
    private $logger;
0 ignored issues
show
Unused Code introduced by
The property $logger is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
30
    
31
    private $folderToStoreLog;
32
    /**
33
     */
34
    public function __construct($folderToStoreLog)
35
    {
36
        $this->folderToStoreLog = $folderToStoreLog;
37
        
38
        $this->createLogger($folderToStoreLog);
39
    }
40
    
41
    /**
42
     * Write log of current event
43
     *
44
     * @param Logger $logger
45
     * @param array $eventData
46
     * @param mixed $event
47
     * @param string $path
48
     */
49 View Code Duplication
    public function writeEventLogs($logger, $eventData, $event, $path)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
    {
51
        if (! empty($eventData) || null != $eventData) {
52
            $logger->addDebug("path: {$path}", [
53
                'DATA' => $eventData,
54
                'EVENT TYPE' => $event->getEventType()
55
            ]);
56
        } else {
57
            $logger->addDebug("path: {$path}", [
58
                'EVENT TYPE' => $event->getEventType()
59
            ]);
60
        }
61
    }
62
    
63
    /**
64
     *
65
     * Create logger instance for save stream log
66
     *
67
     * @param string $folderToStoreLog
68
     * @return Logger $logger
69
     */
70 View Code Duplication
    private function createLogger($folderToStoreLog)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        // the default output format is "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n"
73
        $output = "%datetime% > %level_name% > %message% %context% %extra%\n";
74
        // finally, create a formatter
75
        $formatter = new LineFormatter($output, $this->dateFormatLog);
0 ignored issues
show
Bug introduced by
The property dateFormatLog does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
76
        self::$dateFormatLogFilename = date("Y-m-d_H:i:s");
77
        // Create the logger
78
        $logger = new Logger('stream_logger');
79
    
80
        // Now add some handlers
81
        $stream = new StreamHandler(trim($folderToStoreLog) . self::$dateFormatLogFilename . ".log", Logger::DEBUG);
82
    
83
        $stream->setFormatter($formatter);
84
        $logger->pushHandler($stream);
85
        $logger->pushHandler(new FirePHPHandler());
86
    
87
        // You can now use your logger
88
        $logger->addInfo('Stream logger is ready...');
89
        return $logger;
90
    }
91
}
92
93