Passed
Push — master ( f0e277...a14668 )
by Rougin
03:58
created

UpdateEntity   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 12
dl 0
loc 52
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A set() 0 12 2
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
    /**
17
     * @var array
18
     */
19
    protected $data = array();
20
21
    /**
22
     * @var integer
23
     */
24
    protected $id;
25
26
    /**
27
     * @var string
28
     */
29
    protected $primary = 'id';
30
31
    /**
32
     * @var string
33
     */
34
    protected $table = '';
35
36
    /**
37
     * Initializes the mutator instance.
38
     *
39
     * @param integer $id
40
     * @param array   $data
41
     */
42
    public function __construct($id, $data)
43
    {
44
        $this->id = $id;
45
46
        $this->data = $data;
47
    }
48
49
    /**
50
     * Mutates the specified query instance.
51
     *
52
     * @param \Rougin\Windstorm\QueryInterface $query
53
     */
54
    public function set(QueryInterface $query)
55
    {
56
        $query = $query->update($this->table);
57
58
        foreach ($this->data as $key => $value)
59
        {
60
            $query = $query->set($key, $value);
61
        }
62
63
        $query = $query->where($this->primary);
64
65
        return $query->equals((integer) $this->id);
66
    }
67
}
68