Completed
Push — master ( 8cbf16...01cc9b )
by Sergii
04:56
created

FetchField::condition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 3
crap 1
1
<?php
2
/**
3
 * @author Sergii Bondarenko, <[email protected]>
4
 */
5
namespace Drupal\TqExtension\Utils\Database;
6
7
// Utils.
8
use Behat\DebugExtension\Debugger;
9
10
class FetchField
11
{
12
    use Debugger;
13
14
    /**
15
     * @var \SelectQuery
16
     */
17
    private $query;
18
19
    /**
20
     * FetchField constructor.
21
     *
22
     * @param string $table
23
     * @param string $field
24
     */
25 4
    public function __construct($table, $field)
26
    {
27 4
        $this->query = db_select($table);
28 4
        $this->query->fields($table, [$field]);
29 4
    }
30
31
    /**
32
     * @param string $field
33
     * @param string|array $value
34
     * @param string $operator
35
     *
36
     * @return $this
37
     */
38 4
    public function condition($field, $value = null, $operator = null)
39
    {
40 4
        $this->query->condition($field, $value, $operator);
41
42 4
        return $this;
43
    }
44
45
    /**
46
     * @return string
47
     */
48 4
    public function execute()
49
    {
50 4
        self::debug(
51 4
            ['SQL query is: %s'],
52 4
            [trim(str_replace("\n", ' ', $this->query))]
53 4
        );
54
55 4
        return $this->query->execute()->fetchField();
56
    }
57
}
58