Completed
Branch feature/rework (8c4d59)
by Pavel
07:53
created

DataProvider::equalDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace Pfilsx\DataGrid\Grid\Providers;
5
6
7
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
8
use Doctrine\ORM\EntityManager;
9
use Doctrine\ORM\EntityRepository;
10
use Doctrine\ORM\QueryBuilder;
11
use Pfilsx\DataGrid\DataGridException;
12
use Pfilsx\DataGrid\Grid\Pager;
13
use Symfony\Component\Lock\Exception\NotSupportedException;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Lock\E...n\NotSupportedException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
abstract class DataProvider implements DataProviderInterface
16
{
17
    protected $pager;
18
19
    protected $countFieldName;
20
21
    /**
22
     * @internal
23
     * @return Pager
24
     */
25
    public function getPager(): Pager
26
    {
27
        return $this->pager;
28
    }
29
30
    /**
31
     * @internal
32
     * @param Pager $pager
33
     */
34
    public function setPager(Pager $pager): void
35
    {
36
        $this->pager = $pager;
37
    }
38
39
    public function setCountFieldName(string $name): DataProviderInterface
40
    {
41
        $this->countFieldName = $name;
42
        return $this;
43
    }
44
45
    public function getCountFieldName(): string
46
    {
47
        return $this->countFieldName;
48
    }
49
50
    public function addEqualFilter(string $attribute, $value): DataProviderInterface
51
    {
52
        throw new NotSupportedException("Method addEqualFilter() is not supported in " . static::class);
53
    }
54
55
    public function addLikeFilter(string $attribute, $value): DataProviderInterface
56
    {
57
        throw new NotSupportedException("Method addLikeFilter() is not supported in " . static::class);
58
    }
59
60
    public function addRelationFilter(string $attribute, $value, string $relationClass): DataProviderInterface
61
    {
62
        throw new NotSupportedException("Method addRelationFilter() is not supported in " . static::class);
63
    }
64
65
    public function addCustomFilter(string $attribute, $value, callable $callback): DataProviderInterface
66
    {
67
        throw new NotSupportedException("Method addCustomFilter() is not supported in " . static::class);
68
    }
69
70
    public function addDateFilter(string $attribute, $value, string $comparison = 'equal'): DataProviderInterface
71
    {
72
        $comparisonFunc = lcfirst($comparison) . 'Date';
73
        if (method_exists($this, $comparisonFunc)) {
74
            $this->$comparisonFunc($attribute, $value);
75
        } else {
76
            $this->equalDate($attribute, $value);
77
        }
78
        return $this;
79
    }
80
81
    protected function equalDate($attribute, $value): void
82
    {
83
        throw new NotSupportedException("Method equalDate() is not supported in " . static::class);
84
    }
85
86
87
    public static function create($data, EntityManager $doctrine = null): DataProviderInterface
88
    {
89
        if ($data instanceof EntityRepository) {
90
            return new RepositoryDataProvider($data, $doctrine);
0 ignored issues
show
Bug introduced by
It seems like $doctrine can also be of type null; however, parameter $manager of Pfilsx\DataGrid\Grid\Pro...Provider::__construct() does only seem to accept Doctrine\ORM\EntityManager, maybe add an additional type check? ( Ignorable by Annotation )

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

90
            return new RepositoryDataProvider($data, /** @scrutinizer ignore-type */ $doctrine);
Loading history...
91
        }
92
        if ($data instanceof QueryBuilder) {
93
            return new QueryBuilderDataProvider($data, $doctrine);
0 ignored issues
show
Bug introduced by
It seems like $doctrine can also be of type null; however, parameter $manager of Pfilsx\DataGrid\Grid\Pro...Provider::__construct() does only seem to accept Doctrine\ORM\EntityManager, maybe add an additional type check? ( Ignorable by Annotation )

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

93
            return new QueryBuilderDataProvider($data, /** @scrutinizer ignore-type */ $doctrine);
Loading history...
94
        }
95
        if (is_array($data)) {
96
            return new ArrayDataProvider($data);
97
        }
98
        throw new DataGridException('Provided data must be one of: ' . implode(', ', [
99
                ServiceEntityRepository::class,
100
                QueryBuilder::class,
101
                'Array'
102
            ]) . ', ' . (($type = gettype($data)) == 'object' ? get_class($data) : $type) . ' given');
103
    }
104
}