Completed
Pull Request — master (#90)
by Arnaud
02:26
created

DataProvider::remove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
namespace LAG\AdminBundle\DataProvider;
4
5
use Doctrine\Common\Collections\Collection;
6
use Doctrine\ORM\EntityRepository;
7
use LAG\AdminBundle\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 RepositoryInterface $repository
23
     */
24 6
    public function __construct(RepositoryInterface $repository)
25
    {
26 6
        $this->repository = $repository;
27 6
    }
28
    
29
    /**
30
     * Find entities by criteria.
31
     *
32
     * @param array    $criteria
33
     * @param array    $orderBy
34
     * @param int|null $limit
35
     * @param int|null $offset
36
     *
37
     * @return array|Collection
38
     */
39 1
    public function findBy(array $criteria = [], $orderBy = [], $limit = null, $offset = null)
40
    {
41 1
        if ($this->repository instanceof EntityRepository) {
42
            $queryBuilder = $this
43
                ->repository
44
                ->createQueryBuilder('entity')
45
            ;
46
    
47
            foreach ($criteria as $criterion => $value) {
48
                $comparison = ' = ';
49
    
50
                if ($this->isWildCardValue($value)) {
51
                    $comparison = ' like ';
52
                }
53
                $parameter = ':'.$criterion;
54
                
55
                $queryBuilder
56
                    ->andWhere('entity.'.$criterion.$comparison.$parameter)
57
                    ->setParameter($parameter, $value)
58
                ;
59
            }
60
    
61
            foreach ($orderBy as $sort => $order) {
62
                $queryBuilder
63
                    ->addOrderBy('entity.'.$sort, $order)
64
                ;
65
            }
66
            $entities = $queryBuilder
67
                ->setFirstResult($offset)
68
                ->setMaxResults($limit)
69
                ->getQuery()
70
                ->getResult()
71
            ;
72
        } else {
73
            $entities = $this
74 1
                ->repository
75 1
                ->findBy($criteria, $orderBy, $limit, $offset)
76
            ;
77
        }
78
    
79 1
        return $entities;
80
    }
81
82
    /**
83
     * Find an entity by its unique id.
84
     *
85
     * @param $id
86
     * @return object
87
     */
88 1
    public function find($id)
89
    {
90
        return $this
91 1
            ->repository
92 1
            ->find($id)
93
        ;
94
    }
95
96
    /**
97
     * Save an entity.
98
     *
99
     * @param $entity
100
     */
101 1
    public function save($entity)
102
    {
103
        $this
104 1
            ->repository
105 1
            ->save($entity)
106
        ;
107 1
    }
108
109
    /**
110
     * Remove an entity.
111
     *
112
     * @param $entity
113
     */
114 1
    public function remove($entity)
115
    {
116
        $this
117 1
            ->repository
118 1
            ->delete($entity)
119
        ;
120 1
    }
121
122
    /**
123
     * Create a new entity.
124
     *
125
     * @return object
126
     */
127 1
    public function create()
128
    {
129
        $className = $this
130 1
            ->repository
131 1
            ->getClassName()
132
        ;
133
134 1
        return new $className;
135
    }
136
    
137
    /**
138
     * Return the number of entities in the Repository.
139
     *
140
     * @param array $criteria
141
     * @param array $options
142
     *
143
     * @return int
144
     */
145
    public function count(array $criteria = [], array $options = [])
146
    {
147
        return $this
148
            ->repository
149
            ->count($criteria, $options)
150
        ;
151
    }
152
    
153
    private function isWildCardValue($value)
154
    {
155
        if ('%' !== substr($value, 0, 1)) {
156
            return false;
157
        }
158
        
159
        if ('%' !== substr($value, -1, 1)) {
160
            return false;
161
        }
162
        
163
        return true;
164
    }
165
}
166