Skip to content

Commit

Permalink
Refactoring - extract two methods in Parser class
Browse files Browse the repository at this point in the history
  • Loading branch information
MAXakaWIZARD committed Apr 30, 2015
1 parent b894f9a commit 4c25679
Showing 1 changed file with 34 additions and 11 deletions.
45 changes: 34 additions & 11 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,9 @@ class Parser
*/
public function parse($filePath, $itemCallback)
{
if (!is_callable($itemCallback)) {
throw new \Exception("Callback should be callable");
}
$this->checkCallback($itemCallback);

if (!is_file($filePath)) {
throw new \Exception('File does not exist: ' . $filePath);
}

$stream = @fopen($filePath, 'r');
if (false === $stream) {
throw new \Exception('Unable to open file for read: ' . $filePath);
}
$stream = $this->openFile($filePath);

try {
$listener = new Listener($itemCallback);
Expand All @@ -48,6 +39,38 @@ public function parse($filePath, $itemCallback)
fclose($stream);
}

/**
* @param $callback
*
* @throws \Exception
*/
protected function checkCallback($callback)
{
if (!is_callable($callback)) {
throw new \Exception("Callback should be callable");
}
}

/**
* @param $filePath
*
* @return resource
* @throws \Exception
*/
protected function openFile($filePath)
{
if (!is_file($filePath)) {
throw new \Exception('File does not exist: ' . $filePath);
}

$stream = @fopen($filePath, 'r');
if (false === $stream) {
throw new \Exception('Unable to open file for read: ' . $filePath);
}

return $stream;
}

/**
* @param $name
* @param $value
Expand Down

0 comments on commit 4c25679

Please sign in to comment.