PageController::activeAction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 13
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 13
loc 13
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
namespace Anax\Page;
4
 
5
/**
6
 * A controller for editable pages.
7
 *
8
 */
9
class PageController implements \Anax\DI\IInjectionAware
10
{
11
    use \Anax\DI\TInjectable,
12
        \Anax\MVC\TRedirectHelpers;
13
		
14
	public $page;
15
	
16
	/**
17
	 * Initialize the controller.
18
	 *
19
	 * @return void
20
	 */
21
	public function initialize()
22
	{
23
		$this->page = new \Anax\Page\Page();
24
		$this->page->setDI($this->di);
0 ignored issues
show
Bug introduced by
It seems like $this->di can also be of type array or null; however, Anax\DI\TInjectable::setDI() does only seem to accept object<Anax\DI\class>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
25
	}
26
	
27
	public function setupAction()
28
	{
29
		require('setup.php');
30
		
31
		$this->redirectTo('page');
32
	}
33
	
34
	public function indexAction()
35
	{
36
		$this->listAction();
37
	}
38
39
	
40
	/**
41
	 * List all pages.
42
	 *
43
	 * @return void
44
	 */
45 View Code Duplication
	public function listAction()
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...
46
	{
47
		$all = $this->page->findAll();
48
	 
49
		$this->theme->setTitle("List all pages");
0 ignored issues
show
Documentation introduced by
The property theme does not exist on object<Anax\Page\PageController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
50
		$this->views->add('page/list', [
0 ignored issues
show
Documentation introduced by
The property views does not exist on object<Anax\Page\PageController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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
			'pages' => $all,
52
			'title' => "Editable pages",
53
		]);
54
	}
55
	
56
	/**
57
	 * Views page with with slug.
58
	 *
59
	 * @param int $slug of page to display
60
	 *
61
	 * @return void
62
	 */
63 View Code Duplication
	public function viewAction($slug = null)
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...
64
	{
65
		$pages = $this->page->findWhere('slug', $slug);
66
		
67
		if (empty($pages)) {
68
			die('No such page!');
69
		}
70
		
71
		$page = $pages[0];
72
		
73
		$this->theme->setTitle($page->title);
0 ignored issues
show
Documentation introduced by
The property theme does not exist on object<Anax\Page\PageController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
74
		$this->views->add('page/view', [
0 ignored issues
show
Documentation introduced by
The property views does not exist on object<Anax\Page\PageController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
75
			'page' => $page,
76
		]);
77
	}
78
	
79
	/**
80
	 * Add new page.
81
	 *
82
	 *
83
	 * @return void
84
	 */
85 View Code Duplication
	public function addAction()
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...
86
	{
87
		
88
		$form = new \Anax\Page\CFormAddPage();
89
        $form->setDI($this->di);
0 ignored issues
show
Bug introduced by
It seems like $this->di can also be of type array or null; however, Anax\DI\TInjectionAware::setDI() does only seem to accept object<Anax\DI\class>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
90
		
91
		// Check the status of the form
92
        $form->check();
93
	 
94
		$this->di->theme->setTitle("Add page");
95
        $this->di->views->add('default/page', [
96
            'title' => "Add a page",
97
            'content' => $form->getHTML()
98
        ]);
99
	}
100
	
101
	/**
102
	 * Edit a page.
103
	 *
104
	 * @param string $id of page to edit.
105
	 *
106
	 * @return void
107
	 */
108 View Code Duplication
	public function updateAction($id = null)
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...
109
	{
110
		
111
		if (!$id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $id of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
112
			$this->redirectTo('page');
113
		}
114
		
115
		$page = $this->page->find($id);
116
		
117
		$form = new \Anax\Page\CFormUpdatePage($page);
118
        $form->setDI($this->di);
0 ignored issues
show
Bug introduced by
It seems like $this->di can also be of type array or null; however, Anax\DI\TInjectionAware::setDI() does only seem to accept object<Anax\DI\class>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
119
		
120
		// Check the status of the form
121
        $form->check();
122
	 
123
		$this->di->theme->setTitle("Update page");
124
        $this->di->views->add('default/page', [
125
            'title' => "Update a page",
126
            'content' => $form->getHTML()
127
        ]);
128
	}
129
	
130
	/**
131
	 * Delete page.
132
	 *
133
	 * @param integer $id of page to delete.
134
	 *
135
	 * @return void
136
	 */
137
	public function deleteAction($id = null)
138
	{
139
		if (!isset($id)) {
140
			die("Missing id");
141
		}
142
	 
143
		$res = $this->page->delete($id);
0 ignored issues
show
Unused Code introduced by
$res is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
144
	 
145
		$this->redirectTo('page');
146
	}
147
	
148
	/**
149
	 * Delete (soft) page.
150
	 *
151
	 * @param integer $id of page to delete.
152
	 *
153
	 * @return void
154
	 */
155 View Code Duplication
	public function softDeleteAction($id = null)
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...
156
	{
157
		if (!isset($id)) {
158
			die("Missing id");
159
		}
160
	 
161
		$now = gmdate('Y-m-d H:i:s');
162
	 
163
		$page = $this->page->find($id);
164
	 
165
		$page->deleted = $now;
166
		$page->save();
167
	
168
		$this->redirectTo('page/trash');
169
	}
170
	
171
	/**
172
	 * Restore (soft) deleted page.
173
	 *
174
	 * @param integer $id of page to restore.
175
	 *
176
	 * @return void
177
	 */
178 View Code Duplication
	public function restoreAction($id = null)
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...
179
	{
180
		if (!isset($id)) {
181
			die("Missing id");
182
		}
183
	 
184
		$page = $this->page->find($id);
185
	 
186
		$page->deleted = null;
187
		$page->save();
188
	 
189
		$this->redirectTo('page');
190
	}
191
	
192
	/**
193
	 * Activate page.
194
	 *
195
	 * @param integer $id of page to activate.
196
	 *
197
	 * @return void
198
	 */
199
	public function activateAction($id = null)
200
	{
201
		if (!isset($id)) {
202
			die("Missing id");
203
		}
204
205
		$page = $this->page->find($id);
206
	 
207
		$page->inactivated =  null;
208
		$page->save();
209
	 
210
		$this->redirectTo('page');
211
	}
212
	
213
	/**
214
	 * Inactivate page.
215
	 *
216
	 * @param integer $id of page to inactivate.
217
	 *
218
	 * @return void
219
	 */
220 View Code Duplication
	public function inactivateAction($id = null)
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...
221
	{
222
		if (!isset($id)) {
223
			die("Missing id");
224
		}
225
		
226
		$now = gmdate('Y-m-d H:i:s');
227
	 
228
		$page = $this->page->find($id);
229
	 
230
		$page->inactivated = $now;
231
		$page->save();
232
	 
233
		$this->redirectTo('page/inactive');
234
	}
235
	
236
	/**
237
	 * List all active and not deleted pages.
238
	 *
239
	 * @return void
240
	 */
241 View Code Duplication
	public function activeAction()
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...
242
	{
243
		$all = $this->page->query()
244
			->where('inactivated IS NULL')
245
			->andWhere('deleted is NULL')
246
			->execute();
247
	 
248
		$this->theme->setTitle("Pages that are active");
0 ignored issues
show
Documentation introduced by
The property theme does not exist on object<Anax\Page\PageController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
249
		$this->views->add('page/active', [
0 ignored issues
show
Documentation introduced by
The property views does not exist on object<Anax\Page\PageController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
250
			'pages' => $all,
251
			'title' => "Pages that are active",
252
		]);
253
	}
254
	
255
	/**
256
	 * List all inactive pages.
257
	 *
258
	 * @return void
259
	 */
260 View Code Duplication
	public function inactiveAction()
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...
261
	{
262
		$all = $this->page->query()
263
			->where('inactivated IS NOT NULL')
264
			->execute();
265
	 
266
		$this->theme->setTitle("Pages that are inactive");
0 ignored issues
show
Documentation introduced by
The property theme does not exist on object<Anax\Page\PageController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
267
		$this->views->add('page/inactive', [
0 ignored issues
show
Documentation introduced by
The property views does not exist on object<Anax\Page\PageController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
268
			'pages' => $all,
269
			'title' => "Pages that are inactive",
270
		]);
271
	}
272
	
273
	/**
274
	 * List all deleted pages.
275
	 *
276
	 * @return void
277
	 */
278 View Code Duplication
	public function trashAction()
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...
279
	{
280
		$all = $this->page->query()
281
			->where('deleted IS NOT NULL')
282
			->execute();
283
	 
284
		$this->theme->setTitle("Pages that are deleted");
0 ignored issues
show
Documentation introduced by
The property theme does not exist on object<Anax\Page\PageController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
285
		$this->views->add('page/deleted', [
0 ignored issues
show
Documentation introduced by
The property views does not exist on object<Anax\Page\PageController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
286
			'pages' => $all,
287
			'title' => "Pages that are deleted",
288
		]);
289
	}
290
	 
291
}