Completed
Pull Request — master (#216)
by
unknown
08:27
created

MongoAnalytics::eavesdrop()   B

Complexity

Conditions 10
Paths 34

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 13
nc 34
nop 3
dl 0
loc 17
rs 7.2765
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
trait MongoAnalytics
4
{
5
    public function analyticalStore()
6
    {
7
        static $redis;
8
        if (!$redis) {
9
            $redis = new Predis\Client();
10
        }
11
        return $redis;
12
    }
13
14
    public function eavesdrop($opts = ['criteria' => 0, 'options' => 1], $fnc, $args = [])
15
    {
16
        if (isset($opts['readArgs']) && !$opts['readArgs']) {
17
            $criteria = $opts['criteria'] ?? [];
18
            $options = $opts['options'] ?? [];
19
            $op = $opts['operation'] ?: null;
20
            $name = $opts['name'] ?? $this->name;
0 ignored issues
show
Bug introduced by
The property name 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...
21
        } else {
22
            $criteria = isset($opts['criteria']) ? $args[$opts['criteria']] ?? [] : [];
23
            $options = isset($opts['options']) ? ($args[$opts['options']] ?? []) : [];
24
            $op = is_array($fnc) && !isset($opts['operation']) ?
25
            ($fnc[1] ?? null) : (isset($opts['operation']) ? ($opts['operation'] ?? null) : null);
26
            $name = $opts['name'] ?? $this->name;
27
        }
28
29
        return $this->analyticalDataCapture($op ?: 'unknownOperation', $criteria, $options, $fnc, $name);
30
    }
31
32
    private function analyticalNormalization(&$criteria)
33
    {
34
        if (is_array($criteria)) {
35
            array_walk($criteria, function (&$ref) {
36
                if (is_array($ref)) {
37
                    $this->analyticalNormalization($ref);
38
                    return;
39
                }
40
                $ref = null;
41
            });
42
            ksort($criteria);
43
            return;
44
        }
45
        $criteria = null;
46
    }
47
48
    private function analyticalDataCapture($op = 'criteria', $criteria = [], $options = [], $call = null, $name = null)
49
    {
50
        $payload = ["criteria" => $criteria, "options" => $options];
51
        $this->analyticalNormalization($payload);
52
        $init = microtime(true);
53
        $serialized = json_encode($payload);
54
        $redisKey = sprintf("mongodb/%s/%s/%s", $name ?: $this->name, $op, md5($serialized));
55
56
        $onEnd = function () use ($serialized, $redisKey, $init) {
57
            $diff = ceil((microtime(true) - $init) * 1000);
58
            if ($diff < 10) return;
59
60
            $time = strtotime("+1 hour 00:00");
61
            $redis = $this->analyticalStore();
62
63
            $redis->incr("$redisKey/count");
64
            $redis->set("$redisKey/criteria", $serialized);
65
            $redis->expireat("$redisKey/count", $time);
66
            $redis->expireat("$redisKey/criteria", $time);
67
            $redis->setnx("$redisKey/time", 0);
68
69
            $redis->incrby("$redisKey/time", $diff);
70
            $redis->expireat("$redisKey/time", $time);
71
        };
72
73
        if ($call) {
74
            try {
75
                return $call();
76
            } finally {
77
                $onEnd();
78
            }
79
        }
80
81
        return $onEnd;
82
    }
83
}
84