Completed
Push — 2.0 ( d7d09e...57ce97 )
by Peter
06:52 queued 12s
created

Min::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 6
Ratio 100 %

Importance

Changes 0
Metric Value
dl 6
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of the Happyr Doctrine Specification package.
6
 *
7
 * (c) Tobias Nyholm <[email protected]>
8
 *     Kacper Gunia <[email protected]>
9
 *     Peter Gribanov <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Happyr\DoctrineSpecification\Operand\PlatformFunction;
16
17
use Doctrine\ORM\QueryBuilder;
18
use Happyr\DoctrineSpecification\Exception\OperandNotExecuteException;
19
use Happyr\DoctrineSpecification\Operand\ArgumentToOperandConverter;
20
use Happyr\DoctrineSpecification\Operand\Operand;
21
22 View Code Duplication
final class Min 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...
23
{
24
    /**
25
     * @var Operand|string
26
     */
27
    private $field;
28
29
    /**
30
     * @var bool
31
     */
32
    private $distinct;
33
34
    /**
35
     * @param Operand|string $field
36
     * @param bool           $distinct
37
     */
38
    public function __construct($field, bool $distinct = false)
39
    {
40
        $this->field = $field;
41
        $this->distinct = $distinct;
42
    }
43
44
    /**
45
     * @param QueryBuilder $qb
46
     * @param string       $context
47
     *
48
     * @return string
49
     */
50
    public function transform(QueryBuilder $qb, string $context): string
51
    {
52
        $field = ArgumentToOperandConverter::toField($this->field);
53
        $field = $field->transform($qb, $context);
54
55
        $expression = '';
56
57
        if ($this->distinct) {
58
            $expression = 'DISTINCT ';
59
        }
60
61
        return sprintf('MIN(%s%s)', $expression, $field);
62
    }
63
64
    /**
65
     * @param mixed[]|object $candidate
66
     */
67
    public function execute($candidate): void
68
    {
69
        throw new OperandNotExecuteException(
70
            sprintf('The operand "%s" cannot be executed for a single candidate.', self::class)
71
        );
72
    }
73
}
74