Completed
Push — 1.0 ( ea6409...9b1c59 )
by Peter
08:58
created

Bitwise::transform()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Happyr\DoctrineSpecification\Operand;
4
5
use Doctrine\ORM\QueryBuilder;
6
use Happyr\DoctrineSpecification\Exception\InvalidArgumentException;
7
8 View Code Duplication
abstract class Bitwise implements Operand
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
9
{
10
    const B_AND = '&';
11
12
    const B_OR = '|';
13
14
    const B_XOR = '^';
15
16
    const B_LS = '<<';
17
18
    const B_RS = '>>';
19
20
    /**
21
     * @var string[]
22
     */
23
    private static $operations = array(
24
        self::B_AND,
25
        self::B_OR,
26
        self::B_XOR,
27
        self::B_LS,
28
        self::B_RS,
29
    );
30
31
    /**
32
     * @var string
33
     */
34
    private $operation = '';
35
36
    /**
37
     * @var Operand|string
38
     */
39
    private $field;
40
41
    /**
42
     * @var Operand|string
43
     */
44
    private $value;
45
46
    /**
47
     * @param string         $operation
48
     * @param Operand|string $field
49
     * @param Operand|string $value
50
     */
51
    public function __construct($operation, $field, $value)
52
    {
53
        if (!in_array($operation, self::$operations)) {
54
            throw new InvalidArgumentException(sprintf(
55
                '"%s" is not a valid bitwise operation. Valid operations are: "%s"',
56
                $operation,
57
                implode(', ', self::$operations)
58
            ));
59
        }
60
61
        $this->operation = $operation;
62
        $this->field = $field;
63
        $this->value = $value;
64
    }
65
66
    /**
67
     * @param QueryBuilder $qb
68
     * @param string       $dqlAlias
69
     *
70
     * @return string
71
     */
72
    public function transform(QueryBuilder $qb, $dqlAlias)
73
    {
74
        $field = ArgumentToOperandConverter::toField($this->field);
75
        $value = ArgumentToOperandConverter::toValue($this->value);
76
77
        $field = $field->transform($qb, $dqlAlias);
78
        $value = $value->transform($qb, $dqlAlias);
79
80
        return sprintf('(%s %s %s)', $field, $this->operation, $value);
81
    }
82
}
83