| 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\Form\Type\ImmutableArrayType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\Form\Extension\Core\Type\CheckboxType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Form\Extension\Core\Type\TextType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class SoundCloudProvider extends AbstractOembedProvider 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 buildProviderEditFormBefore(FormMapper $formMapper) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $formMapper->add('providerReference', TextType::class, ['label' => $this->getReferenceLabel()]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @param FormMapper $formMapper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public function buildProviderEditFormAfter(FormMapper $formMapper) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $formMapper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             ->tab('Embed Options') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             ->add( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                 'providerMetaData', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                 ImmutableArrayType::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                 [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                     'keys' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                         ['autoPlay', CheckboxType::class, ['label' => 'form.auto_play', 'required' => false]], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                         ['hideRelated', CheckboxType::class, ['label' => 'form.hide_related', 'required' => false]], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                         ['showComments', CheckboxType::class, ['label' => 'form.show_comments', 'required' => false]], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                         ['showUser', CheckboxType::class, ['label' => 'form.show_user', 'required' => false]], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                         ['showReposts', CheckboxType::class, ['label' => 'form.show_reposts', 'required' => false]], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                         ['showVisual', CheckboxType::class, ['label' => 'form.show_visual', 'required' => false]], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                     ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                     'label' => 'form.embed_options', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                     'required' => false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                 ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             )->end() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param string $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     public function getOembedUrl($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         return sprintf(self::URL_OEMBED, $id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @param $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @throws \Exception | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     public function parseProviderReference($value) | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         if (strpos($value, 'soundcloud.com')) { | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |             $url = parse_url($value); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |             return trim($url['path'], '/'); | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |         return $value; | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @param $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     protected function getOembedData($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $data = parent::getOembedData($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $data['embedUrl'] = $this->extractEmbedUrl($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         return $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @param array $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     protected function extractEmbedUrl(array $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         preg_match('/src="(.*)"/', $data['html'], $matches); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $url = $matches[1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $data = parse_url($url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         parse_str($data['query'], $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         return $data['url']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     public function getIcon() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         return 'fa fa-soundcloud'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     public function getName() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         return 'soundcloud'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     public function getType() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         return AbstractProvider::TYPE_AUDIO; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 124 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 125 |  |  |  |