Skip to content

Commit 8252ca2

Browse files
committed
Fixing exceptions
1 parent 38d111f commit 8252ca2

File tree

4 files changed

+26
-32
lines changed

4 files changed

+26
-32
lines changed

lib/IDS/Caching/FileCache.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333

3434
namespace IDS\Caching;
3535

36+
use IDS\Init;
37+
3638
/**
3739
* File caching wrapper
3840
*
@@ -56,28 +58,28 @@ class FileCache implements CacheInterface
5658
*
5759
* @var string
5860
*/
59-
private $type = null;
61+
private $type;
6062

6163
/**
6264
* Cache configuration
6365
*
6466
* @var array
6567
*/
66-
private $config = null;
68+
private $config;
6769

6870
/**
6971
* Path to cache file
7072
*
7173
* @var string
7274
*/
73-
private $path = null;
75+
private $path;
7476

7577
/**
7678
* Holds an instance of this class
7779
*
7880
* @var object
7981
*/
80-
private static $cachingInstance = null;
82+
private static $cachingInstance;
8183

8284
/**
8385
* Constructor
@@ -88,7 +90,7 @@ class FileCache implements CacheInterface
8890
*
8991
* @return void
9092
*/
91-
public function __construct($type, $init)
93+
public function __construct($type, Init $init)
9294
{
9395
$this->type = $type;
9496
$this->config = $init->config['Caching'];

lib/IDS/Filter/Storage.php

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -201,22 +201,18 @@ public function getFilterFromXML()
201201
* If they aren't, parse the source file
202202
*/
203203
if (!$filters) {
204-
if (file_exists($this->source)) {
205-
if (LIBXML_VERSION >= 20621) {
206-
$filters = simplexml_load_file(
207-
$this->source,
208-
null,
209-
LIBXML_COMPACT
210-
);
211-
} else {
212-
$filters = simplexml_load_file($this->source);
213-
}
204+
205+
if (!file_exists($this->source)) {
206+
throw new \InvalidArgumentException(
207+
sprintf('Invalid config: %s doesn\'t exist.', $this->source)
208+
);
214209
}
215210

216-
throw new \InvalidArgumentException(
217-
sprintf( 'Invalid config: %s doesn\'t exist. ',
218-
$this->source )
219-
);
211+
if (LIBXML_VERSION >= 20621) {
212+
$filters = simplexml_load_file($this->source, null, LIBXML_COMPACT);
213+
} else {
214+
$filters = simplexml_load_file($this->source);
215+
}
220216
}
221217

222218
/*
@@ -306,21 +302,16 @@ public function getFilterFromJson()
306302
* If they aren't, parse the source file
307303
*/
308304
if (!$filters) {
309-
if (file_exists($this->source)) {
310-
$filters = json_decode(file_get_contents($this->source));
305+
if (!file_exists($this->source)) {
306+
throw new \InvalidArgumentException(
307+
sprintf('Invalid config: %s doesn\'t exist.', $this->source)
308+
);
311309
}
312-
313-
throw new \RuntimeException(
314-
'JSON data could not be loaded.' .
315-
' Make sure you specified the correct path.'
316-
);
310+
$filters = json_decode(file_get_contents($this->source));
317311
}
318312

319313
if (!$filters) {
320-
throw new \RuntimeException(
321-
'JSON data could not be loaded.' .
322-
' Make sure you specified the correct path. ' . $this->source
323-
);
314+
throw new \RuntimeException('JSON data could not be loaded. Make sure you specified the correct path.');
324315
}
325316

326317
/*

tests/IDS/Tests/CachingTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function testCachingFileSetCache()
4949
{
5050
$this->init->config['Caching']['caching'] = 'file';
5151
$this->init->config['Caching']['expiration_time'] = 0;
52+
$this->init->config['Caching']['path'] = IDS_FILTER_CACHE_FILE;
5253
$cache = CacheFactory::factory($this->init, 'storage');
5354
$cache = $cache->setCache(array(1,2,3,4));
5455
$this->assertTrue($cache instanceof FileCache);
@@ -57,8 +58,8 @@ public function testCachingFileSetCache()
5758
public function testCachingFileGetCache()
5859
{
5960
$this->init->config['Caching']['caching'] = 'file';
60-
$this->init->config['Caching']['path'] = IDS_FILTER_CACHE_FILE;
6161
$this->init->config['Caching']['expiration_time'] = 0;
62+
$this->init->config['Caching']['path'] = IDS_FILTER_CACHE_FILE;
6263
$cache = CacheFactory::factory($this->init, 'storage');
6364
$cache = $cache->setCache(array(1,2,3,4));
6465
$this->assertEquals($cache->getCache(), array(1,2,3,4));

tests/IDS/Tests/ExceptionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function testInitConfigWrongPathException()
9696

9797
public function testWrongXmlFilterPathException()
9898
{
99-
$this->setExpectedException('RuntimeException');
99+
$this->setExpectedException('InvalidArgumentException');
100100
$this->init->config['General']['filter_type'] = 'xml';
101101
$this->init->config['General']['filter_path'] = 'IDS/wrong_path';
102102
new Monitor(array('test', 'bla'), $this->init);

0 commit comments

Comments
 (0)