Completed
Push — master ( e64970...96a08e )
by
unknown
03:53
created

MediaAdmin::configureRoutes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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
                null,
44
                [
45
                    'template' => 'MediaMonksSonataMediaBundle:MediaAdmin:list_type.html.twig',
46
                ]
47
            )
48
            ->add(
49
                'updatedAt',
50
                'datetime'
51
            )
52
            ->add(
53
                '_action',
54
                'actions',
55
                [
56
                    'actions' => [
57
                        'edit'   => [],
58
                        'delete' => [],
59
                    ],
60
                ]
61
            );
62
    }
63
64
    /**
65
     * @param FormMapper $formMapper
66
     */
67
    protected function configureFormFields(FormMapper $formMapper)
68
    {
69
        /**
70
         * @var Media $media
71
         */
72
        $media = $this->getSubject();
73
        if (!$media) {
74
            $media = $this->getNewInstance();
75
        }
76
77
        $provider = $this->getProvider($media);
78
79
        if ($media->getId()) {
80
            $provider->buildEditForm($formMapper);
81
        } else {
82
            $provider->buildCreateForm($formMapper);
83
        }
84
    }
85
86
    /**
87
     * @param MediaInterface $media
88
     */
89
    public function prePersist($media)
90
    {
91
        $this->getProvider($media)->prePersist($media);
92
    }
93
94
    /**
95
     * @param MediaInterface $media
96
     */
97
    public function preUpdate($media)
98
    {
99
        $this->getProvider($media)->preUpdate($media);
100
    }
101
102
    /**
103
     * @param $media
104
     * @return ProviderInterface
105
     */
106
    protected function getProvider(MediaInterface $media)
107
    {
108
        return $this->providerPool->getProvider($media->getProviderName());
109
    }
110
111
    /**
112
     * @return MediaInterface
113
     */
114
    public function getNewInstance()
115
    {
116
        $media = parent::getNewInstance();
117
        if ($this->hasRequest()) {
118
            if ($this->getRequest()->isMethod('POST')) {
119
                $media->setProviderName($this->getRequest()->get($this->getUniqid())['providerName']);
120
            } elseif ($this->getRequest()->query->has('provider')) {
121
                $media->setProviderName($this->getRequest()->query->get('provider'));
122
            } else {
123
                throw new \InvalidArgumentException('No provider was set');
124
            }
125
        }
126
127
        return $media;
128
    }
129
130
    /**
131
     * @param mixed $object
132
     * @return string
133
     */
134
    public function toString($object)
135
    {
136
        return $object instanceof MediaInterface ? $object->getTitle() : 'Media';
137
    }
138
139
    /**
140
     * @param RouteCollection $collection
141
     */
142
    public function configureRoutes(RouteCollection $collection)
143
    {
144
        $collection->add('image_redirect', $this->getRouterIdParameter().'/image/redirect');
145
    }
146
}
147