Passed
Push — master ( c65fff...5f6e9c )
by Rougin
14:50
created

ReturnEntity::query()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Rougin\Windstorm\Mutators;
4
5
use Rougin\Windstorm\QueryInterface;
6
use Rougin\Windstorm\MutatorInterface;
7
8
/**
9
 * Return Entity Mutator
10
 *
11
 * @package Windstorm
12
 * @author  Rougin Gutib <[email protected]>
13
 */
14
class ReturnEntity implements MutatorInterface
15
{
16
    /**
17
     * @var string
18
     */
19
    protected $alias = '';
20
21
    /**
22
     * @var string[]
23
     */
24
    protected $fields = array('*');
25
26
    /**
27
     * @var integer
28
     */
29
    protected $id;
30
31
    /**
32
     * @var string
33
     */
34
    protected $primary = 'id';
35
36
    /**
37
     * @var string
38
     */
39
    protected $table = '';
40
41 6
    /**
42
     * Initializes the mutator instance.
43 6
     *
44 6
     * @param integer $id
45
     */
46
    public function __construct($id)
47
    {
48
        $this->id = $id;
49
    }
50
51 6
    /**
52
     * Mutates the specified query instance.
53 6
     *
54
     * @param \Rougin\Windstorm\QueryInterface $query
55 6
     */
56
    public function set(QueryInterface $query)
57
    {
58
        if (! $this->alias && $this->table)
59
        {
60
            $this->alias = $this->table[0];
61
        }
62
63
        $query = $this->query($query);
64
65
        return $query->where($this->primary)->equals($this->id);
66
    }
67
68
    /**
69
     * Sets a predefined query instance.
70
     *
71
     * @param  \Rougin\Windstorm\QueryInterface $query
72
     * @return \Rougin\Windstorm\QueryInterface
73
     */
74
    protected function query(QueryInterface $query)
75
    {
76
        return $query->select($this->fields)->from($this->table, $this->alias);
77
    }
78
}
79