Completed
Push — master ( 8e9a24...5a1675 )
by Daniel
12s
created

ModelAsController::getNestedController()   D

Complexity

Conditions 9
Paths 65

Size

Total Lines 34
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 4.909
c 0
b 0
f 0
cc 9
eloc 16
nc 65
nop 0
1
<?php
2
3
use SilverStripe\ORM\DataModel;
4
use SilverStripe\ORM\DB;
5
use SilverStripe\ORM\DataObject;
6
7
/**
8
 * ModelAsController deals with mapping the initial request to the first {@link SiteTree}/{@link ContentController}
9
 * pair, which are then used to handle the request.
10
 *
11
 * @package cms
12
 * @subpackage control
13
 */
14
class ModelAsController extends Controller implements NestedController {
15
	private static $extensions = array('OldPageRedirector');
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
16
17
	/**
18
	 * Get the appropriate {@link ContentController} for handling a {@link SiteTree} object, link it to the object and
19
	 * return it.
20
	 *
21
	 * @param SiteTree $sitetree
22
	 * @param string $action
23
	 * @return ContentController
24
	 */
25
	public static function controller_for(SiteTree $sitetree, $action = null) {
26
		if ($sitetree->class == 'SiteTree') {
27
			$controller = "ContentController";
28
		} else {
29
			$ancestry = ClassInfo::ancestry($sitetree->class);
30
			while ($class = array_pop($ancestry)) {
31
				if (class_exists($class . "_Controller")) break;
32
			}
33
			$controller = ($class !== null) ? "{$class}_Controller" : "ContentController";
34
		}
35
36
		if($action && class_exists($controller . '_' . ucfirst($action))) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $action of type string|null is loosely compared to true; 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...
37
			$controller = $controller . '_' . ucfirst($action);
38
		}
39
40
		return class_exists($controller) ? Injector::inst()->create($controller, $sitetree) : $sitetree;
41
	}
42
43
	public function init() {
44
		singleton('SiteTree')->extend('modelascontrollerInit', $this);
45
		parent::init();
46
	}
47
48 View Code Duplication
	protected function beforeHandleRequest(SS_HTTPRequest $request, DataModel $model) {
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
		parent::beforeHandleRequest($request, $model);
50
		// If the database has not yet been created, redirect to the build page.
51
		if(!DB::is_active() || !ClassInfo::hasTable('SiteTree')) {
52
			$this->getResponse()->redirect(Controller::join_links(
53
				Director::absoluteBaseURL(),
54
				'dev/build',
55
				'?' . http_build_query(array(
56
					'returnURL' => isset($_GET['url']) ? $_GET['url'] : null,
57
				))
58
			));
59
		}
60
	}
61
62
	/**
63
	 * @uses ModelAsController::getNestedController()
64
	 * @param SS_HTTPRequest $request
65
	 * @param DataModel $model
66
	 * @return SS_HTTPResponse
67
	 */
68
	public function handleRequest(SS_HTTPRequest $request, DataModel $model) {
69
		$this->beforeHandleRequest($request, $model);
70
71
		// If we had a redirection or something, halt processing.
72
		if($this->getResponse()->isFinished()) {
73
			$this->popCurrent();
74
			return $this->getResponse();
75
		}
76
77
		// If the database has not yet been created, redirect to the build page.
78 View Code Duplication
		if(!DB::is_active() || !ClassInfo::hasTable('SiteTree')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
79
			$this->getResponse()->redirect(Director::absoluteBaseURL() . 'dev/build?returnURL=' . (isset($_GET['url']) ? urlencode($_GET['url']) : null));
80
			$this->popCurrent();
81
82
			return $this->getResponse();
83
		}
84
85
		try {
86
			$result = $this->getNestedController();
87
88
			if($result instanceof RequestHandler) {
89
				$result = $result->handleRequest($this->getRequest(), $model);
90
			} else if(!($result instanceof SS_HTTPResponse)) {
91
				user_error("ModelAsController::getNestedController() returned bad object type '" .
92
					get_class($result)."'", E_USER_WARNING);
93
			}
94
		} catch(SS_HTTPResponse_Exception $responseException) {
95
			$result = $responseException->getResponse();
96
		}
97
98
		$this->popCurrent();
99
		return $result;
100
	}
101
102
	/**
103
	 * @return ContentController
104
	 * @throws Exception If URLSegment not passed in as a request parameter.
105
	 */
106
	public function getNestedController() {
107
		$request = $this->getRequest();
108
109
		if(!$URLSegment = $request->param('URLSegment')) {
110
			throw new Exception('ModelAsController->getNestedController(): was not passed a URLSegment value.');
111
		}
112
113
		// Find page by link, regardless of current locale settings
114
		if(class_exists('Translatable')) Translatable::disable_locale_filter();
115
116
		// Select child page
117
		$conditions = array('"SiteTree"."URLSegment"' => rawurlencode($URLSegment));
118
		if(SiteTree::config()->nested_urls) {
119
			$conditions[] = array('"SiteTree"."ParentID"' => 0);
120
		}
121
		$sitetree = DataObject::get_one('SiteTree', $conditions);
122
123
		// Check translation module
124
		// @todo Refactor out module specific code
125
		if(class_exists('Translatable')) Translatable::enable_locale_filter();
126
127
		if(!$sitetree) {
128
			$this->httpError(404, 'The requested page could not be found.');
129
		}
130
131
		// Enforce current locale setting to the loaded SiteTree object
132
		if(class_exists('Translatable') && $sitetree->Locale) Translatable::set_current_locale($sitetree->Locale);
133
134
		if(isset($_REQUEST['debug'])) {
135
			Debug::message("Using record #$sitetree->ID of type $sitetree->class with link {$sitetree->Link()}");
136
		}
137
138
		return self::controller_for($sitetree, $this->getRequest()->param('Action'));
0 ignored issues
show
Compatibility introduced by
$sitetree of type object<SilverStripe\ORM\DataObject> is not a sub-type of object<SiteTree>. It seems like you assume a child class of the class SilverStripe\ORM\DataObject to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
139
	}
140
141
	/**
142
	 * @deprecated 4.0 Use OldPageRedirector::find_old_page instead
143
	 *
144
	 * @param string $URLSegment A subset of the url. i.e in /home/contact/ home and contact are URLSegment.
145
	 * @param int $parent The ID of the parent of the page the URLSegment belongs to.
146
	 * @param bool $ignoreNestedURLs
147
	 * @return SiteTree
148
	 */
149
	static public function find_old_page($URLSegment, $parent = null, $ignoreNestedURLs = false) {
0 ignored issues
show
Unused Code introduced by
The parameter $ignoreNestedURLs is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
150
		Deprecation::notice('4.0', 'Use OldPageRedirector::find_old_page instead');
151
		if ($parent) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $parent of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. 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 integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
152
			$parent = SiteTree::get()->byId($parent);
153
		}
154
		$url = OldPageRedirector::find_old_page(array($URLSegment), $parent);
0 ignored issues
show
Bug introduced by
It seems like $parent can also be of type integer or object<SilverStripe\ORM\DataObject>; however, OldPageRedirector::find_old_page() does only seem to accept object<SiteTree>|null, 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...
155
		return SiteTree::get_by_link($url);
0 ignored issues
show
Bug introduced by
It seems like $url defined by \OldPageRedirector::find...($URLSegment), $parent) on line 154 can also be of type boolean; however, SiteTree::get_by_link() does only seem to accept string, 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...
156
	}
157
}
158