1<?php
2/**
3 * A stop watch decorator for outgoing requests from the Kolab storage drivers.
4 *
5 * PHP version 5
6 *
7 * @category Kolab
8 * @package  Kolab_Storage
9 * @author   Gunnar Wrobel <wrobel@pardus.de>
10 * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
11 */
12
13/**
14 * A stop watch decorator for outgoing requests from the Kolab storage drivers.
15 *
16 * Copyright 2010-2017 Horde LLC (http://www.horde.org/)
17 *
18 * See the enclosed file COPYING for license information (LGPL). If you
19 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
20 *
21 * @category Kolab
22 * @package  Kolab_Storage
23 * @author   Gunnar Wrobel <wrobel@pardus.de>
24 * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
25 */
26class Horde_Kolab_Storage_Driver_Decorator_Timer
27extends Horde_Kolab_Storage_Driver_Decorator_Base
28{
29    /**
30     * A log handler.
31     *
32     * @var mixed
33     */
34    private $_logger;
35
36    /**
37     * A stop watch.
38     *
39     * @var Horde_Support_Timer
40     */
41    private $_timer;
42
43    /**
44     * Constructor.
45     *
46     * @param Horde_Kolab_Storage_Driver $driver The decorated driver.
47     * @param Horde_Support_Timer        $timer  A stop watch.
48     * @param mixed                      $logger The log handler. This instance
49     *                                           must provide the debug() method.
50     */
51    public function __construct(Horde_Kolab_Storage_Driver $driver,
52                                Horde_Support_Timer $timer,
53                                $logger)
54    {
55        $this->_logger = $logger;
56        $this->_timer = $timer;
57        parent::__construct($driver);
58    }
59
60    /**
61     * Create the backend driver.
62     *
63     * @return mixed The backend driver.
64     */
65    public function createBackend()
66    {
67        $this->_timer->push();
68        $result = parent::createBackend();
69        $this->_logger->debug(
70            sprintf(
71                'REQUEST OUT IMAP: %s ms [construct]',
72                floor($this->_timer->pop() * 1000)
73            )
74        );
75        return $result;
76    }
77
78    /**
79     * Create the specified folder.
80     *
81     * @param string $folder The folder to create.
82     *
83     * @return NULL
84     */
85    public function create($folder)
86    {
87        $this->_timer->push();
88        $result = parent::create($folder);
89        $this->_logger->debug(
90            sprintf(
91                'REQUEST OUT IMAP: %s ms [createFolder]',
92                floor($this->_timer->pop() * 1000)
93            )
94        );
95    }
96
97    /**
98     * Set the access rights for a folder.
99     *
100     * @param string $folder  The folder to act upon.
101     * @param string $user    The user to set the ACL for.
102     * @param string $acl     The ACL.
103     *
104     * @return NULL
105     */
106    public function setAcl($folder, $user, $acl)
107    {
108        $this->_timer->push();
109        parent::setAcl($folder, $user, $acl);
110        $this->_logger->debug(
111            sprintf(
112                'REQUEST OUT IMAP: %s ms [setAcl]',
113                floor($this->_timer->pop() * 1000)
114            )
115        );
116    }
117
118    /**
119     * Delete the access rights for user on a folder.
120     *
121     * @param string $folder  The folder to act upon.
122     * @param string $user    The user to delete the ACL for
123     *
124     * @return NULL
125     */
126    public function deleteAcl($folder, $user)
127    {
128        $this->_timer->push();
129        parent::deleteAcl($folder, $user);
130        $this->_logger->debug(
131            sprintf(
132                'REQUEST OUT IMAP: %s ms [deleteAcl]',
133                floor($this->_timer->pop() * 1000)
134            )
135        );
136    }
137
138    /**
139     * Retrieves a list of mailboxes from the server.
140     *
141     * @return array The list of mailboxes.
142     */
143    public function listFolders()
144    {
145        $this->_timer->push();
146        $result = parent::listFolders();
147        $this->_logger->debug(
148            sprintf(
149                'REQUEST OUT IMAP: %s ms [listFolders]',
150                floor($this->_timer->pop() * 1000)
151            )
152        );
153        return $result;
154    }
155
156    /**
157     * Retrieves the specified annotation for the complete list of mailboxes.
158     *
159     * @param string $annotation The name of the annotation to retrieve.
160     *
161     * @return array An associative array combining the folder names as key with
162     *               the corresponding annotation value.
163     */
164    public function listAnnotation($annotation)
165    {
166        $this->_timer->push();
167        $result = parent::listAnnotation($annotation);
168        $this->_logger->debug(
169            sprintf(
170                'REQUEST OUT IMAP: %s ms [listAnnotation]',
171                floor($this->_timer->pop() * 1000)
172            )
173        );
174        return $result;
175    }
176
177    /**
178     * Retrieve the namespace information for this connection.
179     *
180     * @return Horde_Kolab_Storage_Driver_Namespace The initialized namespace handler.
181     */
182    public function getNamespace()
183    {
184        $this->_timer->push();
185        $result = parent::getNamespace();
186        $this->_logger->debug(
187            sprintf(
188                'REQUEST OUT IMAP: %s ms [getNamespace]',
189                floor($this->_timer->pop() * 1000)
190            )
191        );
192        return $result;
193    }
194}