Completed
Push — master ( 252a1e...138995 )
by Kris
24s queued 10s
created

UtilsTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
eloc 7
c 2
b 0
f 0
dl 0
loc 69
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getArgumentValue() 0 4 3
A inArguments() 0 3 2
A getScoreColor() 0 4 3
A getDate() 0 4 1
1
<?php
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.8
18
 * @copyright  2020-2021 Kristuff
19
 */
20
namespace Kristuff\AbuseIPDB;
21
22
/**
23
 * Class Utils
24
 * 
25
 */
26
trait UtilsTrait
27
{
28
    /**
29
     * helper function to get formatted date
30
     * 
31
     * @access private
32
     * @static
33
     * @param string    $date        The UTC date
34
     * 
35
     * @return string   Formated time
36
     */
37
    protected static function getDate($date)
38
    {
39
        //2020-05-22T17:06:35+00:00
40
        return \DateTime::createFromFormat('Y-m-d\TH:i:s+', $date)->format('Y-m-d H:i:s');
41
    } 
42
43
    /**
44
     * helper function to get the color corresponding to given score:
45
     *   0    : green
46
     *   1-50 : yellow
47
     *   > 50 : lightred
48
     *  
49
     * @access protected
50
     * @static
51
     * @param mixed          $score    
52
     * 
53
     * @return string   
54
     * 
55
     */    
56
    protected static function getScoreColor($score)
57
    {
58
        $score = intval($score);
59
        return $score > 50 ? 'lightred' : ($score > 0 ? 'yellow' : 'green') ;
60
    }
61
62
  
63
    /**
64
     * Helper function to get the value of an argument
65
     *  
66
     * @access protected
67
     * @static
68
     * @param array         $arguments      The list of arguments     
69
     * @param string        $shortArg       The short argument name
70
     * @param string        $longArg        The long argument name
71
     * 
72
     * @return string   
73
     * 
74
     */
75
    protected static function getArgumentValue(array $arguments, string $shortArg, string $longArg)
76
    {
77
        return (array_key_exists($shortArg, $arguments) ? $arguments[$shortArg] : 
78
               (array_key_exists($longArg, $arguments) ? $arguments[$longArg]  : ''));
79
    }
80
81
    /**
82
     * helper function to check if a argument is given
83
     * 
84
     * @access protected
85
     * @static
86
     * @param array     $arguments      The list of arguments     
87
     * @param string     $shortArg       The short argument name
88
     * @param string     $longArg        The long argument name
89
     * 
90
     * @return bool     True if the short or long argument exist in the arguments array, otherwise false
91
     */
92
    protected static function inArguments(array $arguments, string $shortArg, string $longArg)
93
    {
94
          return array_key_exists($shortArg, $arguments) || array_key_exists($longArg, $arguments);
95
    }
96
}