Completed
Push — master ( 19353e...1a6e95 )
by Stanislav
03:33
created

Transaction::transactionEnd()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 2
1
<?php
2
3
namespace DJStarCOM\NewRelic;
4
5
use DJStarCOM\NewRelic\Config\TransactionConfig;
6
use DJStarCOM\NewRelic\Exception\InvalidCallerInstanceException;
7
use DJStarCOM\NewRelic\Exception\NotLoadedNewRelicExtensionException;
8
use DJStarCOM\NewRelic\Formatter\ArgumentsFormatter;
9
use DJStarCOM\NewRelic\Formatter\FormatterInterface;
10
11
class Transaction
12
{
13
    private $instance;
14
    private $config;
15
    private $formatter;
16
    private $isBackground;
0 ignored issues
show
Unused Code introduced by
The property $isBackground is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
17
18
    /**
19
     * Transaction constructor.
20
     * @param $instance
21
     * @param TransactionConfig $config
22
     */
23 13
    public function __construct($instance, TransactionConfig $config)
24
    {
25 13
        if (!is_object($instance)) {
26 1
            throw new InvalidCallerInstanceException();
27
        }
28
29 13
        $this->instance = $instance;
30 13
        $this->config = $config;
31 13
        $this->formatter = new ArgumentsFormatter();
32
33 13
        if (!extension_loaded('newrelic')) {
34 1
            throw new NotLoadedNewRelicExtensionException();
35
        }
36 13
    }
37
38
    /**
39
     * @param FormatterInterface $formatter
40
     */
41
    public function setFormatter(FormatterInterface $formatter)
42
    {
43
        $this->formatter = $formatter;
44
    }
45
46
    /**
47
     * @param string $name
48
     * @param $arguments
49
     * @return mixed
50
     * @throws \Exception
51
     */
52 11
    public function __call($name, $arguments)
53
    {
54 11
        $this->transactionStart($name, $arguments);
55
56
        try {
57 11
            return call_user_func_array([$this->instance, $name], $arguments);
58 3
        } catch (\Exception $genericException) {
59 3
            $this->transactionFail($name, $genericException);
60 3
            throw $genericException;
61
        } finally {
62 11
            $this->transactionEnd($name);
63
        }
64
    }
65
66
    /**
67
     * @param $customParameters
68
     */
69 11
    private function addNewRelicParameter($customParameters)
70
    {
71 11
        foreach ($customParameters as $key => $value) {
72 2
            if (null === $value || is_scalar($value)) {
73 2
                newrelic_add_custom_parameter($key, $value);
74
            } else {
75 2
                newrelic_add_custom_parameter($key, @json_encode($value));
76
            }
77
        }
78 11
    }
79
80
    /**
81
     * @param string $name
82
     * @param mixed $arguments
83
     */
84 11
    private function transactionStart($name, $arguments)
85
    {
86 11
        if (!$this->shouldBeMonitored($name)) {
87 1
            return;
88
        }
89
90 11
        if (PHP_SAPI === 'cli') {
91 11
            newrelic_background_job(true);
92
        }
93
94 11
        newrelic_set_appname($this->config->applicationName);
95 11
        newrelic_start_transaction($this->config->applicationName);
96 11
        newrelic_name_transaction($this->config->transactionName);
97 11
        $customParameters = $this->formatter->format($arguments);
98 11
        $this->addNewRelicParameter($customParameters);
99 11
    }
100
101
    /**
102
     * @param $name
103
     * @return bool
104
     */
105 11
    private function shouldBeMonitored($name)
106
    {
107 11
        return !$this->config->monitoredMethodName || $name == $this->config->monitoredMethodName;
108
    }
109
110
    /**
111
     * @param $name
112
     */
113 11
    private function transactionEnd($name)
114
    {
115 11
        if (!$this->shouldBeMonitored($name)) {
116 1
            return;
117
        }
118
119 11
        newrelic_end_transaction();
120 11
    }
121
122
    /**
123
     * @param $name
124
     * @param \Exception $genericException
125
     */
126 3
    private function transactionFail($name, \Exception $genericException)
127
    {
128 3
        if (!$this->shouldBeMonitored($name)) {
129
            return;
130
        }
131
132 3
        newrelic_notice_error($genericException->getMessage(), $genericException);
133 3
    }
134
}
135