Async   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 52
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A send() 0 28 4
1
<?php
2
3
namespace Apix\Log\Emitter;
4
5
use Psr\Log\InvalidArgumentException;
6
7
class Async extends AbstractEmitter
8
{
9
    /**
10
     * Holds the emitter/transporter's command string.
11
     * The placeholders in order are:
12
     *      - #1.    (string)    payload
13
     *      - #2.    (string)    url.
14
     */
15
    protected $transporter_cmd = 'curl -X POST -d %1$s \'%2$s\'';
16
17
    /**
18
     * Constructor.
19
     *
20
     * @param string $transporter_cmd The tranpsort command string
21
     */
22 128
    public function __construct($transporter_cmd = null)
23
    {
24 128
        $this->transporter_cmd = $transporter_cmd ?: $this->transporter_cmd;
25 128
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 40
    public function send($payload)
31
    {
32 40
        if (!is_string($payload)) {
33 4
            throw new InvalidArgumentException(sprintf(
34 4
                'Expects a string, got: (%s) %s.',
35 3
                gettype($payload), json_encode($payload)
36 3
            ));
37
        }
38
39 36
        $cmd = sprintf(
40 36
            $this->transporter_cmd,
41 27
            escapeshellarg($payload),
42 36
            $this->url // urlencode($this->url)
43 27
        );
44
45 36
        if (!$this->debug) {
46 8
            $cmd .= ' > /dev/null 2>&1 &';
47
            // $cmd .= ' >&- 2>&- &';
48 6
        }
49
50 36
        exec($cmd, $output, $return_var);
51
52 28
        if ($return_var != 0) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $return_var of type integer|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
53 4
            $this->handleError($return_var, $output);
0 ignored issues
show
Documentation introduced by
$output is of type null|array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
54 3
        }
55
56 28
        return $return_var == 0;
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $return_var of type integer|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
57
    }
58
}
59