Passed
Push — master ( 676de8...b86364 )
by Alex
57s queued 12s
created

AbstractFilter::resolveValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 12
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\DoctrineQueryFilter\Filter;
6
7
use Arp\DoctrineQueryFilter\Filter\Exception\InvalidArgumentException;
8
use Arp\DoctrineQueryFilter\Metadata\MetadataInterface;
9
use Arp\DoctrineQueryFilter\QueryFilterManager;
10
11
/**
12
 * @author  Alex Patterson <[email protected]>
13
 * @package Arp\DoctrineQueryFilter\Filter
14
 */
15
abstract class AbstractFilter implements FilterInterface
16
{
17
    /**
18
     * @var QueryFilterManager
19
     */
20
    protected QueryFilterManager $queryFilterManager;
21
22
    /**
23
     * @param QueryFilterManager $queryFilterManager
24
     */
25
    public function __construct(QueryFilterManager $queryFilterManager)
26
    {
27
        $this->queryFilterManager = $queryFilterManager;
28
    }
29
30
    /**
31
     * Create a new unique parameter name
32
     *
33
     * @param string $prefix
34
     *
35
     * @return string
36
     */
37
    protected function createParamName(string $prefix = ''): string
38
    {
39
        return uniqid($prefix, true);
40
    }
41
42
    /**
43
     * @param MetadataInterface $metadata
44
     * @param array             $criteria
45
     * @param string            $key
46
     *
47
     * @return string
48
     *
49
     * @throws InvalidArgumentException
50
     */
51
    protected function resolveFieldName(MetadataInterface $metadata, array $criteria, string $key = 'field'): string
52
    {
53
        if (empty($criteria[$key])) {
54
            throw new InvalidArgumentException(
55
                sprintf(
56
                    'The required \'%s\' criteria value is missing for filter \'%s\'',
57
                    $key,
58
                    static::class
59
                )
60
            );
61
        }
62
63
        if (!$metadata->hasField($criteria[$key]) && !$metadata->hasAssociation($criteria[$key])) {
64
            throw new InvalidArgumentException(
65
                sprintf(
66
                    'Unable to apply query filter \'%s\': '
67
                    . 'The entity class \'%s\' has no field or association named \'%s\'',
68
                    static::class,
69
                    $metadata->getName(),
70
                    $criteria[$key]
71
                )
72
            );
73
        }
74
75
        return $criteria[$key];
76
    }
77
78
    /**
79
     * @param MetadataInterface $metadata
80
     * @param string            $fieldName
81
     * @param mixed             $value
82
     * @param string|null       $format
83
     *
84
     * @return mixed
85
     *
86
     * @noinspection PhpUnusedParameterInspection
87
     */
88
    protected function formatValue(MetadataInterface $metadata, string $fieldName, $value, ?string $format = null)
0 ignored issues
show
Unused Code introduced by
The parameter $format is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

88
    protected function formatValue(MetadataInterface $metadata, string $fieldName, $value, /** @scrutinizer ignore-unused */ ?string $format = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $metadata is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

88
    protected function formatValue(/** @scrutinizer ignore-unused */ MetadataInterface $metadata, string $fieldName, $value, ?string $format = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $fieldName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

88
    protected function formatValue(MetadataInterface $metadata, /** @scrutinizer ignore-unused */ string $fieldName, $value, ?string $format = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
89
    {
90
        return $value;
91
    }
92
}
93