Completed
Branch FET-9594-attendee-mover (f0632e)
by
unknown
335:55 queued 321:12
created

ProgressStepManager   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 175
rs 10
c 2
b 0
f 0
wmc 18
lcom 1
cbo 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 3
B setDisplayStrategy() 0 29 4
A setDefaultStep() 0 6 2
B setCurrentStep() 0 22 5
A currentStep() 0 3 1
A nextStep() 0 3 1
A enqueueStylesAndScripts() 0 3 1
A displaySteps() 0 7 1
1
<?php
2
namespace EventEspresso\core\services\progress_steps;
3
4
use EE_Request;
5
use EventEspresso\Core\Exceptions\InvalidClassException;
6
use EventEspresso\Core\Exceptions\InvalidDataTypeException;
7
use EventEspresso\Core\Exceptions\InvalidIdentifierException;
8
use EventEspresso\Core\Exceptions\InvalidInterfaceException;
9
use EventEspresso\core\services\collections\Collection;
10
use EventEspresso\core\services\collections\CollectionInterface;
11
use EventEspresso\core\services\progress_steps\display_strategies\ProgressStepsDisplayInterface;
12
13
if ( ! defined( 'EVENT_ESPRESSO_VERSION' ) ) {
14
	exit( 'No direct script access allowed' );
15
}
16
17
18
19
/**
20
 * Class ProgressStepManager
21
 * Description
22
 *
23
 * @package       Event Espresso
24
 * @subpackage    core
25
 * @author        Brent Christensen
26
 * @since         $VID:$
27
 */
