Completed
Push — master ( affb41...f007be )
by Rafał
09:54
created

UserManager   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 90
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A deleteUser() 0 5 1
A getClass() 0 9 2
A findUserBy() 0 4 1
A findUsers() 0 4 1
A updateUser() 0 7 2
A getRepository() 0 4 1
A find() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher User Bundle.
7
 *
8
 * Copyright 2021 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @Copyright 2021 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\UserBundle\Doctrine;
18
19
use Doctrine\Common\Persistence\ObjectManager;
20
use Doctrine\Common\Persistence\ObjectRepository;
21
use SWP\Bundle\UserBundle\Model\UserInterface;
22
use SWP\Bundle\UserBundle\Model\UserManager as BaseUserManager;
23
24
class UserManager extends BaseUserManager
25
{
26
    /**
27
     * @var ObjectManager
28
     */
29
    protected $objectManager;
30
31
    /**
32
     * @var string
33
     */
34
    private $class;
35
36
    /**
37
     * Constructor.
38
     *
39
     * @param string $class
40
     */
41
    public function __construct(
42
        ObjectManager $om,
43
        $class
44
    ) {
45
        $this->objectManager = $om;
46
        $this->class = $class;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function deleteUser(UserInterface $user)
53
    {
54
        $this->objectManager->remove($user);
55
        $this->objectManager->flush();
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function getClass()
62
    {
63
        if (false !== strpos($this->class, ':')) {
64
            $metadata = $this->objectManager->getClassMetadata($this->class);
65
            $this->class = $metadata->getName();
66
        }
67
68
        return $this->class;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function findUserBy(array $criteria)
75
    {
76
        return $this->getRepository()->findOneBy($criteria);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function findUsers()
83
    {
84
        return $this->getRepository()->findAll();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getRepository()->findAll(); (object[]) is incompatible with the return type declared by the interface SWP\Bundle\UserBundle\Mo...gerInterface::findUsers of type Traversable.

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...
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function updateUser(UserInterface $user, $andFlush = true)
91
    {
92
        $this->objectManager->persist($user);
93
        if ($andFlush) {
94
            $this->objectManager->flush();
95
        }
96
    }
97
98
    /**
99
     * @return ObjectRepository
100
     */
101
    protected function getRepository()
102
    {
103
        return $this->objectManager->getRepository($this->getClass());
104
    }
105
106
    /**
107
     * @return object|null
108
     */
109
    public function find(int $id)
110
    {
111
        return $this->getRepository()->find($id);
112
    }
113
}
114