Passed
Pull Request — master (#1)
by Alex
02:14
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
     * @param MetadataInterface $metadata
32
     * @param array             $criteria
33
     * @param string            $key
34
     *
35
     * @return string
36
     *
37
     * @throws InvalidArgumentException
38
     */
39
    protected function resolveFieldName(MetadataInterface $metadata, array $criteria, string $key = 'field'): string
40
    {
41
        if (empty($criteria[$key])) {
42
            throw new InvalidArgumentException(
43
                sprintf(
44
                    'The required \'%s\' criteria value is missing for filter \'%s\'',
45
                    $key,
46
                    static::class
47
                )
48
            );
49
        }
50
51
        if (!$metadata->hasField($criteria[$key]) && !$metadata->hasAssociation($criteria[$key])) {
52
            throw new InvalidArgumentException(
53
                sprintf(
54
                    'Unable to apply query filter \'%s\': '
55
                    . 'The entity class \'%s\' has no field or association named \'%s\'',
56
                    static::class,
57
                    $metadata->getName(),
58
                    $criteria[$key]
59
                )
60
            );
61
        }
62
63
        return $criteria[$key];
64
    }
65
66
    /**
67
     * @param MetadataInterface $metadata
68
     * @param string            $fieldName
69
     * @param mixed             $value
70
     * @param string|null       $format
71
     *
72
     * @return mixed
73
     *
74
     * @noinspection PhpUnusedParameterInspection
75
     */
76
    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

76
    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

76
    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

76
    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...
77
    {
78
        return $value;
79
    }
80
}
81