1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MediaMonks\SonataMediaBundle\Admin; |
4
|
|
|
|
5
|
|
|
use MediaMonks\SonataMediaBundle\Entity\Media; |
6
|
|
|
use MediaMonks\SonataMediaBundle\Model\MediaInterface; |
7
|
|
|
use MediaMonks\SonataMediaBundle\Provider\ProviderPool; |
8
|
|
|
use MediaMonks\SonataMediaBundle\Provider\ProviderInterface; |
9
|
|
|
use Sonata\AdminBundle\Admin\AbstractAdmin; |
10
|
|
|
use Sonata\AdminBundle\Datagrid\ListMapper; |
11
|
|
|
use Sonata\AdminBundle\Form\FormMapper; |
12
|
|
|
use Sonata\AdminBundle\Route\RouteCollection; |
13
|
|
|
|
14
|
|
|
class MediaAdmin extends AbstractAdmin |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var ProviderPool |
18
|
|
|
*/ |
19
|
|
|
private $providerPool; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param string $code |
23
|
|
|
* @param string $class |
24
|
|
|
* @param string $baseControllerName |
25
|
|
|
* @param ProviderPool $providerPool |
26
|
|
|
*/ |
27
|
|
|
public function __construct($code, $class, $baseControllerName, ProviderPool $providerPool) |
28
|
|
|
{ |
29
|
|
|
parent::__construct($code, $class, $baseControllerName); |
30
|
|
|
|
31
|
|
|
$this->providerPool = $providerPool; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param ListMapper $listMapper |
36
|
|
|
*/ |
37
|
|
|
protected function configureListFields(ListMapper $listMapper) |
38
|
|
|
{ |
39
|
|
|
$listMapper |
40
|
|
|
->addIdentifier('title') |
41
|
|
|
->add( |
42
|
|
|
'type' |
43
|
|
|
) |
44
|
|
|
->add( |
45
|
|
|
'updatedAt', |
46
|
|
|
'datetime' |
47
|
|
|
) |
48
|
|
|
->add( |
49
|
|
|
'_action', |
50
|
|
|
'actions', |
51
|
|
|
[ |
52
|
|
|
'actions' => [ |
53
|
|
|
'edit' => [], |
54
|
|
|
'delete' => [], |
55
|
|
|
], |
56
|
|
|
] |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param FormMapper $formMapper |
62
|
|
|
*/ |
63
|
|
|
protected function configureFormFields(FormMapper $formMapper) |
64
|
|
|
{ |
65
|
|
|
/** |
66
|
|
|
* @var Media $media |
67
|
|
|
*/ |
68
|
|
|
$media = $this->getSubject(); |
69
|
|
|
if (!$media) { |
70
|
|
|
$media = $this->getNewInstance(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$provider = $this->getProvider($media); |
74
|
|
|
|
75
|
|
|
if ($media->getId()) { |
76
|
|
|
$provider->buildEditForm($formMapper); |
77
|
|
|
} else { |
78
|
|
|
$provider->buildCreateForm($formMapper); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param MediaInterface $media |
84
|
|
|
*/ |
85
|
|
|
public function prePersist($media) |
86
|
|
|
{ |
87
|
|
|
$this->getProvider($media)->prePersist($media); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param MediaInterface $media |
92
|
|
|
*/ |
93
|
|
|
public function preUpdate($media) |
94
|
|
|
{ |
95
|
|
|
$this->getProvider($media)->preUpdate($media); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param $media |
100
|
|
|
* @return ProviderInterface |
101
|
|
|
*/ |
102
|
|
|
protected function getProvider(MediaInterface $media) |
103
|
|
|
{ |
104
|
|
|
return $this->providerPool->getProvider($media->getProvider()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return MediaInterface |
109
|
|
|
*/ |
110
|
|
|
public function getNewInstance() |
111
|
|
|
{ |
112
|
|
|
$media = parent::getNewInstance(); |
113
|
|
|
if ($this->hasRequest()) { |
114
|
|
|
if ($this->getRequest()->isMethod('POST')) { |
115
|
|
|
$media->setProvider($this->getRequest()->get($this->getUniqid())['provider']); |
116
|
|
|
} elseif ($this->getRequest()->query->has('provider')) { |
117
|
|
|
$media->setProvider($this->getRequest()->query->get('provider')); |
118
|
|
|
} else { |
119
|
|
|
throw new \InvalidArgumentException('No provider was set'); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$provider = $this->getProvider($media); |
124
|
|
|
$media->setType($provider->getType()); |
125
|
|
|
|
126
|
|
|
return $media; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param mixed $object |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
public function toString($object) |
134
|
|
|
{ |
135
|
|
|
return $object instanceof MediaInterface ? $object->getTitle() : 'Media'; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param RouteCollection $collection |
140
|
|
|
*/ |
141
|
|
|
public function configureRoutes(RouteCollection $collection) |
142
|
|
|
{ |
143
|
|
|
$collection->add('image_redirect', $this->getRouterIdParameter().'/image/redirect'); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
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.