UserRepository::findOneByEmail()   A
last analyzed

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 1
1
<?php
2
3
namespace JhUser\Repository;
4
5
use Doctrine\ORM\QueryBuilder;
6
use Doctrine\Common\Persistence\ObjectRepository;
7
use DoctrineORMModule\Paginator\Adapter\DoctrinePaginator;
8
use Doctrine\ORM\Tools\Pagination\Paginator as ORMPaginator;
9
use Zend\Paginator\Paginator;
10
use ZfcUser\Mapper\UserInterface;
11
12
/**
13
 * Class UserRepository
14
 * @package JhUser\Repository
15
 * @author Aydin Hassan <[email protected]>
16
 */
17
class UserRepository implements UserRepositoryInterface, ObjectRepository
18
{
19
20
    /**
21
     * @var \Doctrine\Common\Persistence\ObjectRepository
22
     */
23
    protected $userRepository;
24
25
    /**
26
     * @param ObjectRepository $userRepository
27
     */
28
    public function __construct(ObjectRepository $userRepository)
29
    {
30
        $this->userRepository = $userRepository;
31
    }
32
33
    /**
34
     * Return a paginator object using the query builder object
35
     *
36
     * @param \Doctrine\ORM\QueryBuilder $queryBuilder
37
     * @return \Zend\Paginator\Paginator
38
     */
39
    public function paginate(QueryBuilder $queryBuilder)
40
    {
41
        return new Paginator(
42
            new DoctrinePaginator(new ORMPaginator($queryBuilder))
43
        );
44
    }
45
46
    /**
47
     * findAll(): defined by ObjectRepository.
48
     *
49
     * @param bool $paginate
50
     * @see    ObjectRepository::findAll()
51
     * @return UserInterface[]
52
     */
53
    public function findAll($paginate = false)
54
    {
55
        $qb = $this->userRepository->createQueryBuilder('u');
56
57
        if ($paginate) {
58
            return $this->paginate($qb);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->paginate($qb); (Zend\Paginator\Paginator) is incompatible with the return type declared by the interface JhUser\Repository\UserRepositoryInterface::findAll of type JhUser\Entity\User[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
59
        }
60
        return $qb->getQuery()->getResult();
61
    }
62
63
    /**
64
     * @param string $email
65
     * @return UserInterface|null
66
     */
67
    public function findOneByEmail($email)
68
    {
69
        return $this->userRepository->findOneBy(['email' => $email]);
70
    }
71
72
    /**
73
     * find(): defined by ObjectRepository.
74
     *
75
     * @see    ObjectRepository::find()
76
     * @param  int $id
77
     * @return UserInterface|null
78
     */
79
    public function find($id)
80
    {
81
        return $this->userRepository->find($id);
82
    }
83
84
    /**
85
     * findBy(): defined by ObjectRepository.
86
     *
87
     * @see    ObjectRepository::findBy()
88
     * @param  array      $criteria
89
     * @param  array|null $orderBy
90
     * @param  int|null   $limit
91
     * @param  int|null   $offset
92
     * @return UserInterface[]
93
     */
94
    public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
95
    {
96
        return $this->userRepository->findBy($criteria, $orderBy, $limit, $offset);
97
    }
98
99
    /**
100
     * findOneBy(): defined by ObjectRepository.
101
     *
102
     * @see    ObjectRepository::findOneBy()
103
     * @param  array $criteria
104
     * @return UserInterface|null
105
     */
106
    public function findOneBy(array $criteria)
107
    {
108
        return $this->userRepository->findOneBy($criteria);
109
    }
110
111
    /**
112
     * getClassName(): defined by ObjectRepository.
113
     *
114
     * @see    ObjectRepository::getClassName()
115
     * @return string
116
     */
117
    public function getClassName()
118
    {
119
        return $this->userRepository->getClassName();
120
    }
121
}
122