# Coverage for Doctrine_AuditLog_Listener

Back to coverage report

1 | <?php
2 | /*
3 | * $Id$
4 | *
5 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16 | *
17 | * This software consists of voluntary contributions made by many individuals
18 | * and is licensed under the LGPL. For more information, see
19 | * <http://www.phpdoctrine.org>.
20 | */
21 | Doctrine::autoload('Doctrine_Record_Listener');
22 | /**
23 | * Doctrine_AuditLog_Listener
24 | *
25 | * @package Doctrine
26 | * @subpackage AuditLog
27 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
28 | * @link www.phpdoctrine.org
29 | * @since 1.0
30 | * @version $Revision$
31 | * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
32 | */
33 | class Doctrine_AuditLog_Listener extends Doctrine_Record_Listener
34 | {
35 |
36 | protected $_auditLog;
37 |
38 | public function __construct(Doctrine_AuditLog $auditLog) {
39 | $this->_auditLog = $auditLog;
40 | }
41 | public function preInsert(Doctrine_Event $event)
42 | {
43 | $versionColumn = $this->_auditLog->getOption('versionColumn');
44 |
45 | $event->getInvoker()->set($versionColumn, 1);
46 | }
47 | public function postInsert(Doctrine_Event $event)
48 | {
49 | $class = $this->_auditLog->getOption('className');
50 |
51 | $record = $event->getInvoker();
52 |
53 | $version = new $class();
54 | $version->merge($record->toArray());
55 | $version->save();
56 | }
57 | public function preDelete(Doctrine_Event $event)
58 | {
59 | $class = $this->_auditLog->getOption('className');
60 |
61 | $record = $event->getInvoker();
62 |
63 | $versionColumn = $this->_auditLog->getOption('versionColumn');
64 | $version = $record->get($versionColumn);
65 |
66 | $record->set($versionColumn, ++$version);
67 |
68 | $version = new $class();
69 | $version->merge($record->toArray());
70 | $version->save();
71 | }
72 | public function preUpdate(Doctrine_Event $event)
73 | {
74 | $class = $this->_auditLog->getOption('className');
75 | $record = $event->getInvoker();
76 |
77 | $versionColumn = $this->_auditLog->getOption('versionColumn');
78 |
79 | $version = $record->get($versionColumn);
80 |
81 | $record->set($versionColumn, ++$version);
82 |
83 | $version = new $class();
84 | $version->merge($record->toArray());
85 | $version->save();
86 | }
87 | }