Completed
Push — master ( 75194a...4bed0a )
by Claudson
02:12
created

Transaction   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 81.25%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 10
c 4
b 0
f 1
lcom 0
cbo 5
dl 0
loc 55
ccs 26
cts 32
cp 0.8125
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __call() 0 17 2
A __construct() 0 14 3
A setFormatter() 0 4 1
A addNewRelicParameter() 0 10 4
1
<?php
2
3
namespace EasyTaxi\NewRelic;
4
5
use EasyTaxi\NewRelic\Config\TransactionConfig;
6
use EasyTaxi\NewRelic\Exception\InvalidCallerInstanceException;
7
use EasyTaxi\NewRelic\Exception\NotLoadedNewRelicExtensionException;
8
use EasyTaxi\NewRelic\Formatter\ArgumentsFormatter;
9
use EasyTaxi\NewRelic\Formatter\FormatterInterface;
10
11
class Transaction
12
{
13
    private $instance;
14
    private $config;
15
    private $formatter;
16
17 10
    public function __construct($instance, TransactionConfig $config)
18
    {
19 10
        if (!is_object($instance)) {
20 1
            throw new InvalidCallerInstanceException();
21
        }
22
23 10
        $this->instance = $instance;
24 10
        $this->config = $config;
25 10
        $this->formatter = new ArgumentsFormatter();
26
27 10
        if (!extension_loaded('newrelic')) {
28
            throw new NotLoadedNewRelicExtensionException();
29
        }
30 10
    }
31
32
    public function setFormatter(FormatterInterface $formatter)
33
    {
34
        $this->formatter = $formatter;
35
    }
36
37 9
    public function __call($name, $arguments)
38
    {
39 9
        newrelic_set_appname($this->config->applicationName);
40 9
        newrelic_start_transaction($this->config->applicationName);
41 9
        newrelic_name_transaction($this->config->transactionName);
42 9
        $customParameters = $this->formatter->format($arguments);
43 9
        $this->addNewRelicParameter($customParameters);
44
45
        try {
46 9
            return call_user_func_array([$this->instance, $name], $arguments);
47 3
        } catch (\Exception $genericException) {
48 3
            newrelic_notice_error($genericException->getMessage(), $genericException);
49 3
            throw $genericException;
50
        } finally {
51 9
            newrelic_end_transaction();
52
        }
53
    }
54
55 9
    private function addNewRelicParameter($customParameters)
56
    {
57 9
        foreach ($customParameters as $key => $value) {
58 2
            if (null === $value || is_scalar($value)) {
59 2
                newrelic_add_custom_parameter($key, $value);
60 2
            } else {
61
                newrelic_add_custom_parameter($key, @json_encode($value));
62
            }
63 9
        }
64 9
    }
65
}
66