Reporter   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 10
c 0
b 0
f 0
wmc 9
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
B report() 0 28 8
A send() 0 7 1
1
<?php
2
/* (c) Anton Medvedev <[email protected]>
3
 *
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace Deployer\Utility;
9
10
/**
11
 * @codeCoverageIgnore
12
 */
13
class Reporter
14
{
15
    const ENDPOINT = 'https://deployer.org/api/stats';
16
17
    public static function report(array $stats)
18
    {
19
        $pid = null;
20
        // make sure function is not disabled via php.ini "disable_functions"
21
        if (extension_loaded('pcntl') && function_exists('pcntl_fork')) {
22
            declare(ticks = 1);
23
            $pid = pcntl_fork();
24
        }
25
26
        if (is_null($pid) || $pid === -1) {
27
            // Fork fails or there is no `pcntl` extension.
28
            try {
29
                self::send($stats);
30
            } catch (\Throwable $e) {
31
                // pass
32
            }
33
        } elseif ($pid === 0) {
34
            // Child process.
35
            posix_setsid();
36
            try {
37
                self::send($stats);
38
            } catch (\Throwable $e) {
39
                // pass
40
            }
41
            // Close child process after doing job.
42
            exit(0);
43
        }
44
    }
45
46
    private static function send(array $stats)
47
    {
48
        Httpie::post(self::ENDPOINT)
49
            ->body($stats)
50
            ->setopt(CURLOPT_SSL_VERIFYPEER, false)
51
            ->send();
52
    }
53
}
54