1 | <?php |
||
16 | class MediaAdmin extends AbstractAdmin |
||
17 | { |
||
18 | /** |
||
19 | * @var ProviderPool |
||
20 | */ |
||
21 | private $providerPool; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | private $originalProviderReference; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $baseRouteName = 'admin_mediamonks_sonatamedia_media'; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $baseRoutePattern = 'mediamonks/sonatamedia/media'; |
||
37 | |||
38 | /** |
||
39 | * @param string $code |
||
40 | * @param string $class |
||
41 | * @param string $baseControllerName |
||
42 | * @param ProviderPool $providerPool |
||
43 | */ |
||
44 | 5 | public function __construct($code, $class, $baseControllerName, ProviderPool $providerPool) |
|
50 | |||
51 | /** |
||
52 | * @param ListMapper $listMapper |
||
53 | */ |
||
54 | 5 | protected function configureListFields(ListMapper $listMapper) |
|
55 | { |
||
56 | $listMapper |
||
57 | 5 | ->addIdentifier('title') |
|
58 | 5 | ->add( |
|
59 | 'type' |
||
60 | 5 | ) |
|
61 | 5 | ->add( |
|
62 | 5 | 'updatedAt', |
|
63 | 'datetime' |
||
64 | 5 | ) |
|
65 | 5 | ->add( |
|
66 | 5 | '_action', |
|
67 | 5 | 'actions', |
|
68 | [ |
||
69 | 'actions' => [ |
||
70 | 5 | 'edit' => [], |
|
71 | 5 | 'delete' => [], |
|
72 | 5 | ], |
|
73 | ] |
||
74 | 5 | ); |
|
75 | 5 | } |
|
76 | |||
77 | /** |
||
78 | * @param FormMapper $formMapper |
||
79 | */ |
||
80 | 5 | protected function configureFormFields(FormMapper $formMapper) |
|
81 | { |
||
82 | /** |
||
83 | * @var AbstractMedia $media |
||
84 | */ |
||
85 | 5 | $media = $this->getSubject(); |
|
86 | 5 | if (!$media) { |
|
87 | $media = $this->getNewInstance(); |
||
88 | } |
||
89 | |||
90 | 5 | $this->originalProviderReference = $media->getProviderReference(); |
|
91 | |||
92 | 5 | $provider = $this->getProvider($media); |
|
|
|||
93 | 5 | $provider->setMedia($media); |
|
94 | |||
95 | 5 | if ($media->getId()) { |
|
96 | 5 | $provider->buildEditForm($formMapper); |
|
97 | 5 | } else { |
|
98 | 5 | $provider->buildCreateForm($formMapper); |
|
99 | } |
||
100 | 5 | } |
|
101 | |||
102 | /** |
||
103 | * @param AbstractMedia $media |
||
104 | */ |
||
105 | 5 | public function prePersist($media) |
|
106 | { |
||
107 | 5 | $this->getProvider($media)->update($media, true); |
|
108 | 5 | } |
|
109 | |||
110 | /** |
||
111 | * @param AbstractMedia $media |
||
112 | */ |
||
113 | 3 | public function preUpdate($media) |
|
114 | { |
||
115 | 3 | $this->getProvider($media)->update($media, $this->isProviderReferenceUpdated($media)); |
|
116 | 3 | } |
|
117 | |||
118 | /** |
||
119 | * @param $media |
||
120 | * @return bool |
||
121 | */ |
||
122 | 3 | protected function isProviderReferenceUpdated(AbstractMedia $media) |
|
123 | { |
||
124 | 3 | return $this->originalProviderReference !== $media->getProviderReference(); |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * @param AbstractMedia $media |
||
129 | * @return ProviderInterface |
||
130 | */ |
||
131 | 5 | protected function getProvider(AbstractMedia $media) |
|
132 | { |
||
133 | 5 | if (empty($media->getProvider())) { |
|
134 | throw new \InvalidArgumentException('No provider was set'); |
||
135 | } |
||
136 | |||
137 | 5 | $provider = $this->providerPool->getProvider($media->getProvider()); |
|
138 | 5 | $media->setType($provider->getType()); |
|
139 | |||
140 | 5 | return $this->providerPool->getProvider($media->getProvider()); |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * @return MediaInterface |
||
145 | */ |
||
146 | 5 | public function getNewInstance() |
|
147 | { |
||
148 | 5 | $media = parent::getNewInstance(); |
|
149 | 5 | $providerName = null; |
|
150 | 5 | if ($this->hasRequest()) { |
|
151 | 5 | if ($this->getRequest()->isMethod('POST')) { |
|
152 | 5 | $providerName = $this->getRequest()->get($this->getUniqid())['provider']; |
|
153 | 5 | } elseif ($this->getRequest()->query->has('provider')) { |
|
154 | 5 | $providerName = $this->getRequest()->query->get('provider'); |
|
155 | 5 | } |
|
156 | 5 | } |
|
157 | |||
158 | 5 | if (!empty($providerName)) { |
|
159 | 5 | $media->setProvider($providerName); |
|
160 | 5 | } |
|
161 | |||
162 | 5 | return $media; |
|
163 | } |
||
164 | |||
165 | /** |
||
166 | * @param mixed $object |
||
167 | * @return string |
||
168 | */ |
||
169 | 5 | public function toString($object) |
|
170 | { |
||
171 | 5 | return $object instanceof MediaInterface ? $object->getTitle() : 'Media'; |
|
172 | } |
||
173 | |||
174 | /** |
||
175 | * @param RouteCollection $collection |
||
176 | */ |
||
177 | 1 | public function configureRoutes(RouteCollection $collection) |
|
182 | |||
183 | /** |
||
184 | * @param DatagridMapper $datagridMapper |
||
185 | */ |
||
186 | 5 | protected function configureDatagridFilters(DatagridMapper $datagridMapper) |
|
187 | { |
||
188 | $datagridMapper |
||
194 | |||
195 | /** |
||
196 | * {@inheritdoc} |
||
197 | */ |
||
198 | 5 | public function validate(ErrorElement $errorElement, $object) |
|
202 | } |
||
203 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.