Test Setup Failed
Push — steve-cms-pages ( e7168d...272019 )
by steve
33:31 queued 19:33
created

CmsUrl::getPagesIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * @link http://www.newicon.net/neon
4
 * @copyright Copyright (c) 2008 Newicon Ltd
5
 * @license http://www.newicon.net/neon/license/
6
 */
7
8
9
10
namespace neon\cms\models;
11
12
use neon\core\db\ActiveRecord;
13
use yii\caching\CacheInterface;
14
15
/**
16
 * CmsPage model
17
 *
18
 * The idea is that each url has a dedicated page.  A page then has content attached to it.
19
 * For example a page might show a an ecommerce product, a blog post and an author, each of these
20
 * items needs to be attached to the page.
21
 *
22
 * You could imagine a product manager for example would have to add a product and under the hood
23
 * configure a page record that shows the product.  This allows you to set custom page type things
24
 * and I think gives maximum flexibility, for example different templates for each page if you really
25
 * want.   However another approach could be to make a generic routes lookup table that defers everything
26
 * to the routing.
27
 *
28
 * @property string $nice_id - the nice_id of the page this url points to
29
 * @property string $url - the relative url on the site
30
 * @property string $redirect - a url to redirect to
31
 * @property int $hits - a number representing the number of times this url has been followed
32
 */
33
class CmsUrl extends ActiveRecord
34
{
35
	/**
36
	 * @inheritdoc
37
	 */
38 28
	public static function tableName()
39
	{
40 28
		return '{{%cms_url}}';
41
	}
42
43
	/**
44
	 * @inheritdoc
45
	 */
46 28
	public function rules()
47
	{
48
		return [
49 28
			['url', 'unique'],
50
			[['nice_id', 'url', 'redirect'], 'safe']
51
		];
52
	}
53
54
	/**
55
	 * Update a url
56
	 * Specify whether a 301 redirect should be created for the old url by setting $createRedirect to true
57
	 *
58
	 * @param string $newUrl - the new url value
59
	 * @param boolean $createRedirect - whether to create a redirect
60
	 * @throws \yii\web\HttpException - if $newUrl already exists
61
	 * @return boolean whether new url was saved
62
	 */
63
	public function updateUrl($newUrl, $createRedirect)
64
	{
65
		if ($newUrl !== $this->url) {
66
			$oldUrl = $this->url;
67
			// lets sanity check here - make sure the new url does not already exist:
68
			abort_if(CmsUrl::find()->where(['url' => $newUrl])->exists(), 400, 'The new url must not already exist');
69
			$this->url = $newUrl;
70
			$saved = $this->save();
71
			// do the redirect:
72
			if ($createRedirect) {
73
				static::createRedirect($oldUrl, $newUrl);
74
			}
75
			return $saved;
76
		}
77
		return false;
78
	}
79
80
	/**
81
	 * Create a redirect
82
	 *
83
	 * @param string $fromUrl
84
	 * @param string $toUrl
85
	 * @return CmsUrl - the new CmsUrl record created
86
	 */
87
	public static function createRedirect($fromUrl, $toUrl)
88
	{
89
		$r = new CmsUrl();
90
		$r->url = $fromUrl;
91
		// this must be null to prevent page link lookups (on nice_id) returning redirects
92
		$r->nice_id = null;
93
		$r->redirect = $toUrl;
94
		$r->hits = 0;
95
		$r->save();
96
		return $r;
97
	}
98
99
	/**
100
	 * Returns the url for this niceId or false if not found
101
	 *
102
	 * @param string $niceId
103
	 * @return bool|string
104
	 */
105
	public static function getUrlByNiceId($niceId)
106
	{
107
		$urls = static::getAllUrlsByNiceId();
108
		$url = $urls[$niceId]['url'] ?? false;
109
		// if we can not find the url we try the $niceId as the page $id parameter of the pages table
110
		if ($url === false) {
111
			$pages = static::getPagesIndex();
112
			$pageNiceId = $pages[$niceId] ?? false;
113
			$url = $urls[$pageNiceId]['url'] ?? false;
114 2
		}
115
		return $url;
116 2
	}
117 2
118
	/**
119
	 * Gets all urls indexed by nice_id
120
	 *
121
	 * @return array
122
	 */
123
	public static function getAllUrlsByNiceId()
124
	{
125
		return static::cache()->getOrSet('cms_url', function() {
126
			return collect(CmsUrl::find()->asArray()->all())
127 2
				->indexBy('nice_id')
128 2
				->all();
129 2
		});
130 2
	}
131 2
132
	public static function getPagesIndex()
133
	{
134
		return static::cache()->getOrSet('cms_url_pages', function() {
135
			return collect(CmsPage::find()->select('id, nice_id')->asArray()->all())
136
				->flatMap(function($i){ return [$i['id'] => $i['nice_id']]; })
137 28
				->all();
138
		});
139 28
	}
140 28
141 28
	/**
142
	 * @inheritDoc
143
	 */
144
	public function afterSave($insert, $changedAttributes)
145
	{
146
		parent::afterSave($insert, $changedAttributes);
147
		static::cache()->delete('cms_url');
148 28
	}
149
150 28
	/**
151
	 * Get the cache component to use for url caching
152
	 *
153
	 * @return \yii\caching\CacheInterface
154
	 */
155
	public static function cache()
156
	{
157
		return neon()->isDevMode() ? neon()->cacheArray : neon()->cache;
158
	}
159
}
160