Completed
Push — master ( ac02e0...0e5803 )
by
unknown
04:21
created

AbstractOembedProvider::getReferenceName()   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
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
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 buildProviderEditForm(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(sprintf('%s reference "%s" seems to be incorrect', $this->getTitle(), $id));
125
            }
126
127
            $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...
128
        }
129
130
        return $this->oembedData;
131
    }
132
133
    /**
134
     * @return bool
135
     */
136
    public function supportsDownload()
137
    {
138
        return false;
139
    }
140
141
    /**
142
     * @return bool
143
     */
144
    public function supportsEmbed()
145
    {
146
        return true;
147
    }
148
149
    /**
150
     * @return bool
151
     */
152
    public function supportsImage()
153
    {
154
        return true;
155
    }
156
}
157