/**
  * Get the messages list.
  *
  * @param Request $request HTTP Request object
  *
  * @internal param int $page The pagenumber assigned by MAL.
  *
  * @return View
  */
 public function getAction(Request $request)
 {
     // http://myanimelist.net/mymessages.php?go=&show=#{page}
     $downloader = $this->get('atarashii_api.communicator');
     $page = (int) $request->query->get('page');
     if ($page <= 0) {
         $page = 1;
     }
     //get the credentials we received
     $username = $this->getRequest()->server->get('PHP_AUTH_USER');
     $password = $this->getRequest()->server->get('PHP_AUTH_PW');
     try {
         if (!$downloader->cookieLogin($username, $password)) {
             $view = $this->view(array('error' => 'unauthorized'), 401);
             $view->setHeader('WWW-Authenticate', 'Basic realm="myanimelist.net"');
             return $view;
         }
         Date::setTimeZone($downloader->fetch('/editprofile.php'));
         $messagesdetails = $downloader->fetch('/mymessages.php?go=&show=' . ($page * 20 - 20));
     } catch (Exception\CurlException $e) {
         return $this->view(array('error' => 'network-error'), 500);
     }
     if (strpos($messagesdetails, 'You have 0 messages') !== false) {
         return $this->view(array('error' => 'No messages found.'), 404);
     } else {
         $messages = MessagesParser::parse($messagesdetails);
         $response = new Response();
         $view = $this->view($messages);
         $view->setResponse($response);
         $view->setStatusCode(200);
         return $view;
     }
 }
Пример #2
0
 public function testParseMessage()
 {
     $messageContent = file_get_contents(__DIR__ . '/../InputSamples/user-message.html');
     $message = MessagesParser::parseMessage($messageContent, 1234);
     $this->assertInstanceOf('Atarashii\\APIBundle\\Model\\Messages', $message);
     //Do some basic type checking for things that should exist (not null)
     $this->assertInternalType('int', $message->getId());
     $this->assertInternalType('string', $message->getUsername());
     $this->assertInternalType('string', $message->getSubject());
     $this->assertInternalType('string', $message->getMessage());
     $this->assertNull($message->getPreview());
 }