IdentityMapper   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 71
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSignature() 0 8 2
A fetch() 0 17 3
A clear() 0 6 1
1
<?php
2
/*
3
 * This file is part of the PommProject/ModelManager package.
4
 *
5
 * (c) 2014 - 2015 Grégoire HUBERT <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PommProject\ModelManager\Model;
11
12
use PommProject\ModelManager\Model\FlexibleEntity\FlexibleEntityInterface;
13
14
/**
15
 * IdentityMapper
16
 *
17
 * Cache for FlexibleEntityInterface instances to ensure there are no different
18
 * instances for the same data.
19
 *
20
 * @package   ModelManager
21
 * @copyright 2014 - 2015 Grégoire HUBERT
22
 * @author    Grégoire HUBERT
23
 * @license   X11 {@link http://opensource.org/licenses/mit-license.php}
24
 */
25
class IdentityMapper
26
{
27
    /**
28
     * @var FlexibleEntityInterface[]
29
     */
30
    protected $instances = [];
31
32
    /**
33
     * getSignature
34
     *
35
     * Compute a unique signature upon entity's values in its primary key. If
36
     * an empty primary key is provided, null is returned.
37
     *
38
     * @static
39
     * @access public
40
     * @param  FlexibleEntityInterface  $entity
41
     * @param  array                    $primary_key
42
     * @return string
43
     */
44
    public static function getSignature(FlexibleEntityInterface $entity, array $primary_key)
45
    {
46
        if (count($primary_key) === 0) {
47
            return null;
48
        }
49
50
        return sha1(sprintf("%s|%s", serialize($entity->fields($primary_key)), get_class($entity)));
51
    }
52
53
    /**
54
     * fetch
55
     *
56
     * Pool FlexibleEntityInterface instances and update them if necessary.
57
     *
58
     * @access public
59
     * @param  FlexibleEntityInterface  $entity
60
     * @param  array                    $primary_key
61
     * @return FlexibleEntityInterface
62
     */
63
    public function fetch(FlexibleEntityInterface $entity, array $primary_key)
64
    {
65
        $signature = self::getSignature($entity, $primary_key);
66
67
        if ($signature === null) {
68
            return $entity;
69
        }
70
71
        if (!array_key_exists($signature, $this->instances)) {
72
            $this->instances[$signature] = $entity;
73
            $entity->status(FlexibleEntityInterface::STATUS_EXIST);
74
        } else {
75
            $this->instances[$signature]->hydrate($entity->fields());
76
        }
77
78
        return $this->instances[$signature];
79
    }
80
81
    /**
82
     * clear
83
     *
84
     * Flush instances from the identity mapper.
85
     *
86
     * @access public
87
     * @return IdentityMapper $this
88
     */
89
    public function clear()
90
    {
91
        $this->instances = [];
92
93
        return $this;
94
    }
95
}
96