Completed
Push — master ( 3d485f...407dff )
by Paweł
66:49
created

EntityRepositoryTrait::flush()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Storage Bundle.
5
 *
6
 * Copyright 2016 Sourcefabric z.ú. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2016 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\StorageBundle\Doctrine\ORM;
16
17
use Doctrine\ORM\QueryBuilder;
18
use Knp\Component\Pager\Paginator;
19
use Knp\Component\Pager\Pagination\PaginationInterface;
20
use SWP\Component\Common\Criteria\Criteria;
21
use SWP\Component\Common\Pagination\PaginationData;
22
use SWP\Component\Storage\Model\PersistableInterface;
23
24
trait EntityRepositoryTrait
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function persist(PersistableInterface $object)
30
    {
31
        $this->_em->persist($object);
0 ignored issues
show
Bug introduced by
The property _em does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function add(PersistableInterface $object)
38
    {
39
        $this->_em->persist($object);
40
        $this->_em->flush();
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function flush()
47
    {
48
        $this->_em->flush();
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 View Code Duplication
    public function remove(PersistableInterface $object)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56
        if (null !== $this->find($object->getId())) {
0 ignored issues
show
Bug introduced by
It seems like find() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
57
            $this->_em->remove($object);
58
            $this->_em->flush();
59
        }
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getPaginatedByCriteria(Criteria $criteria, array $sorting = [], PaginationData $paginationData = null)
66
    {
67
        $queryBuilder = $this->getQueryByCriteria($criteria, $sorting, 's');
68
69
        if (null === $paginationData) {
70
            $paginationData = new PaginationData();
71
        }
72
73
        return $this->getPaginator($queryBuilder, $paginationData);
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function getQueryByCriteria(Criteria $criteria, array $sorting, string $alias): QueryBuilder
80
    {
81
        $queryBuilder = $this->createQueryBuilder($alias);
0 ignored issues
show
Bug introduced by
It seems like createQueryBuilder() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
82
        $this->applyCriteria($queryBuilder, $criteria, $alias);
83
        $this->applySorting($queryBuilder, $sorting, $alias);
84
        $this->applyLimiting($queryBuilder, $criteria);
85
86
        return $queryBuilder;
87
    }
88
89
    /**
90
     * @param QueryBuilder $queryBuilder
91
     * @param Criteria     $criteria
92
     */
93
    public function applyLimiting(QueryBuilder $queryBuilder, Criteria $criteria)
94
    {
95
        $queryBuilder->setFirstResult($criteria->get('firstResult', 0));
96
        if ($criteria->has('maxResults')) {
97
            $queryBuilder->setMaxResults($criteria->get('maxResults'));
98
        } else {
99
            $queryBuilder->setMaxResults(self::MAX_RESULTS);
100
        }
101
    }
102
103
    /**
104
     * @param $queryBuilder
105
     * @param PaginationData $paginationData
106
     *
107
     * @return PaginationInterface
108
     */
109
    protected function getPaginator($queryBuilder, PaginationData $paginationData)
110
    {
111
        $paginator = new Paginator();
112
113
        return $paginator->paginate($queryBuilder, $paginationData->getPageNumber(), $paginationData->getLimit());
114
    }
115
116
    /**
117
     * @param QueryBuilder $queryBuilder
118
     * @param Criteria     $criteria
119
     * @param string       $alias
120
     */
121
    protected function applyCriteria(QueryBuilder $queryBuilder, Criteria $criteria, string $alias)
122
    {
123
        $properties = array_merge($this->getClassMetadata()->getFieldNames(), $this->getClassMetadata()->getAssociationNames());
0 ignored issues
show
Bug introduced by
It seems like getClassMetadata() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
124
        foreach ($criteria->all() as $property => $value) {
125
            if (!in_array($property, $properties)) {
126
                continue;
127
            }
128
129
            $name = $this->getPropertyName($property, $alias);
130
            if (null === $value) {
131
                $queryBuilder->andWhere($queryBuilder->expr()->isNull($name));
132
            } elseif (is_array($value)) {
133
                $queryBuilder->andWhere($queryBuilder->expr()->in($name, $value));
134
            } elseif ('' !== $value) {
135
                $parameter = str_replace('.', '_', $property);
136
                $queryBuilder
137
                    ->andWhere($queryBuilder->expr()->eq($name, ':'.$parameter))
138
                    ->setParameter($parameter, $value)
139
                ;
140
            }
141
        }
142
    }
143
144
    /**
145
     * @param QueryBuilder $queryBuilder
146
     * @param array        $sorting
147
     * @param string       $alias
148
     */
149
    protected function applySorting(QueryBuilder $queryBuilder, array $sorting, string $alias)
150
    {
151
        foreach ($sorting as $property => $order) {
152
            if (!empty($order)) {
153
                $queryBuilder->addOrderBy($this->getPropertyName($property, $alias), $order);
154
            }
155
        }
156
    }
157
158
    /**
159
     * @param string $name
160
     * @param string $alias
161
     *
162
     * @return string
163
     */
164
    protected function getPropertyName(string $name, string $alias)
165
    {
166
        if (false === strpos($name, '.')) {
167
            return $alias.'.'.$name;
168
        }
169
170
        return $name;
171
    }
172
}
173