|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DoS\ResourceBundle\Provider; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
6
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
|
7
|
|
|
use Sylius\Component\Resource\Model\ResourceInterface; |
|
8
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @author liverbool <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class AbstractProvider implements ProviderInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var ObjectManager |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $manager; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $dataClass; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var RepositoryInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $repository; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var FactoryInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $factory; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct( |
|
36
|
|
|
RepositoryInterface $repository, |
|
37
|
|
|
FactoryInterface $factory, |
|
38
|
|
|
ObjectManager $manager = null, |
|
39
|
|
|
$dataClass = null |
|
40
|
|
|
) { |
|
41
|
|
|
$this->repository = $repository; |
|
42
|
|
|
$this->factory = $factory; |
|
43
|
|
|
$this->manager = $manager ?: $repository->getManager(); |
|
|
|
|
|
|
44
|
|
|
$this->dataClass = $dataClass ?: $repository->getEntityName(); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return ResourceInterface |
|
49
|
|
|
*/ |
|
50
|
|
|
public function createNew() |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->factory->createNew(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param ResourceInterface $resource |
|
57
|
|
|
*/ |
|
58
|
|
|
public function persist(ResourceInterface $resource) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->manager->persist($resource); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param ResourceInterface|null $resource |
|
65
|
|
|
*/ |
|
66
|
|
|
public function flush(ResourceInterface $resource = null) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->manager->flush($resource); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param ResourceInterface $resource |
|
73
|
|
|
* @param bool $single |
|
74
|
|
|
*/ |
|
75
|
|
|
public function save(ResourceInterface $resource, $single = false) |
|
76
|
|
|
{ |
|
77
|
|
|
$this->manager->persist($resource); |
|
78
|
|
|
$this->manager->flush($single ? $resource : null); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param ResourceInterface $resource |
|
83
|
|
|
*/ |
|
84
|
|
|
public function detach(ResourceInterface $resource) |
|
85
|
|
|
{ |
|
86
|
|
|
$this->manager->detach($resource); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param ResourceInterface $resource |
|
91
|
|
|
*/ |
|
92
|
|
|
public function remove(ResourceInterface $resource) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->manager->remove($resource); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param null $objectName |
|
99
|
|
|
*/ |
|
100
|
|
|
public function clear($objectName = null) |
|
101
|
|
|
{ |
|
102
|
|
|
$this->manager->clear($objectName); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param $id |
|
107
|
|
|
* |
|
108
|
|
|
* @return ResourceInterface |
|
109
|
|
|
*/ |
|
110
|
|
|
public function find($id) |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->repository->find($id); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return ResourceInterface[] |
|
117
|
|
|
*/ |
|
118
|
|
|
public function findAll() |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->repository->findAll(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param array $criteria |
|
125
|
|
|
* @param array|null $orderBy |
|
126
|
|
|
* @param null $limit |
|
127
|
|
|
* @param null $offset |
|
128
|
|
|
* |
|
129
|
|
|
* @return ResourceInterface[] |
|
130
|
|
|
*/ |
|
131
|
|
|
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->repository->findBy($criteria, $orderBy, $limit, $offset); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param array $criteria |
|
138
|
|
|
* |
|
139
|
|
|
* @return ResourceInterface[] |
|
140
|
|
|
*/ |
|
141
|
|
|
public function findOneBy(array $criteria) |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->repository->findOneBy($criteria); |
|
|
|
|
|
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: