Completed
Push — master ( a29a95...dded20 )
by
unknown
03:05
created

BaseMediaAdmin::configureFormFields()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 19
nc 10
nop 1
dl 0
loc 34
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\MediaBundle\Admin;
13
14
use Sonata\AdminBundle\Admin\AbstractAdmin;
15
use Sonata\AdminBundle\Datagrid\ListMapper;
16
use Sonata\AdminBundle\Form\FormMapper;
17
use Sonata\CoreBundle\Model\Metadata;
18
use Sonata\MediaBundle\Form\DataTransformer\ProviderDataTransformer;
19
use Sonata\MediaBundle\Model\CategoryManagerInterface;
20
use Sonata\MediaBundle\Provider\Pool;
21
22
abstract class BaseMediaAdmin extends AbstractAdmin
23
{
24
    /**
25
     * @var Pool
26
     */
27
    protected $pool;
28
29
    /**
30
     * @var CategoryManagerInterface
31
     */
32
    protected $categoryManager;
33
34
    /**
35
     * @param string                   $code
36
     * @param string                   $class
37
     * @param string                   $baseControllerName
38
     * @param Pool                     $pool
39
     * @param CategoryManagerInterface $categoryManager
40
     */
41
    public function __construct($code, $class, $baseControllerName, Pool $pool, CategoryManagerInterface $categoryManager = null)
42
    {
43
        parent::__construct($code, $class, $baseControllerName);
44
45
        $this->pool = $pool;
46
47
        $this->categoryManager = $categoryManager;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function prePersist($media)
54
    {
55
        $parameters = $this->getPersistentParameters();
56
        $media->setContext($parameters['context']);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function getPersistentParameters()
63
    {
64
        $parameters = parent::getPersistentParameters();
65
66
        if (!$this->hasRequest()) {
67
            return $parameters;
68
        }
69
70
        $filter = $this->getRequest()->get('filter');
71
        if ($filter && array_key_exists('context', $this->getRequest()->get('filter'))) {
72
            $context = $filter['context']['value'];
73
        } else {
74
            $context = $this->getRequest()->get('context', $this->pool->getDefaultContext());
75
        }
76
77
        $providers = $this->pool->getProvidersByContext($context);
78
        $provider = $this->getRequest()->get('provider');
79
80
        // if the context has only one provider, set it into the request
81
        // so the intermediate provider selection is skipped
82
        if (count($providers) == 1 && null === $provider) {
83
            $provider = array_shift($providers)->getName();
84
            $this->getRequest()->query->set('provider', $provider);
85
        }
86
87
        $categoryId = $this->getRequest()->get('category');
88
89
        if (null !== $this->categoryManager && !$categoryId) {
90
            $categoryId = $this->categoryManager->getRootCategory($context)->getId();
0 ignored issues
show
Bug introduced by
The method getId() does not seem to exist on object<Sonata\Classifica...odel\CategoryInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
91
        }
92
93
        return array_merge($parameters, array(
94
            'context' => $context,
95
            'category' => $categoryId,
96
            'hide_context' => (bool) $this->getRequest()->get('hide_context'),
97
        ));
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function getNewInstance()
104
    {
105
        $media = parent::getNewInstance();
106
107
        if ($this->hasRequest()) {
108
            if ($this->getRequest()->isMethod('POST')) {
109
                $uniqid = $this->getUniqid();
110
                $providerParams = $this->getRequest()->get($uniqid);
111
                $media->setProviderName($providerParams['providerName']);
112
            } else {
113
                $media->setProviderName($this->getRequest()->get('provider'));
114
            }
115
116
            $media->setContext($context = $this->getRequest()->get('context'));
117
118
            if (null !== $this->categoryManager && $categoryId = $this->getPersistentParameter('category')) {
119
                $category = $this->categoryManager->find($categoryId);
120
121
                if ($category && $category->getContext()->getId() == $context) {
0 ignored issues
show
Bug introduced by
The method getContext cannot be called on $category (of type array<integer,object<Son...del\CategoryInterface>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
Bug Best Practice introduced by
The expression $category of type Sonata\ClassificationBun...del\CategoryInterface[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
122
                    $media->setCategory($category);
123
                }
124
            }
125
        }
126
127
        return $media;
128
    }
129
130
    /**
131
     * @return Pool
132
     */
133
    public function getPool()
134
    {
135
        return $this->pool;
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function getObjectMetadata($object)
142
    {
143
        $provider = $this->pool->getProvider($object->getProviderName());
144
145
        $url = $provider->generatePublicUrl($object, $provider->getFormatName($object, 'admin'));
146
147
        return new Metadata($object->getName(), $object->getDescription(), $url);
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153
    protected function configureListFields(ListMapper $listMapper)
154
    {
155
        $listMapper
156
            ->addIdentifier('name')
157
            ->add('description')
158
            ->add('enabled')
159
            ->add('size')
160
        ;
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166
    protected function configureFormFields(FormMapper $formMapper)
167
    {
168
        $media = $this->getSubject();
169
170
        if (!$media) {
171
            $media = $this->getNewInstance();
172
        }
173
174
        if (!$media || !$media->getProviderName()) {
175
            return;
176
        }
177
178
        $formMapper->add('providerName', 'Symfony\Component\Form\Extension\Core\Type\HiddenType');
179
180
        $formMapper->getFormBuilder()->addModelTransformer(new ProviderDataTransformer($this->pool, $this->getClass()), true);
181
182
        $provider = $this->pool->getProvider($media->getProviderName());
183
184
        if ($media->getId()) {
185
            $provider->buildEditForm($formMapper);
186
        } else {
187
            $provider->buildCreateForm($formMapper);
188
        }
189
190
        if (null !== $this->categoryManager) {
191
            $formMapper->add('category', 'Sonata\AdminBundle\Form\Type\ModelListType', array(), array(
192
                'link_parameters' => array(
193
                    'context' => $media->getContext(),
194
                    'hide_context' => true,
195
                    'mode' => 'tree',
196
                ),
197
            ));
198
        }
199
    }
200
}
201