http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 6 LOC: 168 Statements: 46

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


Report generated at 2008-01-20T12:13:39-05:00