Passed
Push — master ( f42cdc...9659cc )
by Rougin
04:09
created

UpdateEntity   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 57
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A set() 0 17 3
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 9
    public function __construct($id, $data)
43
    {
44 9
        $this->id = $id;
45
46 9
        $this->data = $data;
47 9
    }
48
49
    /**
50
     * Mutates the specified query instance.
51
     *
52
     * @param \Rougin\Windstorm\QueryInterface $query
53
     */
54 9
    public function set(QueryInterface $query)
55
    {
56 9
        $query = $query->update($this->table);
57
58 9
        foreach ($this->data as $key => $value)
59
        {
60 9
            if ($this->primary === $key)
61 6
            {
62 6
                continue;
63
            }
64
65 9
            $query = $query->set($key, $value);
66 6
        }
67
68 9
        $query = $query->where($this->primary);
69
70 9
        return $query->equals((integer) $this->id);
71
    }
72
}
73