Completed
Push — master ( a36a2e...782846 )
by Sergii
05:45
created

FetchField::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
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
    public function __construct($table, $field)
26
    {
27
        $this->query = db_select($table);
28
        $this->query->fields($table, [$field]);
29
    }
30
31
    /**
32
     * @param string $field
33
     * @param string|array $value
34
     * @param string $operator
35
     *
36
     * @return $this
37
     */
38
    public function condition($field, $value = null, $operator = null)
39
    {
40
        $this->query->condition($field, $value, $operator);
41
42
        return $this;
43
    }
44
45
    /**
46
     * @return string
47
     */
48
    public function execute()
49
    {
50
        self::debug(
51
            ['SQL query is: %s'],
52
            [trim(str_replace("\n", ' ', $this->query))]
53
        );
54
55
        return $this->query->execute()->fetchField();
56
    }
57
}
58