Completed
Push — master ( 588e78...191551 )
by Peter
09:07 queued 11s
created

BaseSpecification   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 69
c 0
b 0
f 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getFilter() 0 9 2
A modify() 0 7 2
A getSpec() 0 6 1
A getAlias() 0 8 2
1
<?php
2
3
/**
4
 * This file is part of the Happyr Doctrine Specification package.
5
 *
6
 * (c) Tobias Nyholm <[email protected]>
7
 *     Kacper Gunia <[email protected]>
8
 *     Peter Gribanov <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Happyr\DoctrineSpecification\Specification;
15
16
use Doctrine\ORM\QueryBuilder;
17
use Happyr\DoctrineSpecification\Filter\Filter;
18
use Happyr\DoctrineSpecification\Query\QueryModifier;
19
20
/**
21
 * Extend this abstract class if you want to build a new spec with your domain logic.
22
 */
23
abstract class BaseSpecification implements Specification
24
{
25
    /**
26
     * @var string|null
27
     */
28
    private $dqlAlias;
29
30
    /**
31
     * @param string|null $dqlAlias
32
     */
33
    public function __construct($dqlAlias = null)
34
    {
35
        $this->dqlAlias = $dqlAlias;
36
    }
37
38
    /**
39
     * @param QueryBuilder $qb
40
     * @param string       $dqlAlias
41
     *
42
     * @return string
43
     */
44
    public function getFilter(QueryBuilder $qb, $dqlAlias)
45
    {
46
        $spec = $this->getSpec();
47
        if ($spec instanceof Filter) {
48
            return $spec->getFilter($qb, $this->getAlias($dqlAlias));
49
        }
50
51
        return '';
52
    }
53
54
    /**
55
     * @param QueryBuilder $qb
56
     * @param string       $dqlAlias
57
     */
58
    public function modify(QueryBuilder $qb, $dqlAlias)
59
    {
60
        $spec = $this->getSpec();
61
        if ($spec instanceof QueryModifier) {
62
            $spec->modify($qb, $this->getAlias($dqlAlias));
63
        }
64
    }
65
66
    /**
67
     * Return all the specifications.
68
     *
69
     * @return Filter|QueryModifier|null
70
     */
71
    protected function getSpec()
72
    {
73
        @trigger_error('Using the default implementation of '.__METHOD__.' method is deprecated since version 1.1 and this method will be marked as abstract in 2.0. You must overwrite this implementation.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
74
75
        return null;
76
    }
77
78
    /**
79
     * @param string $dqlAlias
80
     *
81
     * @return string
82
     */
83
    private function getAlias($dqlAlias)
84
    {
85
        if (null !== $this->dqlAlias) {
86
            return $this->dqlAlias;
87
        }
88
89
        return $dqlAlias;
90
    }
91
}
92