Completed
Push — master ( 361a80...57c765 )
by Atanas
07:10
created

Network_Container   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 91
Duplicated Lines 9.89 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
dl 9
loc 91
rs 10
c 0
b 0
f 0
wmc 12
lcom 2
cbo 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A init() 0 6 2
A render() 9 9 3
A site_exists() 0 8 2
A get_site_id() 0 3 1
A set_site_id() 0 13 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Carbon_Fields\Container;
4
5
use Carbon_Fields\Datastore\Datastore;
6
use Carbon_Fields\Exception\Incorrect_Syntax_Exception;
7
8
/**
9
 * Theme options container class.
10
 */
11
class Network_Container extends Theme_Options_Container {
12
	/**
13
	 * ID of the site the container is operating with
14
	 *
15
	 * @see init()
16
	 * @var int
17
	 */
18
	protected $site_id;
19
20
	/**
21
	 * {@inheritDoc}
22
	 */
23
	public function __construct( $id, $title, $type, $condition_collection, $condition_translator ) {
24
		parent::__construct( $id, $title, $type, $condition_collection, $condition_translator );
25
26
		if ( ! is_multisite() ) {
27
			Incorrect_Syntax_Exception::raise( 'The "' . $title . '" container will not be available because your site is not a multisite.' );
28
			return;
29
		}
30
31
		$this->set_datastore( Datastore::make( 'network' ), $this->has_default_datastore() );
32
		$this->set_site_id( SITE_ID_CURRENT_SITE );
33
	}
34
35
	/**
36
	 * {@inheritDoc}
37
	 */
38
	public function init() {
39
		$registered = $this->register_page();
40
		if ( $registered ) {
41
			add_action( 'network_admin_menu', array( $this, '_attach' ) );
42
		}
43
	}
44
45
	/**
46
	 * {@inheritDoc}
47
	 */
48 View Code Duplication
	public function render() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
		$input = stripslashes_deep( $_GET );
50
		$request_settings_updated = isset( $input['settings-updated'] ) ? $input['settings-updated'] : '';
51
		if ( $request_settings_updated === 'true' ) {
52
			$this->notifications[] = __( 'Settings saved.', 'carbon-fields' );
53
		}
54
55
		include \Carbon_Fields\DIR . '/templates/Container/network.php';
56
	}
57
58
	/**
59
	 * Check if a site exists by id
60
	 *
61
	 * @param  integer $id
62
	 * @return boolean
63
	 */
64
	protected function site_exists( $id ) {
65
		if ( ! function_exists( 'get_blog_status' ) ) {
66
			return false;
67
		}
68
69
		$blog_domain = get_blog_status( $id, 'domain' );
70
		return ! empty( $blog_domain );
71
	}
72
73
	/**
74
	 * Get the site ID the container is operating with.
75
	 *
76
	 * @return integer
77
	 */
78
	public function get_site_id() {
79
		return $this->site_id;
80
	}
81
82
	/**
83
	 * Set the site ID the container will operate with.
84
	 *
85
	 * @param  int  $id
86
	 * @return self $this
87
	 */
88
	public function set_site_id( $id ) {
89
		$id = intval( $id );
90
91
		if ( ! $this->site_exists( $id ) ) {
92
			Incorrect_Syntax_Exception::raise( 'The specified site id #' . $id . ' does not exist' );
93
			return $this;
94
		}
95
96
		$this->site_id = $id;
97
		$this->datastore->set_object_id( $this->get_site_id() );
98
99
		return $this;
100
	}
101
}
102