LdapToolsDataCollector::getOperationsByDomain()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of the LdapToolsBundle package.
4
 *
5
 * (c) Chad Sikorra <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LdapTools\Bundle\LdapToolsBundle\DataCollector;
12
13
use LdapTools\Bundle\LdapToolsBundle\Log\LdapProfilerLogger;
14
use LdapTools\LdapManager;
15
use LdapTools\Log\LogOperation;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Response;
18
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
19
20
/**
21
 * Data Collector for LdapTools.
22
 *
23
 * @author Chad Sikorra <[email protected]>
24
 */
25
class LdapToolsDataCollector extends DataCollector
26
{
27
    /**
28
     * @var LdapManager
29
     */
30
    protected $ldap;
31
32
    /**
33
     * @var LdapProfilerLogger
34
     */
35
    protected $logger;
36
37
    /**
38
     * LdapToolsDataCollector constructor.
39
     * @param LdapProfilerLogger $logger
40
     * @param LdapManager|null $ldap
41
     */
42
    public function __construct(LdapProfilerLogger $logger, LdapManager $ldap = null)
43
    {
44
        $this->ldap = $ldap;
45
        $this->logger = $logger;
46
        $this->reset();
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function getName()
53
    {
54
        return 'ldaptools';
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function collect(Request $request, Response $response, \Exception $exception = null)
61
    {
62
        if (!$this->ldap) {
63
            return;
64
        }
65
        $this->data['domains'] = $this->ldap->getDomains();
66
67
        $this->data['operations_by_domain'] = [];
68
        foreach ($this->data['domains'] as $domain) {
69
            $this->data['operations_by_domain'][$domain] = $this->addOperationToData(...$this->logger->getOperations($domain));
70
        }
71
        $this->data['operations'] = $this->addOperationToData(...$this->logger->getOperations());
72
        $this->data['errors'] = $this->addOperationToData(...$this->logger->getErrors());
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function reset()
79
    {
80
        $this->data['domains'] = [];
81
        $this->data['errors'] = [];
82
        $this->data['operations'] = [];
83
        $this->data['operations_by_domain'] = [];
84
    }
85
86
    /**
87
     * @return array
88
     */
89
    public function getOperationsByDomain()
90
    {
91
        return $this->data['operations_by_domain'];
92
    }
93
94
    /**
95
     * @return int
96
     */
97
    public function getTime()
98
    {
99
        $time = 0;
100
101
        foreach ($this->data['operations'] as $operation) {
102
            $time += $operation['duration'];
103
        }
104
105
        return $time;
106
    }
107
108
    /**
109
     * @return array
110
     */
111
    public function getOperations()
112
    {
113
        return $this->data['operations'];
114
    }
115
116
    /**
117
     * @return array
118
     */
119
    public function getErrors()
120
    {
121
        return $this->data['errors'];
122
    }
123
124
    /**
125
     * @return string[]
126
     */
127
    public function getDomains()
128
    {
129
        return $this->data['domains'];
130
    }
131
132
    /**
133
     * @param \LdapTools\Log\LogOperation[] ...$logs
134
     * @return array
135
     */
136
    protected function addOperationToData(LogOperation ...$logs)
137
    {
138
        $logData  = [];
139
140
        foreach ($logs as $log) {
141
            $data = [];
142
143
            $data['data'] = $log->getOperation()->getLogArray();
0 ignored issues
show
Bug introduced by
The method getOperation cannot be called on $log (of type array<integer,object<LdapTools\Log\LogOperation>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
144
            $data['startTime'] = $log->getStartTime();
0 ignored issues
show
Bug introduced by
The method getStartTime cannot be called on $log (of type array<integer,object<LdapTools\Log\LogOperation>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
145
            $data['stopTime'] = $log->getStopTime();
0 ignored issues
show
Bug introduced by
The method getStopTime cannot be called on $log (of type array<integer,object<LdapTools\Log\LogOperation>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
146
            $data['domain'] = $log->getDomain();
0 ignored issues
show
Bug introduced by
The method getDomain cannot be called on $log (of type array<integer,object<LdapTools\Log\LogOperation>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
147
            $data['error'] = $log->getError();
0 ignored issues
show
Bug introduced by
The method getError cannot be called on $log (of type array<integer,object<LdapTools\Log\LogOperation>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
148
            $data['name'] = $log->getOperation()->getName();
0 ignored issues
show
Bug introduced by
The method getOperation cannot be called on $log (of type array<integer,object<LdapTools\Log\LogOperation>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
149
            $data['duration'] = ($data['stopTime'] - $data['startTime']) * 1000;
150
151
            $logData[] = $data;
152
        }
153
154
        return $logData;
155
    }
156
}
157