Completed
Push — master ( 1cc057...1c0904 )
by
unknown
03:48
created

buildProviderEditFormBefore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 2
1
<?php
2
3
namespace MediaMonks\SonataMediaBundle\Provider;
4
5
use MediaMonks\SonataMediaBundle\Entity\Media;
6
use Sonata\AdminBundle\Form\FormMapper;
7
use Sonata\CoreBundle\Validator\ErrorElement;
8
use Symfony\Component\Form\Extension\Core\Type\TextType;
9
10
abstract class AbstractOembedProvider extends AbstractProvider implements OembedProviderInterface
11
{
12
    /**
13
     * @var array
14
     */
15
    protected $oembedData;
16
17
    /**
18
     * @param Media $media
19
     * @param bool $providerReferenceUpdated
20
     * @throws \Exception
21
     */
22
    public function update(Media $media, $providerReferenceUpdated)
23
    {
24
        if ($providerReferenceUpdated) {
25
            $media->setProviderReference($this->parseProviderReference($media->getProviderReference()));
26
27
            $data = $this->getOembedData($media->getProviderReference());
28
29
            $media->setProviderMetaData($data);
30
31
            if (empty($media->getTitle()) && isset($data['title'])) {
32
                $media->setTitle($data['title']);
33
            }
34
            if (empty($media->getDescription()) && isset($data['description'])) {
35
                $media->setDescription($data['description']);
36
            }
37
            if (empty($media->getAuthorName()) && isset($data['author_name'])) {
38
                $media->setAuthorName($data['author_name']);
39
            }
40
            if (empty($media->getImage())) {
41
                $this->refreshImage($media);
42
            }
43
        }
44
45
        parent::update($media, $providerReferenceUpdated);
46
    }
47
48
    /**
49
     * @param FormMapper $formMapper
50
     */
51
    public function buildProviderCreateForm(FormMapper $formMapper)
52
    {
53
        $formMapper->add(
54
            'providerReference',
55
            TextType::class,
56
            ['label' => $this->getReferenceLabel()]
57
        );
58
    }
59
60
    /**
61
     * @param FormMapper $formMapper
62
     */
63
    public function buildProviderEditFormBefore(FormMapper $formMapper)
64
    {
65
        $formMapper->add(
66
            'providerReference',
67
            TextType::class,
68
            ['label' => $this->getReferenceLabel()]
69
        );
70
    }
71
72
    /**
73
     * @param ErrorElement $errorElement
74
     * @param Media $media
75
     */
76
    public function validate(ErrorElement $errorElement, Media $media)
77
    {
78
        try {
79
            $this->getOembedData($this->parseProviderReference($media->getProviderReference()));
80
        }
81
        catch (\Exception $e) {
82
            $errorElement->with('providerReference')->addViolation($e->getMessage());
83
        }
84
    }
85
86
    /**
87
     * @param \MediaMonks\SonataMediaBundle\Entity\Media $media
88
     */
89
    public function refreshImage(Media $media)
90
    {
91
        $filename = sprintf('%s_%d.%s', sha1($media->getProviderReference()), time(), 'jpg');
92
        $thumbnailUrl = $this->getImageUrl($media->getProviderReference());
93
94
        $this->getFilesystem()->write(
95
            $filename,
96
            file_get_contents($thumbnailUrl)
97
        );
98
        $media->setImage($filename);
99
    }
100
101
    /**
102
     * @param string $id
103
     * @return string
104
     */
105
    public function getImageUrl($id)
106
    {
107
        return $this->getOembedData($id)['thumbnail_url'];
108
    }
109
110
    /**
111
     * @param $id
112
     * @return mixed
113
     * @throws \Exception
114
     */
115
    protected function getOembedData($id)
116
    {
117
        if (empty($this->oembedData)) {
118
119
            $this->disableErrorHandler();
120
            $data = json_decode(file_get_contents($this->getOembedUrl($id)), true);
121
            $this->restoreErrorHandler();
122
123
            if (empty($data['title'])) {
124
                throw new \Exception($this->getTranslator()->trans('error.provider_reference', [
125
                    '%provider%' => $this->getName(),
126
                    '%reference%' => $id
127
                ]));
128
            }
129
130
            $this->oembedData = $data;
0 ignored issues
show
Documentation Bug introduced by
It seems like $data of type * is incompatible with the declared type array of property $oembedData.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
131
        }
132
133
        return $this->oembedData;
134
    }
135
136
    /**
137
     * @return string
138
     */
139
    public function getReferenceLabel()
140
    {
141
        return sprintf('form.%s.reference', $this->getName());
142
    }
143
144
    /**
145
     * @return bool
146
     */
147
    public function supportsDownload()
148
    {
149
        return false;
150
    }
151
152
    /**
153
     * @return bool
154
     */
155
    public function supportsEmbed()
156
    {
157
        return true;
158
    }
159
160
    /**
161
     * @return bool
162
     */
163
    public function supportsImage()
164
    {
165
        return true;
166
    }
167
}
168