Completed
Push — master ( aba493...5356ed )
by Ruud
315:38 queued 305:00
created

Helper/RemoteAudio/RemoteAudioHandler.php (4 issues)

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
     * @var string
16
     */
17
    private $soundcloudApiKey;
18
19
    /**
20
     * @var string
21
     */
22
    const CONTENT_TYPE = 'remote/audio';
23
24
    /**
25
     * @var string
26
     */
27
    const TYPE = 'audio';
28
29
    public function __construct($priority, $soundcloudApiKey)
30
    {
31
        parent::__construct($priority);
32
        $this->soundcloudApiKey = $soundcloudApiKey;
33
    }
34
35
    /**
36
     * @return string
37
     */
38
    public function getName()
39
    {
40
        return 'Remote Audio Handler';
41
    }
42
43
    /**
44
     * @return string
45
     */
46
    public function getType()
47
    {
48
        return RemoteAudioHandler::TYPE;
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    public function getFormType()
55
    {
56
        return RemoteAudioType::class;
57
    }
58
59
    /**
60
     * @return mixed
0 ignored issues
show
Consider making the return type a bit more specific; maybe use string.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
61
     */
62
    public function getSoundcloudApiKey()
63
    {
64
        return $this->soundcloudApiKey;
65
    }
66
67
    /**
68
     * @param mixed $object
69
     *
70
     * @return bool
71
     */
72 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...
73
    {
74
        if (
75
            (\is_string($object)) ||
76
            ($object instanceof Media && $object->getContentType() == RemoteAudioHandler::CONTENT_TYPE)
77
        ) {
78
            return true;
79
        }
80
81
        return false;
82
    }
83
84
    /**
85
     * @param Media $media
86
     *
87
     * @return RemoteAudioHelper
88
     */
89
    public function getFormHelper(Media $media)
90
    {
91
        return new RemoteAudioHelper($media);
92
    }
93
94
    /**
95
     * @param Media $media
96
     *
97
     * @throws \RuntimeException when the file does not exist
98
     */
99
    public function prepareMedia(Media $media)
100
    {
101
        if (null === $media->getUuid()) {
102
            $uuid = uniqid();
103
            $media->setUuid($uuid);
104
        }
105
        $audio = new RemoteAudioHelper($media);
106
        $code = $audio->getCode();
107
        //update thumbnail
108
        switch ($audio->getType()) {
109
            case 'soundcloud':
110
                $scData = json_decode(
111
                    file_get_contents(
112
                        'http://api.soundcloud.com/tracks/' . $code . '.json?client_id=' . $this->getSoundcloudApiKey()
113
                    )
114
                );
115
                $artworkUrl = $scData->artwork_url;
116
                $artworkUrl = str_replace('large.jpg', 't500x500.jpg', $artworkUrl);
117
                $audio->setThumbnailUrl($artworkUrl);
118
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
0 ignored issues
show
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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
0 ignored issues
show
Consider making the return type a bit more specific; maybe use array<*,array<string,string>>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
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