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(); |
|
|
|
|
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
|
|
|
|
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:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.