Example #1
0
 /**
  * @covers ::parseFriends
  */
 public function testParseFriends()
 {
     $friendsContent = file_get_contents(__DIR__ . '/../InputSamples/profile-motokochan-friends.html');
     $friends = User::parseFriends($friendsContent);
     foreach ($friends as $friend) {
         if ($friend['name'] == 'AnimaSA') {
             break;
         }
     }
     $this->assertEquals('AnimaSA', $friend['name']);
     $this->assertEquals('2014-06-15T20:58-0700', $friend['friend_since']);
     $this->assertInstanceOf('Atarashii\\ApiBundle\\Model\\Profile', $friend['profile']);
 }
 /**
  * Get a list of friends of the specified username.
  *
  * Returns a view of user objects constituting friends of the specified user. Sorting
  * is MyAnimeList default, in order of the most recently active user.
  *
  * @param string $apiVersion The API version of the request
  * @param string $username   The MyAnimeList username of the user.
  *
  * @return View
  */
 public function getFriendsAction($apiVersion, $username)
 {
     // http://myanimelist.net/profile/#{username}/friends
     $downloader = $this->get('atarashii_api.communicator');
     try {
         $friendscontent = $downloader->fetch('/profile/' . $username . '/friends');
     } catch (Exception\CurlException $e) {
         return $this->view(array('error' => 'network-error'), 500);
     } catch (Exception\ClientErrorResponseException $e) {
         $friendscontent = $e->getResponse();
     }
     $response = new Response();
     $serializationContext = SerializationContext::create();
     $serializationContext->setVersion($apiVersion);
     //For compatibility, API 1.0 explicitly passes null parameters.
     if ($apiVersion == '1.0') {
         $serializationContext->setSerializeNull(true);
     }
     $response->setPublic();
     $response->setMaxAge(900);
     //15 minutes
     $response->headers->addCacheControlDirective('must-revalidate', true);
     $response->setEtag('friends/' . $username);
     //Also, set "expires" header for caches that don't understand Cache-Control
     $date = new \DateTime();
     $date->modify('+900 seconds');
     //15 minutes
     $response->setExpires($date);
     if (strpos($friendscontent, 'Failed to find') !== false) {
         $view = $this->view(array('error' => 'not-found'));
         $view->setResponse($response);
         $view->setStatusCode(404);
         return $view;
     } else {
         if (strpos($friendscontent, 'No friends found') === false) {
             $friendlist = User::parseFriends($friendscontent);
         } else {
             $friendlist = array();
         }
         $view = $this->view($friendlist);
         $view->setSerializationContext($serializationContext);
         $view->setResponse($response);
         $view->setStatusCode(200);
         return $view;
     }
 }