Localization::holidayData()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 4
eloc 15
c 3
b 0
f 0
nc 8
nop 1
dl 0
loc 25
rs 9.7666
1
<?php
2
/* 
3
	Author: Irfa Ardiansyah <[email protected]>
4
    version: 1.1
5
    https://github.com/irfaardy/php-hari-libur
6
*/
7
namespace Irfa\HariLibur\Core;
8
9
use Irfa\HariLibur\Core\ConfigLoader as Conf;
10
11
class Localization
12
{
13
	private $region;
14
	/**
15
     * Method ini berfungsi untuk set tanggal dari config.
16
     *
17
     * @return boolean
18
    */
19
	private function lang()
20
	{
21
		if(!empty($this->region))
22
		{
23
			return strtoupper($this->region);
0 ignored issues
show
Bug Best Practice introduced by
The expression return strtoupper($this->region) returns the type string which is incompatible with the documented return type boolean.
Loading history...
24
		}
25
26
		$config = new Conf();
27
		return $config->region();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $config->region() returns the type string which is incompatible with the documented return type boolean.
Loading history...
28
	}
29
30
	protected function regionSet($region)
31
	{
32
		$this->region = $region;
33
	}
34
	/**
35
     * Method ini berfungsi untuk mengambil data libur dari file json.
36
     *
37
     * @return boolean
38
    */
39
	public function holidayData($array = true)
40
	{
41
		if(function_exists('resource_path'))
42
		{
43
			$published_file = resource_path('irfa/php-hari-libur/'.$this->lang().'.json');
44
		}else{
45
			$published_file = __DIR__.'../../../../../../resources/irfa/php-hari-libur/'.$this->lang().'.json';
46
		}
47
		if(file_exists($published_file))
48
		{
49
			$file = $published_file;
50
		} else{
51
			$file = __DIR__.'/'.'../../Data/'.$this->lang().'.json';
52
		}
53
		// dd($file);
54
		
55
		$this->checkFile($file);
56
		$arr = file_get_contents($file);
57
		if($this->isJson($arr))
58
		{
59
    		return json_decode($arr,$array);
60
		} else{
61
			 throw new \Exception('Json data is invalid.');
62
63
			 return false;
0 ignored issues
show
Unused Code introduced by
return false is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
64
		}
65
	}
66
	/**
67
     * Method ini berfungsi untuk validasi json
68
     *
69
     * @return boolean
70
    */
71
	private function isJson($string) {
72
		 json_decode($string);
73
		 return (json_last_error() == JSON_ERROR_NONE);
74
	}
75
76
	private function checkFile($file)
77
	{
78
		if(!file_exists($file))
79
		{
80
			 throw new \Exception('Region "'.$this->lang().'" is not found.');
81
			 return false;
0 ignored issues
show
Unused Code introduced by
return false is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
82
		}
83
	}
84
85
}
86