Passed
Push — develop ( 84f32e...989890 )
by
unknown
15:42 queued 41s
created

CmsUrlRule::getUrlByNiceId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
namespace neon\cms\components;
4
5
use neon\cms\models\CmsVisit;
0 ignored issues
show
Bug introduced by
The type neon\cms\models\CmsVisit was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use neon\core\web\Request;
7
use neon\core\web\Response;
8
use yii\web\UrlManager;
9
use yii\web\UrlRuleInterface;
10
use \neon\cms\models\CmsUrl;
11
use yii\base\BaseObject;
12
13
class CmsUrlRule extends BaseObject implements UrlRuleInterface
14
{
15
	/**
16
	 * The controller action route where matching rules will be sent
17
	 * @var string
18
	 */
19
	public $cmsRoute = 'cms/render/page';
20
21
	/**
22
	 * Parses the given request and returns the corresponding route and parameters.
23
	 * Parse the incoming request url to see if it is a CMS route - we test for an exact url path match
24
	 *
25
	 * @param UrlManager $manager the URL manager
26
	 * @param Request $request the request component
27
	 * @throws \Exception - if query goes wrong.
28
	 * @return array|bool the parsing result. The route and the parameters are returned as an array.
29
	 * If false, it means this rule cannot be used to parse this path info.
30
	 */
31
	public function parseRequest($manager, $request)
32
	{
33
		$pathInfo = '/' . $request->getPathInfo();
34
		$url = neon()->cache->getOrSet($pathInfo, function() use ($pathInfo) {
35
			return CmsUrl::find()->where(['url' => $pathInfo])->limit(1)->one();
36
		});
37
		// if the url is not in the url table then return false to continue processing other route rules
38
		// typically in a neon setup the module/controller/action rule
39
		if (! $url) return false;
40
		// check if the url found represents a redirect
41
		if ($url['redirect'] !== null) {
42
			neon()->response->redirect($url['redirect'], 301)->send();
43
			// CmsVisit::add();
44
			exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
45
		}
46
		return [$this->cmsRoute, ['nice_id' => $url['nice_id'], 'slug' => $pathInfo]];
47
	}
48
49
	/**
50
	 * Creates a URL according to the given route and parameters.
51
	 *
52
	 * Convert a cms page route to its pretty url for e.g.:
53
	 * a route of: `['/cms/render/page', 'nice_id' => 'my_nice_id']`
54
	 * will return the pretty url as defined against the nice_id in the url table
55
	 *
56
	 * @param UrlManager $manager the URL manager
57
	 * @param string $route the route. It should not have slashes at the beginning or the end.
58
	 * @param array $params the parameters
59
	 * @return string|bool the created URL, or false if this rule cannot be used for creating this URL.
60
	 */
61 2
	public function createUrl($manager, $route, $params)
62
	{
63
		// only run this if trying to access a cms controller route otherwise
64
		// this would be called for every url call.
65 2
		if ($route === $this->cmsRoute && isset($params['nice_id'])) {
66
			// enables the canonical lookup to work when only the request data is available
67 2
			if (isset($params['slug']) && !empty($params['slug'])) {
68
				return $params['slug'];
69
			}
70
			// without a provided slug asking yii to translate ['cms/render/page', 'nice_id'=>'blog']
71
			// will lookup the correct url for the blog page.
72 2
			return $this->getUrlByNiceId($params['nice_id']);
73
		}
74 2
		return false;
75
	}
76
77
	/**
78
	 * Get the url details based on nice_id - assumes routes for /cms/render/page
79
	 *
80
	 * @param string $niceId
81
	 * @return array|false
82
	 */
83 2
	public function getUrlByNiceId($niceId)
84
	{
85 2
		return CmsUrl::getUrlByNiceId($niceId);
86
	}
87
}
88