Completed
Push — 0.3-dev ( 29e489...87cbea )
by Arnaud
03:16
created

DataProvider::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace LAG\AdminBundle\DataProvider;
4
5
use Doctrine\Common\Collections\Collection;
6
use Doctrine\Common\Persistence\ObjectRepository;
7
use LAG\DoctrineRepositoryBundle\Repository\RepositoryInterface;
8
9
/**
10
 * Default data provider using generic repositories
11
 */
12
class DataProvider implements DataProviderInterface
13
{
14
    /**
15
     * @var RepositoryInterface
16
     */
17
    protected $repository;
18
19
    /**
20
     * DataProvider constructor.
21
     *
22
     * @param ObjectRepository $repository
23
     */
24
    public function __construct(
25
        ObjectRepository $repository
26
    ) {
27
        $this->repository = $repository;
0 ignored issues
show
Documentation Bug introduced by
$repository is of type object<Doctrine\Common\P...tence\ObjectRepository>, but the property $repository was declared to be of type object<LAG\DoctrineRepos...ry\RepositoryInterface>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
28
    }
29
30
    /**
31
     * Find entities by criteria.
32
     *
33
     * @param array $criteria
34
     * @param array $orderBy
35
     * @param int|null $limit
36
     * @param int|null $offset
37
     * @return Collection
38
     */
39
    public function findBy(array $criteria = [], $orderBy = [], $limit = null, $offset = null)
40
    {
41
        return $this
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->repository->findB...erBy, $limit, $offset); of type array|Doctrine\Common\Collections\Collection adds the type array to the return on line 41 which is incompatible with the return type documented by LAG\AdminBundle\DataProvider\DataProvider::findBy of type Doctrine\Common\Collections\Collection.
Loading history...
42
            ->repository
43
            ->findBy($criteria, $orderBy, $limit, $offset);
44
    }
45
46
    /**
47
     * Find an entity by its unique id.
48
     *
49
     * @param $id
50
     * @return object
51
     */
52
    public function find($id)
53
    {
54
        return $this
55
            ->repository
56
            ->find($id);
57
    }
58
59
    /**
60
     * Save an entity.
61
     *
62
     * @param $entity
63
     */
64
    public function save($entity)
65
    {
66
        $this
67
            ->repository
68
            ->save($entity);
69
    }
70
71
    /**
72
     * Remove an entity.
73
     *
74
     * @param $entity
75
     */
76
    public function remove($entity)
77
    {
78
        $this
79
            ->repository
80
            ->delete($entity);
81
    }
82
83
    /**
84
     * Create a new entity.
85
     *
86
     * @return object
87
     */
88
    public function create()
89
    {
90
        $className = $this->repository->getClassName();
91
92
        return new $className;
93
    }
94
}
95