forked from ddeboer/DdeboerSalesforceMapperBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnitOfWork.php
54 lines (43 loc) · 1.5 KB
/
UnitOfWork.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
namespace Ddeboer\Salesforce\MapperBundle;
use Ddeboer\Salesforce\MapperBundle\Annotation\AnnotationReader;
use Ddeboer\Salesforce\MapperBundle\Model\AbstractModel;
use Ddeboer\Salesforce\MapperBundle\Model\NonCacheableModel;
class UnitOfWork
{
protected $mapper;
protected $annotationReader;
protected $identityMap = array();
public function __construct(Mapper $mapper, AnnotationReader $annotationReader)
{
$this->mapper = $mapper;
$this->annotationReader = $annotationReader;
}
public function find($modelClass, $id)
{
$sObjectName = $this->getObjectName($modelClass);
if (isset($this->identityMap[$sObjectName][$id])) {
return $this->identityMap[$sObjectName][$id];
}
}
public function addToIdentityMap($model)
{
if ($model instanceof NonCacheableModel) {
return;
}
$this->identityMap[$this->getObjectName($model)][$model->getId()] = $model;
}
public function removeFromIdentityMap(AbstractModel $model)
{
$sObjectName = $this->getObjectName($model);
$id = $model->getId();
if (isset($this->identityMap[$sObjectName][$id])) {
unset($this->identityMap[$sObjectName][$id]);
}
}
protected function getObjectName($model)
{
$description = $this->mapper->getObjectDescription($model);
return (is_object($model) ? get_class($model) : $model) . "-" . $description->getName();
}
}