Completed
Push — master ( 4c03fa...5b0bab )
by Franck
02:34
created

Dashbot::__construct()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 14
nc 3
nop 3
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
        'platform' => 'generic',    // Either generic, facebook, slack, kik
23
        'v' => '0.7.4-rest',        // API Version
24
        'type' => null,             // Hit type (required)
25
        'apiKey' => null,           // API key (required)
26
    );
27
28
    /**
29
     * Constructor.
30
     *
31
     * @param
32
     */
33
    public function __construct(
34
        $mixed, LogEmitter $emitter = null, LogFormatter $formatter = null
35
    ) {
36
        $this->setEmitter(
37
            $emitter ? $emitter : new Emitter\Async(self::TRANSPORTER_CMD),
38
            $formatter ? $formatter : new LogFormatter\Json()
39
        );
40
        $this->emitter->setParams(self::DEFAULT_PARAMS);
41
42
        if (is_array($mixed) && isset($mixed['apiKey'])) {
43
            $this->emitter->addParams($mixed);
44
        } elseif (is_string($mixed)) {
45
            $this->emitter->setParam('apiKey', $mixed);
46
        } else {
47
            throw new InvalidArgumentException(sprintf(
48
                '%s expects `apiKey` to be set, got: %s.',
49
                __CLASS__, json_encode($mixed)
50
            ));
51
        }
52
    }
53
54
    /**
55
     * Sets the platform (format).
56
     *
57
     * @see https://www.dashbot.io/sdk/template
58
     *
59
     * @param string $platform Either 'generic', 'facebook', 'slack', 'kik'
60
     *
61
     * @return self
62
     */
63
    public function setPlatform($platform)
64
    {
65
        $this->emitter->setParam('platform', $platform);
66
67
        return $this;
68
    }
69
70
    /**
71
     * Sets a global tag (used to combined metrics).
72
     *
73
     * @see https://www.dashbot.io/sdk/template
74
     *
75
     * @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...
76
     * @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...
77
     *
78
     * @return self
79
     */
80
    public function setGlobalTag($global_tag = null)
81
    {
82
        $this->emitter->setParam('dashbotTemplateId', $global_tag);
83
84
        return $this;
85
    }
86
87
    /**
88
     * Rewrite the dashbot template Id.
89
     *
90
     * @see https://www.dashbot.io/sdk/template
91
     *
92
     * @param array $entries
93
     * @param array $params
94
     *
95
     * @return array
96
     */
97
    public function rewriteTemplateId(array $entries, array $params)
98
    {
99
        if (isset($entries['json']) && $params['platform'] == 'facebook') {
100
            if ($tpl_id = // get from `entries` (local) then `params` (global).
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
101
                 $entries['dashbotTemplateId'] ?: $params['dashbotTemplateId']
102
                    ?: false
103
            ) {
104
                $entries['json']['dashbotTemplateId'] = $tpl_id;
105
                // remove from `entries` (local) to avoid duplicate.
106
                unset($entries['dashbotTemplateId']);
107
            }
108
        }
109
110
        return $entries;
111
    }
112
113
    /**
114
     * Sets the event hit type.
115
     *
116
     * @param string $type
117
     * @param array  $entries
118
     * @param string $local_tag
119
     *
120
     * @return array
121
     */
122
    protected function get($type, array $entries, $local_tag = null)
123
    {
124
        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
            $entries['dashbotTemplateId'] = $local_tag;
126
        }
127
128
        $entries = $this->rewriteTemplateId(
129
            $entries, $this->emitter->getParams()
130
        );
131
132
        $this->emitter->setParam('type', $type);
133
        $this->emitter->setUrl(self::TRACKER_URL);
134
135
        return (array) $entries;
136
    }
137
138
    /**
139
     * Returns an incoming tracking dataset.
140
     *
141
     * @param array  $entries
142
     * @param string $local_tag
143
     *
144
     * @return array
145
     */
146
    public function incoming(array $entries, $local_tag = null)
147
    {
148
        return $this->get(__FUNCTION__, $entries, $local_tag);
149
    }
150
151
    /**
152
     * Returns an outgoing tracking dataset.
153
     *
154
     * @param array  $entries
155
     * @param string $local_tag
156
     *
157
     * @return array
158
     */
159
    public function outgoing(array $entries, $local_tag = null)
160
    {
161
        return $this->get(__FUNCTION__, $entries, $local_tag);
162
    }
163
}
164