Skip to content

Commit

Permalink
Parser factory now expects data file path as argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Tibor Barna committed Jul 16, 2016
1 parent 4ae2864 commit 20c5133
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion examples/account.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require_once dirname(__DIR__) . '/vendor/autoload.php';

$factory = new Udger\ParserFactory();
$factory = new Udger\ParserFactory(sys_get_temp_dir() . "/udgercache/udgerdb_v3.dat");
$parser = $factory->getParser();

try {
Expand Down
7 changes: 1 addition & 6 deletions examples/parse.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@
require_once dirname(__DIR__) . '/vendor/autoload.php';

// creates a new UdgerParser object
$factory = new Udger\ParserFactory();
$factory = new Udger\ParserFactory(sys_get_temp_dir() . "/udgercache/udgerdb_v3.dat");
$parser = $factory->getParser();




try {
// set data file
$parser->setDataFile(sys_get_temp_dir() . "/udgercache/udgerdb_v3.dat");
$parser->setUA('Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36');
$parser->setIP("66.249.64.1");

Expand Down
14 changes: 11 additions & 3 deletions src/ParserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,24 @@ class ParserFactory {
*/
private $logger;

/**
* @var string $dataFile path to the data file
*/
private $dataFile;

/**
*
* @param string $dataFile path to the data file
* @param LoggerInterface $logger
*/
public function __construct($logger = null)
public function __construct($dataFile, $logger = null)
{
if (is_null($logger)) {
// create a log channel
$logger = new Logger($this->loggerName);
$logger->pushHandler(new NullHandler());
}

$this->dataFile = $dataFile;
$this->logger = $logger;
}

Expand All @@ -45,6 +51,8 @@ public function __construct($logger = null)
*/
public function getParser()
{
return new Parser($this->logger, new Helper\IP());
$parser = new Parser($this->logger, new Helper\IP());
$parser->setDataFile($this->dataFile);
return $parser;
}
}
8 changes: 7 additions & 1 deletion tests/unit/ParserFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@ class ParserFactoryTest extends \Codeception\TestCase\Test {

protected function _before()
{
$this->factory = new ParserFactory();
$this->factory = new ParserFactory("/dev/null");
}

public function testGetParser()
{
$this->assertInstanceOf("Udger\Parser", $this->factory->getParser());
}

public function testNewFactoryWithoutPathShouldFail()
{
$this->setExpectedException('PHPUnit_Framework_Exception');
new ParserFactory();
}
}

0 comments on commit 20c5133

Please sign in to comment.