Completed
Push — master ( 1fe58d...1d38e8 )
by
unknown
07:43
created

SoundCloudProvider::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 2
1
<?php
2
3
namespace MediaMonks\SonataMediaBundle\Provider;
4
5
use MediaMonks\SonataMediaBundle\Entity\Media;
6
use MediaMonks\SonataMediaBundle\Model\MediaInterface;
7
use Sonata\AdminBundle\Form\FormMapper;
8
use Symfony\Component\Form\Extension\Core\Type\TextType;
9
use Symfony\Component\HttpFoundation\Response;
10
11
class SoundCloudProvider extends AbstractProvider implements ProviderInterface
12
{
13
    const URL_OEMBED = 'https://soundcloud.com/oembed?format=json&url=https://soundcloud.com/%s';
14
    const URL = 'https://soundcloud.com/%s';
15
16
    /**
17
     * @param FormMapper $formMapper
18
     */
19
    public function buildProviderCreateForm(FormMapper $formMapper)
20
    {
21
        $formMapper->add('providerReference', TextType::class, ['label' => 'SoundCloud URL']);
22
    }
23
24
    /**
25
     * @param FormMapper $formMapper
26
     */
27
    public function buildProviderEditForm(FormMapper $formMapper)
28
    {
29
        $formMapper->add('providerReference', TextType::class, ['label' => 'SoundCloud URL']);
30
    }
31
32
    /**
33
     * @param Media $media
34
     * @throws \Exception
35
     */
36
    public function update(Media $media)
37
    {
38
        $currentSoundCloudId = $media->getProviderReference();
39
        $media->setProviderReference($this->parseReference($media->getProviderReference()));
40
41
        if ($currentSoundCloudId !== $media->getProviderReference()) {
42
            $data = $this->getDataByReference($media->getProviderReference());
43
44
            $data['embedUrl'] = $this->extractEmbedUrl($data);
45
            $media->setProviderMetaData($data);
46
47
            if (empty($media->getTitle())) {
48
                $media->setTitle($data['title']);
49
            }
50
            if (empty($media->getDescription())) {
51
                $media->setDescription($data['description']);
52
            }
53
            if (empty($media->getAuthorName())) {
54
                $media->setAuthorName($data['author_name']);
55
            }
56
            if (empty($media->getImage())) {
57
                $this->setImage($media, $data['thumbnail_url']);
58
            }
59
        }
60
61
        parent::update($media);
62
    }
63
64
    /**
65
     * @param Media $media
66
     * @param $thumbnailUrl
67
     */
68
    public function setImage(Media $media, $thumbnailUrl)
69
    {
70
        $filename = sprintf('%s_%d.%s', sha1($media->getProviderReference()), time(), 'jpg');
71
        $this->getFilesystem()->write(
72
            $filename,
73
            file_get_contents($thumbnailUrl)
74
        );
75
        $media->setImage($filename);
76
    }
77
78
    /**
79
     * @param $reference
80
     * @return mixed
81
     * @throws \Exception
82
     */
83 View Code Duplication
    protected function getDataByReference($reference)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
    {
85
        $this->disableErrorHandler();
86
        $data = json_decode(file_get_contents(sprintf(self::URL_OEMBED, $reference)), true);
87
        $this->restoreErrorHandler();
88
89
        if (empty($data['title'])) {
90
            throw new \Exception(
91
                sprintf('Could not get data from SoundCloud for id "%s", is the name correct?', $reference)
92
            );
93
        }
94
95
        return $data;
96
    }
97
98
    /**
99
     * @param $value
100
     * @return string
101
     * @throws \Exception
102
     */
103
    protected function parseReference($value)
104
    {
105
        if (strpos($value, 'soundcloud.com')) {
106
            $url = parse_url($value);
107
108
            return trim($url['path'], '/');
109
        }
110
111
        return $value;
112
    }
113
114
    /**
115
     * @param array $data
116
     */
117
    protected function extractEmbedUrl(array $data)
118
    {
119
        preg_match('/src="(.*)"/', $data['html'], $matches);
120
        $url = $matches[1];
121
122
        $data = parse_url($url);
123
        parse_str($data['query'], $data);
124
125
        return $data['url'];
126
    }
127
128
    /**
129
     * @return string
130
     */
131
    public function getIcon()
132
    {
133
        return 'soundcloud';
134
    }
135
136
    /**
137
     * @return string
138
     */
139
    public function getTitle()
140
    {
141
        return 'SoundCloud';
142
    }
143
144
    public function getType()
145
    {
146
        return 'audio';
147
    }
148
149
    /**
150
     * @return string
151
     */
152
    public function getEmbedTemplate()
153
    {
154
        return 'MediaMonksSonataMediaBundle:Provider:soundcloud_embed.html.twig';
155
    }
156
157
    /**
158
     * @return bool
159
     */
160
    public function supportsDownload()
161
    {
162
        return false;
163
    }
164
165
    /**
166
     * @return bool
167
     */
168
    public function supportsEmbed()
169
    {
170
        return true;
171
    }
172
173
    /**
174
     * @return bool
175
     */
176
    public function supportsImage()
177
    {
178
        return true;
179
    }
180
}
181