protected function execute(InputInterface $input, OutputInterface $output) { $table = $this->tableManager->getTable('Phpsx\\Website\\Table\\Blog'); $atom = new Atom(); $message = new Message(['Content-Type' => 'application/atom+xml']); $message->setBody(new TempStream(fopen($this->feedFile, 'r'))); $postCount = $putCount = 0; $atom = $this->importer->import($atom, $message); foreach ($atom as $entry) { $row = $table->getOneById($entry->getId()); $author = $this->getFirstAuthor($entry); if (!$author instanceof Atom\Person) { throw new \RuntimeException(sprintf('No author provided for entry %s', $entry->getId())); } if (!$row instanceof RecordInterface) { // the blog entry does not exist create it $table->create(['id' => $entry->getId(), 'title' => $entry->getTitle(), 'titleSlug' => $this->slugify->slugify($entry->getTitle()), 'authorName' => $author->getName(), 'authorUri' => $author->getUri(), 'updated' => $entry->getUpdated(), 'summary' => $entry->getSummary(), 'category' => $this->getCategories($entry), 'content' => $entry->getContent()->getContent()]); $postCount++; } else { if ($entry->getUpdated() > $row->getUpdated()) { // if the update date has change update the entry $table->update(['id' => $entry->getId(), 'title' => $entry->getTitle(), 'titleSlug' => $this->slugify->slugify($entry->getTitle()), 'authorName' => $author->getName(), 'authorUri' => $author->getUri(), 'updated' => $entry->getUpdated(), 'summary' => $entry->getSummary(), 'category' => $this->getCategories($entry), 'content' => $entry->getContent()->getContent()]); $putCount++; } } } $output->writeln(sprintf('Created %s and updated %s entries', $postCount, $putCount)); }
/** * @param integer $code * @param array $headers * @param string $body */ public function __construct($code = null, array $headers = array(), $body = null) { parent::__construct($headers, $body); $this->code = $code; }
/** * Parses an raw http header string into an Message object * * @param \PSX\Http\Message $message * @param string $header * @return array */ protected function headerToArray(Message $message, $header) { $lines = explode(Http::$newLine, $header); foreach ($lines as $line) { $parts = explode(':', $line, 2); if (isset($parts[0]) && isset($parts[1])) { $key = $parts[0]; $value = substr($parts[1], 1); $message->addHeader($key, $value); } } }
/** * @param \PSX\Uri $uri * @param string $method * @param array $headers * @param string $body */ public function __construct(Uri $uri, $method, array $headers = array(), $body = null) { parent::__construct($headers, $body); $this->uri = $uri; $this->method = $method; }
public function testSetBodyResource() { $handle = fopen('php://memory', 'r+'); fwrite($handle, 'foobar'); $message = new Message(array(), $handle); $this->assertEquals('foobar', (string) $message->getBody()); }
/** * @expectedException \RuntimeException */ public function testImportEmptyBody() { $message = new Message(); $message->addHeader('Content-Type', 'application/json'); $message->setBody(new StringStream('')); $importer = new Importer(Environment::getService('extractor'), Environment::getService('importer_manager')); $importer->import(new Page(), $message); }
public function testExtractJsonExplicitReader() { $data = <<<TEXT {"foo": "bar"} TEXT; $message = new Message(); $message->addHeader('Content-Type', 'text/plain'); $message->setBody(new StringStream($data)); $extractor = new Extractor(Environment::getService('reader_factory'), Environment::getService('transformer_manager')); $data = $extractor->extract($message, null, ReaderInterface::JSON); $expect = new \stdClass(); $expect->foo = 'bar'; $this->assertEquals($expect, $data); }