http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 6 LOC: 173 Statements: 46

Source file Statements Methods Total coverage
WorkUnit.php 91.3% 100.0% 92.3%
   
1
<?php
2
/**
3
 * Xyster Framework
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to the new BSD license that is bundled
8
 * with this package in the file LICENSE.txt.
9
 * It is also available through the world-wide-web at this URL:
10
 * http://www.opensource.org/licenses/bsd-license.php
11
 * If you did not receive a copy of the license and are unable to
12
 * obtain it through the world-wide-web, please send an email
13
 * to xyster@devweblog.org so we can send you a copy immediately.
14
 *
15
 * @category  Xyster
16
 * @package   Xyster_Orm
17
 * @copyright Copyright (c) 2007 Irrational Logic (http://devweblog.org)
18
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
19
 * @version   $Id: WorkUnit.php 98 2007-09-20 21:38:36Z doublecompile $
20
 */
21
/**
22
 * @see Xyster_Collection_Set
23
 */
24 1
require_once 'Xyster/Collection/Set.php';
25
/**
26
 * A transactional unit of work
27
 *
28
 * Fowler describes a unit of work as "[Maintaining] a list of objects affected
29
 * by a business transaction and [coordinating] the writing out of changes and
30
 * the resolution of concurrency problems."
31
 * {@link http://martinfowler.com/eaaCatalog/unitOfWork.html}
32
 *
33
 * @category  Xyster
34
 * @package   Xyster_Orm
35
 * @copyright Copyright (c) 2007 Irrational Logic (http://devweblog.org)
36
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
37
 */
38
class Xyster_Orm_WorkUnit
39
{
40
    /**
41
     * Entities that are in queue for update
42
     *
43
     * @var Xyster_Collection_Set
44
     */
45
    protected $_dirty;
46
47
    /**
48
     * Entities that are in queue for insertion
49
     *
50
     * @var Xyster_Collection_Set
51
     */
52
    protected $_new;
53
54
    /**
55
     * Entities that are in queue for removal
56
     *
57
     * @var Xyster_Collection_Set
58
     */
59
    protected $_removed;
60
61
    /**
62
     * Creates a new Xyster_Orm_WorkUnit object
63
     *
64
     */
65
    public function __construct()
66
    {
67 10
        $this->_new = new Xyster_Collection_Set();
68 10
        $this->_dirty = new Xyster_Collection_Set();
69 10
        $this->_removed = new Xyster_Collection_Set();
70
    }
71
72
    /**
73
     * Execute pending transactions
74
     *
75
     * @param Xyster_Orm_Manager $manager The session manager
76
     */
77
    public function commit( Xyster_Orm_Manager $manager )
78
    {
79 3
        $repo = $manager->getRepository();
80 3
        $mapFactory = $manager->getMapperFactory();
81
82
        /*
83
	     * Save all new entities
84
	     */
85 3
		foreach( $this->_new as $v ) {
86 2
			$mapFactory->get(get_class($v))->save($v);
87 2
			$repo->add($v);
88 2
		}
89 3
		$this->_new->clear();
90
91
		/*
92
		 * Update any existing entities
93
		 */
94 3
		foreach( $this->_dirty as $v ) {
95 3
			$mapFactory->get(get_class($v))->save($v);
96 3
			$manager->putInSecondaryCache($v);
97 3
		}
98 3
		$this->_dirty->clear();
99
100
		/*
101
		 * Delete entities to be removed
102
		 */
103 3
		foreach( $this->_removed as $v ) {
104 2
			$mapFactory->get(get_class($v))->delete($v);
105 2
			$repo->remove($v);
106 2
		}
107 3
		$this->_removed->clear();
108
    }
109
110
    /**
111
     * Register an entity as in queue for insertion
112
     *
113
     * @param Xyster_Orm_Entity $entity The entity to persist
114
     * @throws Xyster_Orm_Exception if the entity is already registered
115
     */
116
    public function registerNew( Xyster_Orm_Entity $entity )
117
    {
118 4
        if ( $this->_dirty->contains($entity) ||
119 4
            $this->_removed->contains($entity) ||
120 4
            $this->_new->contains($entity) ) {
121 1
            require_once 'Xyster/Orm/Exception.php';
122 1
            throw new Xyster_Orm_Exception('This entity is already registered');
123 0
        }
124 4
        $this->_new->add($entity);
125
    }
126
127
    /**
128
     * Register an entity as dirty
129
     *
130
     * @param Xyster_Orm_Entity $entity The entity to update
131
     * @throws Xyster_Orm_Exception if the entity can't be set as dirty
132
     */
133
    public function registerDirty( Xyster_Orm_Entity $entity )
134
    {
135 9
        $key = $entity->getPrimaryKey();
136 9
        if ( array_keys($key, null, true) == array_keys($key) ) {
137 1
            require_once 'Xyster/Orm/Exception.php';
138 1
            throw new Xyster_Orm_Exception('This entity cannot be saved, it must first be persisted');
139 0
        }
140 8
        if ( $this->_removed->contains($entity) ) {
141 1
            require_once 'Xyster/Orm/Exception.php';
142 1
            throw new Xyster_Orm_Exception('This entity cannot be updated because it is in queue for removal');
143 0
        }
144 7
        $this->_dirty->add($entity);
145
    }
146
147
    /**
148
     * Register an entity as in queue for removal
149
     *
150
     * @param Xyster_Orm_Entity $entity The entity to remove
151
     */
152
    public function registerRemoved( Xyster_Orm_Entity $entity )
153
    {
154 5
        if ( $this->_new->remove($entity) )  {
155 1
            return;
156 0
        }
157 5
        $this->_dirty->remove($entity);
158 5
        $this->_removed->add($entity);
159
    }
160
161
    /**
162
     * Cancel any pending changes
163
     *
164
     * Any entity objects still in memory will remain in a modified state, but
165
     * will not be committed to the data store.
166
     */
167
    public function rollback()
168
    {
169 4
        $this->_new->clear();
170 4
        $this->_removed->clear();
171 4
        $this->_dirty->clear();
172
    }
173
}


Report generated at 2007-11-05T09:09:01-05:00