Completed
Push — master ( c49bae...43ad9f )
by
unknown
09:25
created

MediaAdmin::isProviderReferenceUpdated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace MediaMonks\SonataMediaBundle\Admin;
4
5
use MediaMonks\SonataMediaBundle\Model\AbstractMedia;
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\DatagridMapper;
11
use Sonata\AdminBundle\Datagrid\ListMapper;
12
use Sonata\AdminBundle\Form\FormMapper;
13
use Sonata\AdminBundle\Route\RouteCollection;
14
use Sonata\CoreBundle\Validator\ErrorElement;
15
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)
45
    {
46 5
        parent::__construct($code, $class, $baseControllerName);
47
48 5
        $this->providerPool = $providerPool;
49 5
    }
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);
0 ignored issues
show
Compatibility introduced by
$media of type object<MediaMonks\Sonata...e\Model\MediaInterface> is not a sub-type of object<MediaMonks\Sonata...le\Model\AbstractMedia>. It seems like you assume a concrete implementation of the interface MediaMonks\SonataMediaBundle\Model\MediaInterface to be always present.

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.

Loading history...
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)
178
    {
179 1
        $collection->add('image', $this->getRouterIdParameter().'/image/{width}/{height}');
180 1
        $collection->add('download', $this->getRouterIdParameter().'/download');
181 1
    }
182
183
    /**
184
     * @param DatagridMapper $datagridMapper
185
     */
186 5
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
187
    {
188
        $datagridMapper
189 5
            ->add('title')
190 5
            ->add('type')
191 5
            ->add('provider')
192
        ;
193 5
    }
194
195
    /**
196
     * {@inheritdoc}
197
     */
198 5
    public function validate(ErrorElement $errorElement, $object)
199
    {
200 5
        $this->getProvider($object)->validate($errorElement, $object);
201 5
    }
202
}
203