28
class ProgressStepManager {
29
30
	/**
31
	 * @var ProgressStepInterface[] $collection
32
	 */
33
	protected $collection;
34
35
	/**
36
	 * @var string $default_step
37
	 */
38
	protected $default_step;
39
40
	/**
41
	 * @var ProgressStepsDisplayInterface $display_strategy
42
	 */
43
	protected $display_strategy;
44
45
	/**
46
	 * @var EE_Request $request
47
	 */
48
	protected $request;
49
50
51
52
	/**
53
	 * ProgressStepManager constructor
54
	 *
55
	 * @param string              $display_strategy_name
56
	 * @param string              $default_step
57
	 * @param CollectionInterface $collection
58
	 * @param \EE_Request         $request
59
	 * @throws InvalidClassException
60
	 * @throws InvalidDataTypeException
61
	 * @throws InvalidInterfaceException
62
	 */
63
	public function __construct(
64
		$display_strategy_name = 'number_bubbles',
65
		$default_step = '',
66
		CollectionInterface $collection = null,
67
		EE_Request $request = null
68
	) {
69
		$this->setDisplayStrategy( $display_strategy_name );
70
		$this->setDefaultStep( $default_step );
71
		if ( ! $collection instanceof CollectionInterface ) {
72
			$collection = new Collection( '\EventEspresso\core\services\progress_steps\ProgressStepInterface' );
73
		}
74
		$this->collection = $collection;
0 ignored issues
show
Documentation Bug introduced by
It seems like $collection of type object<EventEspresso\cor...ns\CollectionInterface> is incompatible with the declared type array<integer,object<Eve...ProgressStepInterface>> of property $collection.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
75
		if ( ! $request instanceof EE_Request ) {
76
			$request = \EE_Registry::instance()->load_core( 'Request' );
77
		}
78
		$this->request = $request;
0 ignored issues
show
Documentation Bug introduced by
It seems like $request can also be of type boolean. However, the property $request is declared as type object<EE_Request>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
79
	}
80
81
82
83
	/**
84
	 * @param string $display_strategy_name
85
	 * @throws InvalidDataTypeException
86
	 * @throws InvalidClassException
87
	 */
88
	protected function setDisplayStrategy( $display_strategy_name = 'number_bubbles' ) {
89
		if ( ! is_string( $display_strategy_name ) ) {
90
			throw new InvalidDataTypeException( '$display_strategy_name', $display_strategy_name, 'string' );
91
		}
92
		// build up FQCN from incoming display strategy folder name
93
		$display_strategy_class = 'EventEspresso\core\services\progress_steps\display_strategies\\';
94
		$display_strategy_class .= $display_strategy_name . '\\';
95
		$display_strategy_class .= str_replace( ' ', '', ucwords( str_replace( '_', ' ', $display_strategy_name ) ) );
96
		$display_strategy_class .= 'ProgressStepsDisplay';
97
		$display_strategy_class = apply_filters(
98
			'FHEE__ProgressStepManager__setDisplayStrategy__display_strategy_class',
99
			$display_strategy_class
100
		);
101
		if ( ! class_exists( $display_strategy_class ) ) {
102
			throw new InvalidClassException( $display_strategy_class );
103
		}
104
		$display_strategy = new $display_strategy_class();
105
		if ( ! $display_strategy instanceof ProgressStepsDisplayInterface ) {
106
			throw new InvalidClassException(
107
				$display_strategy_class,
108
				sprintf(
109
					__( 'The "%1$s" Class needs to be an implementation of the "%1$s" Interface.', 'event_espresso' ),
110
					$display_strategy_class,
111
					'\EventEspresso\core\services\progress_steps\display_strategies\ProgressStepsDisplayInterface'
112
				)
113
			);
114
		}
115
		$this->display_strategy = $display_strategy;
116
	}
117
118
119
120
	/**
121
	 * @param string $default_step
122
	 * @throws InvalidDataTypeException
123
	 */
124
	public function setDefaultStep( $default_step ) {
125
		if ( ! is_string( $default_step ) ) {
126
			throw new InvalidDataTypeException( '$default_step', $default_step, 'string' );
127
		}
128
		$this->default_step = $default_step;
129
	}
130
131
132
133
	/**
134
	 * @param string $step
135
	 * @throws \EventEspresso\Core\Exceptions\InvalidIdentifierException
136
	 */
137
	public function setCurrentStep( $step = '' ) {
138
		// use incoming value if it's set, otherwise use request param if it's set, otherwise use default
139
		$step = ! empty( $step )
140
			? $step
141
			: $this->request->get( 'ee-step', $this->default_step );
142
		// grab the step previously known as current, in case we need to revert
143
		$current_current_step = $this->collection->current();
0 ignored issues
show
Bug introduced by
The method current cannot be called on $this->collection (of type array<integer,object<Eve...ProgressStepInterface>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
144
		// verify that requested step exists
145
		if ( ! $this->collection->has( $step ) ) {
0 ignored issues
show
Bug introduced by
The method has cannot be called on $this->collection (of type array<integer,object<Eve...ProgressStepInterface>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
146
			throw new InvalidIdentifierException( $step, $this->default_step );
147
		}
148
		if ( $this->collection->setCurrent( $step ) ) {
0 ignored issues
show
Bug introduced by
The method setCurrent cannot be called on $this->collection (of type array<integer,object<Eve...ProgressStepInterface>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
149
			// if the old boss is the same as the new boss, then nothing changes
150
			if ( $this->collection->current() !== $current_current_step ) {
0 ignored issues
show
Bug introduced by
The method current cannot be called on $this->collection (of type array<integer,object<Eve...ProgressStepInterface>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
151
				$current_current_step->setIsCurrent( false );
152
			}
153
			$this->collection->current()->setIsCurrent();
0 ignored issues
show
Bug introduced by
The method current cannot be called on $this->collection (of type array<integer,object<Eve...ProgressStepInterface>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
154
		} else {
155
			$this->collection->setCurrent( $current_current_step->id() );
0 ignored issues
show
Bug introduced by
The method setCurrent cannot be called on $this->collection (of type array<integer,object<Eve...ProgressStepInterface>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
156
			$current_current_step->setIsCurrent( true );
157
		}
158
	}
159
160
161
162
	/**
163
	 * @return ProgressStepInterface
164
	 */
165
	public function currentStep() {
166
		return $this->collection->current();
0 ignored issues
show
Bug introduced by
The method current cannot be called on $this->collection (of type array<integer,object<Eve...ProgressStepInterface>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
167
	}
168
169
170
171
	/**
172
	 * @return ProgressStepInterface
173
	 */
174
	public function nextStep() {
175
		return $this->collection->next();
0 ignored issues
show
Bug introduced by
The method next cannot be called on $this->collection (of type array<integer,object<Eve...ProgressStepInterface>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
176
	}
177
178
179
180
	/**
181
	 * @return void
182
	 */
183
	public function enqueueStylesAndScripts() {
184
		$this->display_strategy->enqueueStylesAndScripts();
185
	}
186
187
188
189
	/**
190
	 * echos out HTML
191
	 *
192
	 * @return void
193
	 */
194
	public function displaySteps() {
195
		echo \EEH_Template::display_template(
196
			$this->display_strategy->getTemplate(),
197
			array( 'progress_steps' => $this->collection ),
198
			true
199
		);
200
	}
201
202
}
203
204
// End of file ProgressStepManager.php
205
// Location: core/services/progress_steps/ProgressStepManager.php