Completed
Push — master ( 8cf48b...afece8 )
by Kris
13s queued 10s
created

UtilsTrait::loadJsonFile()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 9
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 20
rs 8.8333
1
<?php declare(strict_types=1);
2
3
/**
4
 *     _    _                    ___ ____  ____  ____
5
 *    / \  | |__  _   _ ___  ___|_ _|  _ \|  _ \| __ )
6
 *   / _ \ | '_ \| | | / __|/ _ \| || |_) | | | |  _ \
7
 *  / ___ \| |_) | |_| \__ \  __/| ||  __/| |_| | |_) |
8
 * /_/   \_\_.__/ \__,_|___/\___|___|_|   |____/|____/
9
 *
10
 * This file is part of Kristuff\AbsuseIPDB.
11
 *
12
 * (c) Kristuff <[email protected]>
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 *
17
 * @version    0.9.10
18
 * @copyright  2020-2021 Kristuff
19
 */
20
namespace Kristuff\AbuseIPDB;
21
22
/**
23
 * Trait Utils
24
 */
25
trait UtilsTrait
26
{
27
    /**
28
     * helper function to get formatted date
29
     * 
30
     * @access private
31
     * @static
32
     * @param string    $date        The UTC date
33
     * 
34
     * @return string   Formated time
35
     */
36
    protected static function getDate($date): string
37
    {
38
        //2020-05-22T17:06:35+00:00
39
        return \DateTime::createFromFormat('Y-m-d\TH:i:s+', $date)->format('Y-m-d H:i:s');
40
    } 
41
42
    /**
43
     * helper function to get the color corresponding to given score:
44
     *   0    : green
45
     *   1-50 : yellow
46
     *   > 50 : lightred
47
     *  
48
     * @access protected
49
     * @static
50
     * @param mixed          $score    
51
     * 
52
     * @return string   
53
     * 
54
     */    
55
    protected static function getScoreColor($score): string
56
    {
57
        $score = intval($score);
58
        return $score > 50 ? 'lightred' : ($score > 0 ? 'yellow' : 'green') ;
59
    }
60
61
   
62
    /**
63
     * Helper function to get the value of an argument
64
     *  
65
     * @access protected
66
     * @static
67
     * @param array         $arguments      The list of arguments     
68
     * @param string        $shortArg       The short argument name
69
     * @param string        $longArg        The long argument name
70
     * 
71
     * @return string   
72
     * 
73
     */
74
    protected static function getArgumentValue(array $arguments, string $shortArg, string $longArg): string
75
    {
76
        return (array_key_exists($shortArg, $arguments) ? $arguments[$shortArg] : 
77
               (array_key_exists($longArg, $arguments) ? $arguments[$longArg]  : ''));
78
    }
79
80
    /**
81
     * helper function to check if a given argument is given
82
     * 
83
     * @access protected
84
     * @static
85
     * @param array     $arguments      The list of arguments     
86
     * @param string    $shortArg       The short argument name
87
     * @param string    $longArg        The long argument name
88
     * 
89
     * @return bool     True if the short or long argument exist in the arguments array, otherwise false
90
     */
91
    protected static function inArguments(array $arguments, string $shortArg, string $longArg): bool
92
    {
93
          return array_key_exists($shortArg, $arguments) || array_key_exists($longArg, $arguments);
94
    }
95
96
    /** 
97
     * Load and returns decoded Json from given file  
98
     *
99
     * @access public
100
     * @static
101
     * @param string    $filePath       The file's full path
102
     * @param bool      $throwError     Throw error on true or silent process. Default is true
103
     *  
104
	 * @return object|null 
105
     * @throws \Exception
106
     * @throws \LogicException
107
     */
108
    protected static function loadJsonFile(string $filePath, bool $throwError = true): ?object
109
    {
110
        // check file exists
111
        if (!file_exists($filePath) || !is_file($filePath)){
112
           if ($throwError) {
113
                throw new \Exception('Config file not found');
114
           }
115
           return null;  
116
        }
117
118
        // get and parse content
119
        $content = utf8_encode(file_get_contents($filePath));
120
        $json    = json_decode($content);
121
122
        // check for errors
123
        if ($json == null && json_last_error() != JSON_ERROR_NONE && $throwError) {
124
            throw new \LogicException(sprintf("Failed to parse config file Error: '%s'", json_last_error_msg()));
125
        }
126
127
        return $json;        
128
    }
129
}