Completed
Push — master ( eef885...465d0c )
by
unknown
30:08 queued 12:49
created

log_service_class   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 105
rs 10
c 0
b 0
f 0
wmc 20
lcom 0
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A construct() 0 1 1
B add() 0 28 6
A get_service_by_id() 0 14 4
B get_service_by_name() 0 14 7
A create_service() 0 10 2
1
<?php
2
/**
3
 * File for service log definition
4
 *
5
 * @package wpeo_logs
6
 */
7
8
namespace eoxia;
9
10
if ( ! defined( 'ABSPATH' ) ) {
11
	exit;
12
}
13
14
if ( ! class_exists( '\eoxia\log_service_class' ) ) {
15
	/**
16
	 * Class for service log definition
17
	 */
18
	class log_service_class extends \eoxia\Singleton_Util {
19
20
		/**
21
		 * Instanciate the log service
22
		 */
23
		protected function construct() {}
24
25
		/**
26
		 * Crée un service par défaut et l'ajoutes au tableau JSON _wpeo_log_settings.
27
		 * Renvoie un message de type updated en transient pour afficher qu'un
28
		 * nouveau service à été crée.
29
		 *
30
		 * @param string $name Le nom du service.
31
		 */
32
		public function add( $name ) {
33
			$data_service = array(
34
					'active' 		=> true,
35
					'name'			=> ! empty( $name ) ? $name : 'new_log',
36
					'size' 			=> '1000000',
37
					'format' 		=> 'ko',
38
					'rotate'		=> false,
39
					'number' 		=> 0,
40
					'created_date'	=> current_time( 'mysql' ),
41
			);
42
			$array_current_settings = get_option( '_wpeo_log_settings' );
43
			if ( ! empty( $array_current_settings ) && ! is_array( $array_current_settings ) ) {
44
				$array_current_settings = json_decode( $array_current_settings, true );
45
			} else {
46
				$array_current_settings = array();
47
			}
48
			$array_current_settings[] = $data_service;
49
			$success = update_option( '_wpeo_log_settings', wp_json_encode( $array_current_settings ) );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $success is correct as update_option('_wpeo_log...rray_current_settings)) (which targets update_option()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
50
			if ( $success ) {
51
				set_transient( 'log_message', wp_json_encode( array( 'type' => 'updated', 'message' => __( 'A new service has been created!', 'digirisk' ) ) ) );
52
			}
53
			if ( ! empty( $name ) ) {
54
				return $data_service;
55
			} else {
56
				wp_safe_redirect( wp_get_referer() );
57
				die();
58
			}
59
		}
60
61
		/**
62
		 * Get a service definition by id
63
		 *
64
		 * @param  integer $id Service identifier to get complete definition for.
65
		 * @return array     The service definition
66
		 */
67
		public function get_service_by_id( $id ) {
68
			$array_service = get_option( '_wpeo_log_settings', array() );
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
69
			$getted_service = null;
70
			if ( ! empty( $array_service ) ) {
71
				$array_service = json_decode( $array_service, true );
72
				foreach ( $array_service as $key => $service ) {
73
					if ( $key == $id ) {
74
						$getted_service = $service;
75
						break;
76
					}
77
				}
78
			}
79
			return $getted_service;
80
		}
81
82
		/**
83
		 * Récupères le service selon son nom.
84
		 *
85
		 * @param string $name Le nom du service.
86
		 *
87
		 * @return array | null
88
		 */
89
		public static function get_service_by_name( $name ) {
90
			$array_service = get_option( '_wpeo_log_settings', array() );
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
91
			$getted_service = null;
92
			if ( ! empty( $array_service ) ) {
93
				$array_service = ! is_array( $array_service ) ? json_decode( $array_service, true ) : $array_service;
94
				foreach ( $array_service as $service ) {
95
					if ( ! empty( $service ) && ! empty( $service['name'] ) && ( $service['name'] === $name ) ) {
96
						$getted_service = $service;
97
						break;
98
					}
99
				}
100
			}
101
			return $getted_service;
102
		}
103
104
		/**
105
		 * Create a new service in database in case it does not exists
106
		 *
107
		 * @param  string $service_name The service name to check if it exists or to create if it is not the case.
108
		 *
109
		 * @return array               The service definition
110
		 */
111
		public function create_service( $service_name ) {
112
			$service = self::get_service_by_name( $service_name );
113
114
			if ( null === $service ) {
115
				/** Créer le service s'il n'existe pas */
116
				$service = self::add( $service_name );
117
			}
118
119
			return $service;
120
		}
121
122
	}
123
124
}
125