Completed
Push — master ( cf9740...0c1274 )
by Zack
19:03 queued 03:33
created

GF_Form   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 21.15%

Importance

Changes 0
Metric Value
dl 0
loc 162
ccs 11
cts 52
cp 0.2115
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 4

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A by_id() 0 13 2
A offsetExists() 0 3 1
A offsetGet() 0 3 1
A offsetSet() 0 3 1
A offsetUnset() 0 3 1
B get_entries() 0 60 7
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 14 and the first side effect is on line 6.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
namespace GV;
3
4
/** If this file is called directly, abort. */
5
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
6
	die();
7
}
8
9
/**
10
 * The Gravity Forms Form class implementation.
11
 *
12
 * Accessible as an array for back-compatibility.
13
 */
14
class GF_Form extends Form implements \ArrayAccess {
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
15
16
	/**
17
	 * @var string The identifier of the backend used for this form.
18
	 * @api
19
	 * @since future
20
	 */
21
	public static $backend = self::BACKEND_GRAVITYFORMS;
22
23
	/**
24
	 * Initialization.
25
	 */
26 3
	private function __construct() {
27 3
		if ( ! class_exists( 'GFAPI' ) ) {
28
			gravityview()->log->error( 'Gravity Forms plugin is not active.' );
29
		}
30 3
	}
31
32
	/**
33
	 * Construct a \GV\GF_Form instance by ID.
34
	 *
35
	 * @param int|string $form_id The internal form ID.
36
	 *
37
	 * @api
38
	 * @since future
39
	 * @return \GV\GF_Form|null An instance of this form or null if not found.
40
	 */
41 4
	public static function by_id( $form_id ) {
42 4
		$form = \GFAPI::get_form( $form_id );
43 4
		if ( !$form ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
44 1
			return null;
45
		}
46
47 4
		$self = new self();
48 4
		$self->form = $form;
0 ignored issues
show
Bug introduced by
The property form cannot be accessed from this context as it is declared private in class GV\Form.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
49
50 4
		$self->ID = $self->form['id'];
0 ignored issues
show
Documentation introduced by
The property $form is declared private in GV\Form. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
51
52 4
		return $self;
53
	}
54
55
	/**
56
	 * Get all entries for this form.
57
	 *
58
	 * @api
59
	 * @since future
60
	 *
61
	 * @return \GV\Entry_Collection The \GV\Entry_Collection
62
	 */
63
	public function get_entries() {
64
		$entries = new \GV\Entry_Collection();
65
66
		$form = &$this;
67
68
		/** Add the fetcher lazy callback. */
69
		$entries->add_fetch_callback( function( $filters, $sorts, $offset ) use ( $form ) {
70
			$entries = new \GV\Entry_Collection();
71
72
			$search_criteria = array();
73
			$sorting = array();
74
			$paging = array();
75
76
			/** Apply the filters */
77
			foreach ( $filters as $filter ) {
78
				$search_criteria = $filter::merge_search_criteria( $search_criteria, $filter->as_search_criteria() );
79
			}
80
81
			/** Apply the sorts */
82
			foreach ( $sorts as $sort ) {
83
				/** Gravity Forms does not have multi-sorting, so just overwrite. */
84
				$sorting = array(
85
					'key' => $sort->field->ID,
86
					'direction' => $sort->direction,
87
					'is_numeric' => $sort->mode == Entry_Sort::NUMERIC,
88
				);
89
			}
90
91
			/** The offset and limit */
92
			if ( ! empty( $offset->limit ) ) {
93
				$paging['page_size'] = $offset->limit;
94
			}
95
96
			if ( ! empty( $offset->offset ) ) {
97
				$paging['offset'] = $offset->offset;
98
			}
99
100
			foreach ( \GFAPI::get_entries( $form->ID, $search_criteria, $sorting, $paging ) as $entry ) {
101
				$entries->add( \GV\GF_Entry::from_entry( $entry ) );
102
			}
103
104
			return $entries;
105
		} );
106
107
		/** Add the counter lazy callback. */
108
		$entries->add_count_callback( function( $filters ) use ( $form ) {
109
110
			$search_criteria = array();
111
			$sorting = array();
112
113
			/** Apply the filters */
114
			foreach ( $filters as $filter ) {
115
				$search_criteria = $filter::merge_search_criteria( $search_criteria, $filter->as_search_criteria() );
116
			}
117
118
			return \GFAPI::count_entries( $form->ID, $search_criteria );
119
		} );
120
121
		return $entries;
122
	}
123
124
	/**
125
	 * ArrayAccess compatibility layer with a Gravity Forms form array.
126
	 *
127
	 * @internal
128
	 * @deprecated
129
	 * @since future
130
	 * @return bool Whether the offset exists or not.
131
	 */
132
	public function offsetExists( $offset ) {
0 ignored issues
show
Coding Style introduced by
The function name offsetExists is in camel caps, but expected offset_exists instead as per the coding standard.
Loading history...
133
		return isset( $this->form[$offset] );
0 ignored issues
show
Documentation introduced by
The property $form is declared private in GV\Form. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
134
	}
135
136
	/**
137
	 * ArrayAccess compatibility layer with a Gravity Forms form array.
138
	 *
139
	 * Maps the old keys to the new data;
140
	 *
141
	 * @internal
142
	 * @deprecated
143
	 * @since future
144
	 *
145
	 * @return mixed The value of the requested form data.
146
	 */
147
	public function offsetGet( $offset ) {
0 ignored issues
show
Coding Style introduced by
The function name offsetGet is in camel caps, but expected offset_get instead as per the coding standard.
Loading history...
148
		return $this->form[$offset];
0 ignored issues
show
Documentation introduced by
The property $form is declared private in GV\Form. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
149
	}
150
151
	/**
152
	 * ArrayAccess compatibility layer with a Gravity Forms form array.
153
	 *
154
	 * @internal
155
	 * @deprecated
156
	 * @since future
157
	 *
158
	 * @return void
159
	 */
160
	public function offsetSet( $offset, $value ) {
0 ignored issues
show
Coding Style introduced by
The function name offsetSet is in camel caps, but expected offset_set instead as per the coding standard.
Loading history...
161
		gravityview()->log->error( 'The underlying Gravity Forms form is immutable. This is a \GV\Form object and should not be accessed as an array.' );
162
	}
163
164
	/**
165
	 * ArrayAccess compatibility layer with a Gravity Forms form array.
166
	 *
167
	 * @internal
168
	 * @deprecated
169
	 * @since future
170
	 * @return void
171
	 */
172
	public function offsetUnset( $offset ) {
0 ignored issues
show
Coding Style introduced by
The function name offsetUnset is in camel caps, but expected offset_unset instead as per the coding standard.
Loading history...
173
		gravityview()->log->error( 'The underlying Gravity Forms form is immutable. This is a \GV\Form object and should not be accessed as an array.' );
174
	}
175
}
176