|  | @@ -0,0 +1,157 @@
 | 
	
		
			
			|  | 1 | +<?php
 | 
	
		
			
			|  | 2 | +
 | 
	
		
			
			|  | 3 | +namespace Muzich\CoreBundle\Factory\Elements;
 | 
	
		
			
			|  | 4 | +
 | 
	
		
			
			|  | 5 | +use Muzich\CoreBundle\Factory\ElementFactory;
 | 
	
		
			
			|  | 6 | +use Muzich\CoreBundle\Entity\Element;
 | 
	
		
			
			|  | 7 | +
 | 
	
		
			
			|  | 8 | +/**
 | 
	
		
			
			|  | 9 | + * 
 | 
	
		
			
			|  | 10 | + *
 | 
	
		
			
			|  | 11 | + * @author bux
 | 
	
		
			
			|  | 12 | + */
 | 
	
		
			
			|  | 13 | +class Spotifycom extends ElementFactory
 | 
	
		
			
			|  | 14 | +{
 | 
	
		
			
			|  | 15 | +  
 | 
	
		
			
			|  | 16 | +  /**
 | 
	
		
			
			|  | 17 | +   * 
 | 
	
		
			
			|  | 18 | +    track http url: http://open.spotify.com/track/7ylMdCOkqumPAwIMb6j2D5
 | 
	
		
			
			|  | 19 | +    track spotify uri: spotify:track:7ylMdCOkqumPAwIMb6j2D5
 | 
	
		
			
			|  | 20 | +
 | 
	
		
			
			|  | 21 | +    album hhtp url: http://open.spotify.com/album/1VAB3Xn92dPKPWzocgQqkh
 | 
	
		
			
			|  | 22 | +    album spotify uri: spotify:album:1VAB3Xn92dPKPWzocgQqkh
 | 
	
		
			
			|  | 23 | +
 | 
	
		
			
			|  | 24 | +    playlist: open.spotify.com/user/bux/playlist/2kNeCiQaATbUi3lixhNwco
 | 
	
		
			
			|  | 25 | +   */
 | 
	
		
			
			|  | 26 | +  public function retrieveDatas()
 | 
	
		
			
			|  | 27 | +  {
 | 
	
		
			
			|  | 28 | +    $url_clean = $this->getCleanedUrl(false, 'open.spotify.com');
 | 
	
		
			
			|  | 29 | +        
 | 
	
		
			
			|  | 30 | +    $type   = null;
 | 
	
		
			
			|  | 31 | +    $ref_id = null;
 | 
	
		
			
			|  | 32 | +    $get_data_from_api = false;
 | 
	
		
			
			|  | 33 | +    if (preg_match("#^\/track\/([a-zA-Z0-9]+)#", $url_clean, $chaines))
 | 
	
		
			
			|  | 34 | +    {
 | 
	
		
			
			|  | 35 | +      $type   = 'track';
 | 
	
		
			
			|  | 36 | +      $ref_id = $chaines[1];
 | 
	
		
			
			|  | 37 | +      $get_data_from_api = true;
 | 
	
		
			
			|  | 38 | +    }
 | 
	
		
			
			|  | 39 | +    if (preg_match("#^\/album\/([a-zA-Z0-9]+)#", $url_clean, $chaines))
 | 
	
		
			
			|  | 40 | +    {
 | 
	
		
			
			|  | 41 | +      $type   = 'album';
 | 
	
		
			
			|  | 42 | +      $ref_id = $chaines[1];
 | 
	
		
			
			|  | 43 | +      $get_data_from_api = true;
 | 
	
		
			
			|  | 44 | +    }
 | 
	
		
			
			|  | 45 | +    if (preg_match("#^\/user\/([a-zA-Z0-9_-]+)\/playlist\/([a-zA-Z0-9]+)#", $url_clean, $chaines))
 | 
	
		
			
			|  | 46 | +    {
 | 
	
		
			
			|  | 47 | +      $type   = 'playlist';
 | 
	
		
			
			|  | 48 | +      $this->element->setData(Element::DATA_PLAYLIST_AUTHOR, $chaines[1]);
 | 
	
		
			
			|  | 49 | +      $ref_id = $chaines[2];
 | 
	
		
			
			|  | 50 | +      $get_data_from_api = false;
 | 
	
		
			
			|  | 51 | +    }
 | 
	
		
			
			|  | 52 | +    
 | 
	
		
			
			|  | 53 | +    $this->element->setData(Element::DATA_TYPE  , $type);
 | 
	
		
			
			|  | 54 | +    $this->element->setData(Element::DATA_REF_ID, $ref_id);
 | 
	
		
			
			|  | 55 | +      
 | 
	
		
			
			|  | 56 | +    if ($get_data_from_api)
 | 
	
		
			
			|  | 57 | +    {
 | 
	
		
			
			|  | 58 | +      $this->getDataFromApi($ref_id);
 | 
	
		
			
			|  | 59 | +    }
 | 
	
		
			
			|  | 60 | +  }
 | 
	
		
			
			|  | 61 | +  
 | 
	
		
			
			|  | 62 | +  protected function getDataFromApi($ref_id)
 | 
	
		
			
			|  | 63 | +  {
 | 
	
		
			
			|  | 64 | +    if ($this->element->getData(Element::DATA_TYPE) == 'track')
 | 
	
		
			
			|  | 65 | +    {
 | 
	
		
			
			|  | 66 | +      $data = $this->getJsonDataFromApiWithUrl('http://ws.spotify.com/lookup/1/.json?uri=spotify:track:'.$ref_id);
 | 
	
		
			
			|  | 67 | +      
 | 
	
		
			
			|  | 68 | +      if (array_key_exists('track', $data))
 | 
	
		
			
			|  | 69 | +      {
 | 
	
		
			
			|  | 70 | +        if (array_key_exists('available', $data['track']))
 | 
	
		
			
			|  | 71 | +        {
 | 
	
		
			
			|  | 72 | +          if ($data['track']['available'])
 | 
	
		
			
			|  | 73 | +          {
 | 
	
		
			
			|  | 74 | +            if (array_key_exists('artist', $data['track']))
 | 
	
		
			
			|  | 75 | +            {
 | 
	
		
			
			|  | 76 | +              $this->element->setData(Element::DATA_ARTIST, $data['track']['artist']);
 | 
	
		
			
			|  | 77 | +            }
 | 
	
		
			
			|  | 78 | +            if (array_key_exists('artists', $data['track']))
 | 
	
		
			
			|  | 79 | +            {
 | 
	
		
			
			|  | 80 | +              if (count($data['track']['artists']))
 | 
	
		
			
			|  | 81 | +              {
 | 
	
		
			
			|  | 82 | +                if (array_key_exists('name', $data['track']['artists'][0]))
 | 
	
		
			
			|  | 83 | +                {
 | 
	
		
			
			|  | 84 | +                  $this->element->setData(Element::DATA_ARTIST, $data['track']['artists'][0]['name']);
 | 
	
		
			
			|  | 85 | +                }
 | 
	
		
			
			|  | 86 | +              }
 | 
	
		
			
			|  | 87 | +            }
 | 
	
		
			
			|  | 88 | +            if (array_key_exists('name', $data['track']))
 | 
	
		
			
			|  | 89 | +            {
 | 
	
		
			
			|  | 90 | +              $this->element->setData(Element::DATA_TITLE, $data['track']['name']);
 | 
	
		
			
			|  | 91 | +            }
 | 
	
		
			
			|  | 92 | +          }
 | 
	
		
			
			|  | 93 | +        }
 | 
	
		
			
			|  | 94 | +      }
 | 
	
		
			
			|  | 95 | +    }
 | 
	
		
			
			|  | 96 | +    if ($this->element->getData(Element::DATA_TYPE) == 'album')
 | 
	
		
			
			|  | 97 | +    {
 | 
	
		
			
			|  | 98 | +      $data = $this->getJsonDataFromApiWithUrl('http://ws.spotify.com/lookup/1/.json?uri=spotify:album:'.$ref_id);
 | 
	
		
			
			|  | 99 | +      
 | 
	
		
			
			|  | 100 | +      if (array_key_exists('album', $data))
 | 
	
		
			
			|  | 101 | +      {
 | 
	
		
			
			|  | 102 | +        if (array_key_exists('artist', $data['album']))
 | 
	
		
			
			|  | 103 | +        {
 | 
	
		
			
			|  | 104 | +          $this->element->setData(Element::DATA_ARTIST, $data['album']['artist']);
 | 
	
		
			
			|  | 105 | +        }
 | 
	
		
			
			|  | 106 | +        if (array_key_exists('artists', $data['album']))
 | 
	
		
			
			|  | 107 | +        {
 | 
	
		
			
			|  | 108 | +          if (count($data['album']['artists']))
 | 
	
		
			
			|  | 109 | +          {
 | 
	
		
			
			|  | 110 | +            if (array_key_exists('name', $data['album']['artists'][0]))
 | 
	
		
			
			|  | 111 | +            {
 | 
	
		
			
			|  | 112 | +              $this->element->setData(Element::DATA_ARTIST, $data['album']['artists'][0]['name']);
 | 
	
		
			
			|  | 113 | +            }
 | 
	
		
			
			|  | 114 | +          }
 | 
	
		
			
			|  | 115 | +        }
 | 
	
		
			
			|  | 116 | +        if (array_key_exists('name', $data['album']))
 | 
	
		
			
			|  | 117 | +        {
 | 
	
		
			
			|  | 118 | +          $this->element->setData(Element::DATA_TITLE, $data['album']['name']);
 | 
	
		
			
			|  | 119 | +        }
 | 
	
		
			
			|  | 120 | +      }
 | 
	
		
			
			|  | 121 | +    }
 | 
	
		
			
			|  | 122 | +    
 | 
	
		
			
			|  | 123 | +  }
 | 
	
		
			
			|  | 124 | +  
 | 
	
		
			
			|  | 125 | +  public function proceedEmbedCode()
 | 
	
		
			
			|  | 126 | +  {
 | 
	
		
			
			|  | 127 | +    if (($ref_id = $this->element->getData(Element::DATA_REF_ID)))
 | 
	
		
			
			|  | 128 | +    {
 | 
	
		
			
			|  | 129 | +      $uri = '';
 | 
	
		
			
			|  | 130 | +      $type = $this->element->getData(Element::DATA_TYPE);
 | 
	
		
			
			|  | 131 | +      
 | 
	
		
			
			|  | 132 | +      if ($type == 'playlist')
 | 
	
		
			
			|  | 133 | +      {
 | 
	
		
			
			|  | 134 | +        $uri = 
 | 
	
		
			
			|  | 135 | +          'spotify:user:'.$this->element->getData(Element::DATA_PLAYLIST_AUTHOR).':'
 | 
	
		
			
			|  | 136 | +          .'playlist:'.$ref_id
 | 
	
		
			
			|  | 137 | +        ;
 | 
	
		
			
			|  | 138 | +      }
 | 
	
		
			
			|  | 139 | +      if ($type == 'album')
 | 
	
		
			
			|  | 140 | +      {
 | 
	
		
			
			|  | 141 | +        $uri = 
 | 
	
		
			
			|  | 142 | +          'spotify:album:'.$ref_id
 | 
	
		
			
			|  | 143 | +        ;
 | 
	
		
			
			|  | 144 | +      }
 | 
	
		
			
			|  | 145 | +      if ($type == 'track')
 | 
	
		
			
			|  | 146 | +      {
 | 
	
		
			
			|  | 147 | +        $uri = 
 | 
	
		
			
			|  | 148 | +          'spotify:track:'.$ref_id
 | 
	
		
			
			|  | 149 | +        ;
 | 
	
		
			
			|  | 150 | +      }
 | 
	
		
			
			|  | 151 | +      
 | 
	
		
			
			|  | 152 | +      $this->element->setEmbed(
 | 
	
		
			
			|  | 153 | +        '<iframe src="https://embed.spotify.com/?uri='.$uri.'&theme=white" frameborder="0" allowtransparency="true"></iframe>');
 | 
	
		
			
			|  | 154 | +    }
 | 
	
		
			
			|  | 155 | +  }
 | 
	
		
			
			|  | 156 | +  
 | 
	
		
			
			|  | 157 | +}
 |