UpdateEntity   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 5
eloc 19
c 3
b 0
f 0
dl 0
loc 74
ccs 18
cts 18
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A set() 0 22 4
1
<?php
2
3
namespace Rougin\Windstorm\Mutators;
4
5
use Rougin\Windstorm\QueryInterface;
6
use Rougin\Windstorm\MutatorInterface;
7
8
/**
9
 * Update Entity Mutator
10
 *
11
 * @package Windstorm
12
 * @author  Rougin Gutib <[email protected]>
13
 */
14
class UpdateEntity implements MutatorInterface
15
{
16
    const UPDATED_AT = 'updated_at';
17
18
    /**
19
     * @var array
20
     */
21
    protected $data = array();
22
23
    /**
24
     * @var string
25
     */
26
    protected $dateFormat = 'Y-m-d H:i:s';
27
28
    /**
29
     * @var integer
30
     */
31
    protected $id;
32
33
    /**
34
     * @var string
35
     */
36
    protected $primary = 'id';
37
38
    /**
39
     * @var string
40
     */
41
    protected $table = '';
42
43
    /**
44
     * @var boolean
45
     */
46
    protected $timestamp = true;
47
48
    /**
49
     * Initializes the mutator instance.
50
     *
51
     * @param integer $id
52
     * @param array   $data
53
     */
54 9
    public function __construct($id, $data)
55
    {
56 9
        $this->id = $id;
57
58 9
        $this->data = $data;
59 9
    }
60
61
    /**
62
     * Mutates the specified query instance.
63
     *
64
     * @param \Rougin\Windstorm\QueryInterface $query
65
     */
66 9
    public function set(QueryInterface $query)
67
    {
68 9
        if ($this->timestamp)
69 6
        {
70 9
            $this->data[static::UPDATED_AT] = date($this->dateFormat);
71 6
        }
72
73 9
        $query = $query->update($this->table);
74
75 9
        foreach ($this->data as $key => $value)
76
        {
77 9
            if ($this->primary === $key)
78 6
            {
79 6
                continue;
80
            }
81
82 9
            $query = $query->set($key, $value);
83 6
        }
84
85 9
        $query = $query->where($this->primary);
86
87 9
        return $query->equals((integer) $this->id);
88
    }
89
}
90