Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
created

Helper/RemoteAudio/RemoteAudioHandler.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\MediaBundle\Helper\RemoteAudio;
4
5
use Kunstmaan\MediaBundle\Entity\Media;
6
use Kunstmaan\MediaBundle\Form\RemoteAudio\RemoteAudioType;
7
use Kunstmaan\MediaBundle\Helper\Media\AbstractMediaHandler;
8
9
/**
10
 * RemoteAudioStrategy
11
 */
12
class RemoteAudioHandler extends AbstractMediaHandler
13
{
14
15
    /**
16
     * @var string
17
     */
18
    private $soundcloudApiKey;
19
20
    /**
21
     * @var string
22
     */
23
    const CONTENT_TYPE = 'remote/audio';
24
25
    /**
26
     * @var string
27
     */
28
    const TYPE = 'audio';
29
30
    public function __construct($priority, $soundcloudApiKey)
31
    {
32
        parent::__construct($priority);
33
        $this->soundcloudApiKey = $soundcloudApiKey;
34
    }
35
36
    /**
37
     * @return string
38
     */
39
    public function getName()
40
    {
41
        return 'Remote Audio Handler';
42
    }
43
44
    /**
45
     * @return string
46
     */
47
    public function getType()
48
    {
49
        return RemoteAudioHandler::TYPE;
50
    }
51
52
    /**
53
     * @return string
54
     */
55
    public function getFormType()
56
    {
57
        return RemoteAudioType::class;
58
    }
59
60
    /**
61
     * @return mixed
62
     */
63
    public function getSoundcloudApiKey()
64
    {
65
        return $this->soundcloudApiKey;
66
    }
67
68
    /**
69
     * @param mixed $object
70
     *
71
     * @return bool
72
     */
73 View Code Duplication
    public function canHandle($object)
0 ignored issues
show
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...
74
    {
75
        if (
76
            (is_string($object)) ||
77
            ($object instanceof Media && $object->getContentType() == RemoteAudioHandler::CONTENT_TYPE)
78
        ) {
79
            return true;
80
        }
81
82
        return false;
83
    }
84
85
    /**
86
     * @param Media $media
87
     *
88
     * @return RemoteAudioHelper
89
     */
90
    public function getFormHelper(Media $media)
91
    {
92
        return new RemoteAudioHelper($media);
93
    }
94
95
    /**
96
     * @param Media $media
97
     *
98
     * @throws \RuntimeException when the file does not exist
99
     */
100
    public function prepareMedia(Media $media)
101
    {
102
        if (null === $media->getUuid()) {
103
            $uuid = uniqid();
104
            $media->setUuid($uuid);
105
        }
106
        $audio = new RemoteAudioHelper($media);
107
        $code  = $audio->getCode();
108
        //update thumbnail
109
        switch ($audio->getType()) {
110
            case 'soundcloud':
111
                $scData     = json_decode(
112
                    file_get_contents(
113
                        'http://api.soundcloud.com/tracks/' . $code . '.json?client_id=' . $this->getSoundcloudApiKey()
114
                    )
115
                );
116
                $artworkUrl = $scData->artwork_url;
117
                $artworkUrl = str_replace('large.jpg', 't500x500.jpg', $artworkUrl);
118
                $audio->setThumbnailUrl($artworkUrl);
119
                break;
120
        }
121
    }
122
123
    /**
124
     * @param Media $media
125
     */
126
    public function saveMedia(Media $media)
127
    {
128
    }
129
130
    /**
131
     * @param Media $media
132
     */
133
    public function removeMedia(Media $media)
134
    {
135
    }
136
137
    /**
138
     * {@inheritDoc}
139
     */
140
    public function updateMedia(Media $media)
141
    {
142
    }
143
144
    /**
145
     * {@inheritDoc}
146
     */
147
    public function createNew($data)
148
    {
149
        return null;
150
    }
151
152
    /**
153
     * {@inheritDoc}
154
     */
155
    public function getShowTemplate(Media $media)
156
    {
157
        return 'KunstmaanMediaBundle:Media\RemoteAudio:show.html.twig';
158
    }
159
160
    /**
161
     * @param Media  $media    The media entity
162
     * @param string $basepath The base path
163
     *
164
     * @return string
165
     */
166
    public function getImageUrl(Media $media, $basepath)
167
    {
168
        $helper = new RemoteAudioHelper($media);
169
170
        return $helper->getThumbnailUrl();
171
    }
172
173
    /**
174
     * @return array
175
     */
176
    public function getAddFolderActions()
177
    {
178
        return array(
179
            RemoteAudioHandler::TYPE => array(
180
                'type' => RemoteAudioHandler::TYPE,
181
                'name' => 'media.audio.add'
182
            )
183
        );
184
    }
185
}
186