Completed
Push — develop ( 725207...6c2664 )
by
unknown
16:33 queued 08:34
created

DraftableEntityAwareTrait::findBy()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 12
loc 12
rs 9.4285
c 1
b 0
f 1
cc 3
eloc 6
nc 3
nop 4
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2016 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Core\Repository;
12
13
use Core\Entity\EntityInterface;
14
15
/**
16
 * Enables an implementing repository to handle draftable entities.
17
 *
18
 * @method EntityInterface create(array $data = [], bool $persist = false)
19
 *
20
 * @author Mathias Gelhausen <[email protected]>
21
 * @since 0.26
22
 */
23
trait DraftableEntityAwareTrait
24
{
25 View Code Duplication
    public function findBy(array $criteria, array $sort = null, $limit = null, $skip = null)
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...
26
    {
27
        if (!array_key_exists('isDraft', $criteria)) {
28
            $criteria['isDraft'] = false;
29
        } elseif (null === $criteria['isDraft']) {
30
            unset($criteria['isDraft']);
31
        }
32
33
        /** @noinspection PhpUndefinedClassInspection */
34
        /** @noinspection PhpUndefinedMethodInspection */
35
        return parent::findBy($criteria, $sort, $limit, $skip);
36
    }
37
38
    /**
39
     * Find entities in draft mode.
40
     *
41
     * Sets the key 'isDraft' with the value true and proxies to parent::findBy()
42
     *
43
     * @param array $criteria
44
     * @param array|null $sort
45
     * @param null|int  $limit
46
     * @param null|int  $skip
47
     *
48
     * @return \MongoCursor
49
     */
50
    public function findDraftsBy(array $criteria, array $sort = null, $limit = null, $skip = null)
51
    {
52
        $criteria['isDraft'] = true;
53
54
        /** @noinspection PhpUndefinedClassInspection */
55
        /** @noinspection PhpUndefinedMethodInspection */
56
        return parent::findBy($criteria, $sort, $limit, $skip);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (findBy() instead of findDraftsBy()). Are you sure this is correct? If so, you might want to change this to $this->findBy().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
57
    }
58
59 View Code Duplication
    public function findOneBy(array $criteria)
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...
60
    {
61
        if (!array_key_exists('isDraft', $criteria)) {
62
            $criteria['isDraft'] = false;
63
        } elseif (null === $criteria['isDraft']) {
64
            unset($criteria['isDraft']);
65
        }
66
        /** @noinspection PhpUndefinedClassInspection */
67
        /** @noinspection PhpUndefinedMethodInspection */
68
        return parent::findOneBy($criteria);
69
    }
70
71
    /**
72
     * Finds one entity in draft mode.
73
     *
74
     * Sets the key 'isDraft' to true and proxies to parent::findOneBy()
75
     *
76
     * @param array $criteria
77
     *
78
     * @return \MongoCursor
79
     */
80
    public function findOneDraftBy(array $criteria)
81
    {
82
        $criteria['isDraft'] = true;
83
84
        /** @noinspection PhpUndefinedClassInspection */
85
        /** @noinspection PhpUndefinedMethodInspection */
86
        return parent::findOneBy($criteria);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (findOneBy() instead of findOneDraftBy()). Are you sure this is correct? If so, you might want to change this to $this->findOneBy().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
87
    }
88
89
    /**
90
     * Creates a query builder.
91
     *
92
     * Prepopulate the constraint for 'isDraft' according to $findDrafts:
93
     * - true: queryBuilder will return only drafts
94
     * - false: queryBuilder will return only non drafts.
95
     * - null: queryBuilder will return all entities matching the additional constraints.
96
     *
97
     * @param bool|null $findDrafts
98
     *
99
     * @return \Doctrine\MongoDB\Query\Builder
100
     */
101 View Code Duplication
    public function createQueryBuilder($findDrafts = false)
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...
102
    {
103
        /* @var \Doctrine\MongoDB\Query\Builder $qb */
104
        /** @noinspection PhpUndefinedClassInspection */
105
        /** @noinspection PhpUndefinedMethodInspection */
106
        $qb = parent::createQueryBuilder();
107
108
        if (null !== $findDrafts) {
109
            $qb->field('isDraft')->equals($findDrafts);
110
        }
111
        return $qb;
112
    }
113
114
    /**
115
     * Creates an entity in draft mode.
116
     *
117
     * @param array $data
0 ignored issues
show
Documentation introduced by
Should the type for parameter $data not be null|array?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
118
     * @param bool  $persist
119
     *
120
     * @return EntityInterface
121
     */
122
    public function createDraft(array $data = null, $persist = false)
123
    {
124
        $data['isDraft'] = true;
125
126
        return $this->create($data, $persist);
127
    }
128
}