GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

SolrCdcrMonitoring::getMetricName()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 12
rs 8.8571
1
<?php
2
namespace CloudWatchScript\Plugins;
3
4
use CloudWatchScript\AbstractMonitoring;
5
6
/**
7
 * Check Solr using ping URL.
8
 * Add and configure the folliwong lines to the config file
9
 * "Solr" : {
10
 *         "name" : "Name of metric and alarm",
11
 *         "url": "http://localhost:8080/solr/collection1/",
12
 *         "namespace": "Metric/Namespace",
13
 *         "description": "Descrption"
14
 * }
15
 */
16
class SolrCdcrMonitoring extends AbstractMonitoring
17
{
18
    private $solrUrl;
19
    /**
20
     * @param array $config
21
     * @param String $name
22
     */
23
    public function __construct($config, $name)
24
    {
25
        parent::__construct($config, $name);
26
        $this->solrUrl = $this->config->url;
27
    }
28
    /**
29
     * Check solr cdcr : queue size, process and buffer.
30
     * @return metric 0 Ok, 1 KO
31
     */
32
    public function getMetric()
33
    {
34
        $queue = -1;
35
        $process = 0;
36
        $buffer = 0;
37
        $cdcrQueues = @file_get_contents($this->solrUrl . "cdcr?action=QUEUES&wt=json");
38
        if($cdcrQueues !== false) {
39
            $cdcrQueues = json_decode($cdcrQueues, true);
40
            $queue = $cdcrQueues['queues'][1][1][1];
41
        }
42
        $cdcrStatus = @file_get_contents($this->solrUrl . "cdcr?action=STATUS&wt=json");
43
        if($cdcrStatus !== false) {
44
            $cdcrStatus = json_decode($cdcrStatus, true);
45
            $process = $cdcrStatus['status'][1]==='started'?1:0;
46
            $buffer = $cdcrStatus['status'][3]==='enabled'?1:0;
47
        }
48
49
        return array('queue' => $queue, 'process'=> $process, 'buffer' => $buffer);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('queue' => ..., 'buffer' => $buffer); (array) is incompatible with the return type declared by the abstract method CloudWatchScript\AbstractMonitoring::getMetric of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
50
    }
51
52
    public function getUnit($alarmName=null)
53
    {
54
         if($alarmName === null) {
55
           return array('queue' => 'Count', 'process' => 'None', 'buffer' => 'None');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('queue' => ...', 'buffer' => 'None'); (array<string,string>) is incompatible with the return type declared by the abstract method CloudWatchScript\AbstractMonitoring::getUnit of type integer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
56
         }
57
         else {
58
           switch ($alarmName) {
59
             case $this->name . " synchronisation stop":
60
               return "Count";
61
             case $this->name . " synchronisation delay (1000)":
62
               return "Count";
63
             case $this->name . " buffer":
64
               return "None";
65
             case $this->name . " process":
66
               return "None";
67
           }
68
         }
69
    }
70
    /**
71
     * Alarm if :
72
     * - queue size set to -1
73
     * - queue size great than 1000
74
     * - process is not started
75
     * - buffer is not enabled
76
     */
77
    public function getAlarms()
78
    {
79
        return array(
80
                    array("ComparisonOperator" => "LessThanThreshold",
81
                          "Threshold" => -1,
82
                          "Name" => $this->name . " synchronisation stop"
83
                    ),
84
                    array("ComparisonOperator" => "GreaterThanThreshold",
85
                          "Threshold" => 1000,
86
                          "Name" => $this->name . " synchronisation delay (1000)"
87
                    ),
88
                    array("ComparisonOperator" => "LessThanThreshold",
89
                          "Threshold" => 1,
90
                          "Name" => $this->name . " process"
91
                    ),
92
                    array("ComparisonOperator" => "LessThanThreshold",
93
                          "Threshold" => 1,
94
                          "Name" => $this->name . " buffer"
95
                    )
96
        );
97
    }
98
    public function getMetricName($alarm) {
99
      switch ($alarm) {
100
        case $this->name . " synchronisation stop":
101
          return $this->name . " queue";
102
        case $this->name . " synchronisation delay (1000)":
103
          return $this->name . " queue";
104
        case $this->name . " buffer":
105
          return $this->name . " buffer";
106
        case $this->name . " process":
107
          return $this->name . " process";
108
      }
109
   }
110
}
111