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

UpdateEntity::set()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 17
ccs 10
cts 10
cp 1
crap 3
rs 10
c 0
b 0
f 0
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