Skip to content

Commit

Permalink
Merge 842a4de into 378e2f9
Browse files Browse the repository at this point in the history
  • Loading branch information
elabz committed Nov 15, 2017
2 parents 378e2f9 + 842a4de commit 27a1082
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ public function body($article)
return $this->sendCommand(new Command\BodyCommand($article));
}

/**
* {@inheritdoc}
*/
public function head($article)
{
return $this->sendCommand(new Command\HeadCommand($article));
}

/**
* {@inheritdoc}
*/
Expand Down
79 changes: 79 additions & 0 deletions src/Command/HeadCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/*
* This file is part of the NNTP library.
*
* (c) Robin van der Vleuten <robin@webstronauts.co>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Rvdv\Nntp\Command;

use Rvdv\Nntp\Exception\RuntimeException;
use Rvdv\Nntp\Response\MultiLineResponse;
use Rvdv\Nntp\Response\Response;

/**
* HeadCommand.
*
* @author elabz
*/
class HeadCommand extends Command implements CommandInterface
{
/**
* @var string
*/
private $article;

/**
* Constructor.
*
* @param string $article
*/
public function __construct($article)
{
$this->article = $article;

parent::__construct(true);
}

/**
* {@inheritdoc}
*/
public function __invoke()
{
return sprintf('HEAD %s', $this->article);
}

/**
* @return string
*/
public function onHeadFollows(MultiLineResponse $response)
{
return array_reduce($response->getLines(), function ($headers, $line) {
preg_match('/^([^\:]+)\:\s*(.*)$/', $line, $matches);
if (!empty($matches)) {
$headers[$matches[1]] = trim($matches[2]);
}

return $headers;
});
}

public function onNoNewsGroupCurrentSelected(Response $response)
{
throw new RuntimeException('A group must be selected first before getting an article header.', $response->getStatusCode());
}

public function onNoSuchArticleNumber(Response $response)
{
throw new RuntimeException('No article with that number.', $response->getStatusCode());
}

public function onNoSuchArticleId(Response $response)
{
throw new RuntimeException('No article with that message-id.', $response->getStatusCode());
}
}
1 change: 1 addition & 0 deletions src/Response/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Response implements ResponseInterface
'GroupSelected' => 211, // rfc 3977
'InformationFollows' => 215, // rfc 2980
'ArticleFollows' => 220, //rfc 3977
'HeadFollows' => 221, //rfc 3977 Section 6.2.2
'BodyFollows' => 222, //rfc 3977
'OverviewInformationFollows' => 224, // rfc 2980
'ArticleReceived' => 240, //rfc 3977
Expand Down

0 comments on commit 27a1082

Please sign in to comment.