Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
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
     * @return RemoteAudioHelper
86
     */
87
    public function getFormHelper(Media $media)
88
    {
89
        return new RemoteAudioHelper($media);
90
    }
91
92
    /**
93
     * @throws \RuntimeException when the file does not exist
94
     */
95
    public function prepareMedia(Media $media)
96
    {
97
        if (null === $media->getUuid()) {
98
            $uuid = uniqid();
99
            $media->setUuid($uuid);
100
        }
101
        $audio = new RemoteAudioHelper($media);
102
        $code = $audio->getCode();
103
        //update thumbnail
104
        switch ($audio->getType()) {
105
            case 'soundcloud':
106
                $scData = json_decode(
107
                    file_get_contents(
108
                        'http://api.soundcloud.com/tracks/' . $code . '.json?client_id=' . $this->getSoundcloudApiKey()
109
                    )
110
                );
111
                $artworkUrl = $scData->artwork_url;
112
                $artworkUrl = str_replace('large.jpg', 't500x500.jpg', $artworkUrl);
113
                $audio->setThumbnailUrl($artworkUrl);
114
115
                break;
116
        }
117
    }
118
119
    public function saveMedia(Media $media)
120
    {
121
    }
122
123
    public function removeMedia(Media $media)
124
    {
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public function updateMedia(Media $media)
131
    {
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function createNew($data)
138
    {
139
        return null;
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function getShowTemplate(Media $media)
146
    {
147
        return '@KunstmaanMedia/Media/RemoteAudio/show.html.twig';
148
    }
149
150
    /**
151
     * @param Media  $media    The media entity
152
     * @param string $basepath The base path
153
     *
154
     * @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...
155
     */
156
    public function getImageUrl(Media $media, $basepath)
157
    {
158
        $helper = new RemoteAudioHelper($media);
159
160
        return $helper->getThumbnailUrl();
161
    }
162
163
    /**
164
     * @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...
165
     */
166
    public function getAddFolderActions()
167
    {
168
        return [
169
            RemoteAudioHandler::TYPE => [
170
                'type' => RemoteAudioHandler::TYPE,
171
                'name' => 'media.audio.add',
172
            ],
173
        ];
174
    }
175
}
176