1: <?php
2: /**
3: * A server decorator that counts the number of database calls and
4: * reports them via a logger.
5: *
6: * PHP version 5
7: *
8: * @category Kolab
9: * @package Kolab_Server
10: * @author Gunnar Wrobel <wrobel@pardus.de>
11: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
12: * @link http://pear.horde.org/index.php?package=Kolab_Server
13: */
14:
15: /**
16: * A server decorator that counts the number of database calls and
17: * reports them via a logger.
18: *
19: * Copyright 2008-2012 Horde LLC (http://www.horde.org/)
20: *
21: * See the enclosed file COPYING for license information (LGPL). If you
22: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
23: *
24: * @category Kolab
25: * @package Kolab_Server
26: * @author Gunnar Wrobel <wrobel@pardus.de>
27: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
28: * @link http://pear.horde.org/index.php?package=Kolab_Server
29: */
30: class Horde_Kolab_Server_Decorator_Count
31: implements Horde_Kolab_Server_Interface
32: {
33: /**
34: * The server we delegate to.
35: *
36: * @var Horde_Kolab_Server
37: */
38: private $_server;
39:
40: /**
41: * The log handler.
42: *
43: * @var Horde_Log_Logger
44: */
45: private $_logger;
46:
47: /**
48: * The statistic.
49: *
50: * @var array
51: */
52: private $_count = array();
53:
54: /**
55: * Constructor.
56: *
57: * @param Horde_Kolab_Server $server The base server connection.
58: * @param mixed $logger The log handler. The class must at
59: * least provide the info() method.
60: */
61: public function __construct(
62: Horde_Kolab_Server_Interface $server,
63: $logger
64: ) {
65: $this->_server = $server;
66: $this->_logger = $logger;
67: }
68:
69: /**
70: * Destructor.
71: *
72: * Logs the counted events.
73: */
74: public function __destruct()
75: {
76: foreach ($this->_count as $method => $count) {
77: $this->_logger->info(
78: sprintf(
79: 'Horde_Kolab_Server: Method %s called %s times.',
80: $method, $count
81: )
82: );
83: }
84: }
85:
86: /**
87: * Connect to the server.
88: *
89: * @param string $guid The global unique id of the user.
90: * @param string $pass The password.
91: *
92: * @return NULL.
93: *
94: * @throws Horde_Kolab_Server_Exception If the connection failed.
95: */
96: public function connectGuid($guid = null, $pass = null)
97: {
98: $this->_server->connectGuid($guid, $pass);
99: if (!isset($this->_count['connectGuid'])) {
100: $this->_count['connectGuid'] = 1;
101: } else {
102: $this->_count['connectGuid']++;
103: }
104: }
105:
106: /**
107: * Get the current GUID
108: *
109: * @return string The GUID of the connected user.
110: */
111: public function getGuid()
112: {
113: return $this->_server->getGuid();
114: }
115:
116: /**
117: * Get the base GUID of this server
118: *
119: * @return string The base GUID of this server.
120: */
121: public function getBaseGuid()
122: {
123: return $this->_server->getBaseGuid();
124: }
125:
126: /**
127: * Low level access to reading object data.
128: *
129: * @param string $guid The object to retrieve.
130: * @param array $attrs Restrict to these attributes.
131: *
132: * @return array An array of attributes.
133: *
134: * @throws Horde_Kolab_Server_Exception If the search operation hit an error
135: * or returned no result.
136: */
137: public function read($guid, array $attrs = array())
138: {
139: if (!isset($this->_count['read'])) {
140: $this->_count['read'] = 1;
141: } else {
142: $this->_count['read']++;
143: }
144: return $this->_server->read($guid);
145: }
146:
147: /**
148: * Low level access to reading some object attributes.
149: *
150: * @param string $guid The object to retrieve.
151: * @param string $attrs Restrict to these attributes.
152: *
153: * @return array An array of attributes.
154: *
155: * @throws Horde_Kolab_Server_Exception
156: *
157: * @see Horde_Kolab_Server::read
158: */
159: public function readAttributes($guid, array $attrs)
160: {
161: if (!isset($this->_count['readAttributes'])) {
162: $this->_count['readAttributes'] = 1;
163: } else {
164: $this->_count['readAttributes']++;
165: }
166: return $this->_server->readAttributes($guid, $attrs);
167: }
168:
169: /**
170: * Finds object data matching a given set of criteria.
171: *
172: * @param string $query The LDAP search query
173: * @param array $params Additional search parameters.
174: *
175: * @return Horde_Kolab_Server_Result The result object.
176: *
177: * @throws Horde_Kolab_Server_Exception
178: */
179: public function find($query, array $params = array())
180: {
181: if (!isset($this->_count['find'])) {
182: $this->_count['find'] = 1;
183: } else {
184: $this->_count['find']++;
185: }
186: return $this->_server->find($query, $params);
187: }
188:
189: /**
190: * Finds all object data below a parent matching a given set of criteria.
191: *
192: * @param string $query The LDAP search query
193: * @param string $parent The parent to search below.
194: * @param array $params Additional search parameters.
195: *
196: * @return Horde_Kolab_Server_Result The result object.
197: *
198: * @throws Horde_Kolab_Server_Exception
199: */
200: public function findBelow($query, $parent, array $params = array())
201: {
202: if (!isset($this->_count['findBelow'])) {
203: $this->_count['findBelow'] = 1;
204: } else {
205: $this->_count['findBelow']++;
206: }
207: return $this->_server->findBelow($query, $parent, $params);
208: }
209:
210: /**
211: * Modify existing object data.
212: *
213: * @param Horde_Kolab_Server_Object $object The object to be modified.
214: * @param array $data The attributes of the object
215: * to be stored.
216: *
217: * @return NULL
218: *
219: * @throws Horde_Kolab_Server_Exception
220: */
221: public function save(
222: Horde_Kolab_Server_Object_Interface $object,
223: array $data
224: ) {
225: $this->_server->save($object, $data);
226: if (!isset($this->_count['save'])) {
227: $this->_count['save'] = 1;
228: } else {
229: $this->_count['save']++;
230: }
231: }
232:
233: /**
234: * Add new object data.
235: *
236: * @param Horde_Kolab_Server_Object $object The object to be added.
237: * @param array $data The attributes of the object
238: * to be added.
239: *
240: * @return NULL
241: *
242: * @throws Horde_Kolab_Server_Exception
243: */
244: public function add(
245: Horde_Kolab_Server_Object_Interface $object,
246: array $data
247: ) {
248: $this->_server->add($object, $data);
249: if (!isset($this->_count['add'])) {
250: $this->_count['add'] = 1;
251: } else {
252: $this->_count['add']++;
253: }
254: }
255:
256: /**
257: * Delete an object.
258: *
259: * @param string $guid The GUID of the object to be deleted.
260: *
261: * @return NULL
262: *
263: * @throws Horde_Kolab_Server_Exception
264: */
265: public function delete($guid)
266: {
267: $this->_server->delete($guid);
268: if (!isset($this->_count['delete'])) {
269: $this->_count['delete'] = 1;
270: } else {
271: $this->_count['delete']++;
272: }
273: }
274:
275: /**
276: * Rename an object.
277: *
278: * @param string $guid The GUID of the object to be renamed.
279: * @param string $new The new GUID of the object.
280: *
281: * @return NULL
282: *
283: * @throws Horde_Kolab_Server_Exception
284: */
285: public function rename($guid, $new)
286: {
287: $this->_server->rename($guid, $new);
288: if (!isset($this->_count['rename'])) {
289: $this->_count['rename'] = 1;
290: } else {
291: $this->_count['rename']++;
292: }
293: }
294:
295: /**
296: * Return the ldap schema.
297: *
298: * @return Horde_Ldap_Schema The LDAP schema.
299: *
300: * @throws Horde_Kolab_Server_Exception If retrieval of the schema failed.
301: */
302: public function getSchema()
303: {
304: return $this->_server->getSchema();
305: if (!isset($this->_count['getSchema'])) {
306: $this->_count['getSchema'] = 1;
307: } else {
308: $this->_count['getSchema']++;
309: }
310: }
311:
312: /**
313: * Get the parent GUID of this object.
314: *
315: * @param string $guid The GUID of the child.
316: *
317: * @return string the parent GUID of this object.
318: */
319: public function getParentGuid($guid)
320: {
321: return $this->_server->getParentGuid($guid);
322: }
323: }
324: