Dashbot::__construct()   B
last analyzed

Complexity

Conditions 6
Paths 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
nc 3
nop 3
dl 0
loc 20
ccs 16
cts 16
cp 1
crap 6
rs 8.9777
c 0
b 0
f 0
1
<?php
2
3
namespace Apix\Log;
4
5
use Apix\Log\Emitter\EmitterInterface as LogEmitter;
6
use Psr\Log\InvalidArgumentException;
7
8
/**
9
 * Dashbot logger for Apix Log.
10
 *
11
 * @see https://www.dashbot.io/sdk/generic
12
 */
13
class Dashbot extends AbstractTracker
14
{
15
    const TRACKER_URL =
16
        'https://tracker.dashbot.io/track?platform=%s&v=%s&type=%s&apiKey=%s';
17
18
    const TRANSPORTER_CMD =
19
        'curl -X POST -d %1$s \'%2$s\' -H \'Content-Type: application/json\'';
20
21
    // const DEFAULT_PARAMS = array(
22
    private $DEFAULT_PARAMS = array(
23
        'platform' => 'generic',    // Either generic, facebook, slack, kik
24
        'v' => '0.7.4-rest',        // API Version
25
        'type' => null,             // Hit type (required)
26
        'apiKey' => null,           // API key (required)
27
    );
28
29
    /**
30
     * Constructor.
31
     *
32
     * @param
33
     */
34 60
    public function __construct(
35
        $mixed, LogEmitter $emitter = null, LogFormatter $formatter = null
36
    ) {
37 60
        $this->setEmitter(
38 60
            $emitter ? $emitter : new Emitter\Async(self::TRANSPORTER_CMD),
39 60
            $formatter ? $formatter : new LogFormatter\Json()
40 45
        );
41 60
        $this->emitter->setParams($this->DEFAULT_PARAMS);
42
43 60
        if (is_array($mixed) && isset($mixed['apiKey'])) {
44 60
            $this->emitter->addParams($mixed);
45 47
        } elseif (is_string($mixed)) {
46 4
            $this->emitter->setParam('apiKey', $mixed);
47 3
        } else {
48 4
            throw new InvalidArgumentException(sprintf(
49 4
                '%s expects `apiKey` to be set, got: %s.',
50 4
                __CLASS__, json_encode($mixed)
51 3
            ));
52
        }
53 60
    }
54
55
    /**
56
     * Sets the platform (format).
57
     *
58
     * @see https://www.dashbot.io/sdk/template
59
     *
60
     * @param string $platform Either 'generic', 'facebook', 'slack', 'kik'
61
     *
62
     * @return self
63
     */
64 16
    public function setPlatform($platform)
65
    {
66 16
        $this->emitter->setParam('platform', $platform);
67
68 16
        return $this;
69
    }
70
71
    /**
72
     * Sets a global tag (used to combined metrics).
73
     *
74
     * @see https://www.dashbot.io/sdk/template
75
     *
76
     * @param array  $entries
0 ignored issues
show
Bug introduced by
There is no parameter named $entries. 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...
77
     * @param string $local_tag
0 ignored issues
show
Bug introduced by
There is no parameter named $local_tag. 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...
78
     *
79
     * @return self
80
     */
81 4
    public function setGlobalTag($global_tag = null)
82
    {
83 4
        $this->emitter->setParam('dashbotTemplateId', $global_tag);
84
85 4
        return $this;
86
    }
87
88
    /**
89
     * Rewrite the dashbot template Id.
90
     *
91
     * @see https://www.dashbot.io/sdk/template
92
     *
93
     * @param array $entries
94
     * @param array $params
95
     *
96
     * @return array
97
     */
98 32
    public function rewriteTemplateId(array $entries, array $params)
99
    {
100 32
        if (isset($entries['json']) && $params['platform'] == 'facebook') {
101
            if ($tpl_id = // get from entries (local) then params (global).
102 8
                $entries['dashbotTemplateId'] ?: $params['dashbotTemplateId'] ?: false
103 6
            ) {
104 8
                $entries['json']['dashbotTemplateId'] = $tpl_id;
105
                // remove from `entries` (local) to avoid duplicate.
106 8
                unset($entries['dashbotTemplateId']);
107 6
            }
108 6
        }
109
110 32
        return $entries;
111
    }
112
113
    /**
114
     * Returns the named tracking dataset.
115
     *
116
     * @param string $type
117
     * @param array  $entries
118
     * @param string $local_tag
119
     *
120
     * @return array
121
     */
122 32
    public function get($type, array $entries, $local_tag = null)
123
    {
124 32
        if ($local_tag) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $local_tag of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
125 4
            $entries['dashbotTemplateId'] = $local_tag;
126 3
        }
127
128 32
        $entries = $this->rewriteTemplateId(
129 32
            $entries, $this->emitter->getParams()
130 24
        );
131
132 32
        $this->emitter->setParam('type', $type);
133 32
        $this->emitter->setUrl(self::TRACKER_URL);
134
135 32
        $this->data= [ $type, $entries ];
0 ignored issues
show
Bug introduced by
The property data 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...
136
137 32
        return (array) $entries;
138
    }
139
140
    /**
141
     * Returns an incoming tracking dataset.
142
     *
143
     * @param array  $entries
144
     * @param string $local_tag
145
     *
146
     * @return array
147
     */
148 20
    public function incoming(array $entries, $local_tag = null)
149
    {
150 20
        return $this->get(__FUNCTION__, $entries, $local_tag);
151
    }
152
153
    /**
154
     * Returns an outgoing tracking dataset.
155
     *
156
     * @param array  $entries
157
     * @param string $local_tag
158
     *
159
     * @return array
160
     */
161 8
    public function outgoing(array $entries, $local_tag = null)
162
    {
163 8
        return $this->get(__FUNCTION__, $entries, $local_tag);
164
    }
165
}
166