Passed
Pull Request — develop (#912)
by Shandak
05:28
created

ContentHistory::createObserver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 1
nc 1
nop 2
1
<?php
2
/**
3
 * Joomla! Content Management System
4
 *
5
 * @copyright  Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
6
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
7
 */
8
9
namespace Joomla\CMS\Table\Observer;
10
11
use Joomla\CMS\Helper\ContentHistoryHelper;
12
13
defined('JPATH_PLATFORM') or die;
14
15
/**
16
 * Table class supporting modified pre-order tree traversal behavior.
17
 *
18
 * @since  3.2
19
 */
20
class ContentHistory extends AbstractObserver
21
{
22
	/**
23
	 * Helper object for storing and deleting version history information associated with this table observer
24
	 *
25
	 * @var    ContentHistoryHelper
26
	 * @since  3.2
27
	 */
28
	protected $contenthistoryHelper;
29
30
	/**
31
	 * The pattern for this table's TypeAlias
32
	 *
33
	 * @var    string
34
	 * @since  3.2
35
	 */
36
	protected $typeAliasPattern = null;
37
38
	/**
39
	 * Not public, so marking private and deprecated, but needed internally in parseTypeAlias for
40
	 * PHP < 5.4.0 as it's not passing context $this to closure function.
41
	 *
42
	 * @var         ContentHistory
43
	 * @since       3.2
44
	 * @deprecated  Never use this
45
	 * @private
46
	 */
47
	public static $_myTableForPregreplaceOnly;
48
49
	/**
50
	 * Creates the associated observer instance and attaches it to the $observableObject
51
	 * Creates the associated content history helper class instance
52
	 * $typeAlias can be of the form "{variableName}.type", automatically replacing {variableName} with table-instance variables variableName
53
	 *
54
	 * @param   \JObservableInterface  $observableObject  The subject object to be observed
55
	 * @param   array                  $params            ( 'typeAlias' => $typeAlias )
56
	 *
57
	 * @return  ContentHistory
58
	 *
59
	 * @since   3.2
60
	 */
61
	public static function createObserver(\JObservableInterface $observableObject, $params = array())
62
	{
63
		$typeAlias = $params['typeAlias'];
64
65
		$observer = new self($observableObject);
66
67
		$observer->contenthistoryHelper = new ContentHistoryHelper($typeAlias);
68
		$observer->typeAliasPattern = $typeAlias;
69
70
		return $observer;
71
	}
72
73
	/**
74
	 * Post-processor for $table->store($updateNulls)
75
	 *
76
	 * @param   boolean  &$result  The result of the load
77
	 *
78
	 * @return  void
79
	 *
80
	 * @since   3.2
81
	 */
82
	public function onAfterStore(&$result)
83
	{
84
		if ($result)
85
		{
86
			$this->parseTypeAlias();
87
			$aliasParts = explode('.', $this->contenthistoryHelper->typeAlias);
88
89
			if (\JComponentHelper::getParams($aliasParts[0])->get('save_history', 0))
90
			{
91
				$this->contenthistoryHelper->store($this->table);
92
			}
93
		}
94
	}
95
96
	/**
97
	 * Pre-processor for $table->delete($pk)
98
	 *
99
	 * @param   mixed  $pk  An optional primary key value to delete.  If not set the instance property value is used.
100
	 *
101
	 * @return  void
102
	 *
103
	 * @since   3.2
104
	 * @throws  \UnexpectedValueException
105
	 */
106
	public function onBeforeDelete($pk)
107
	{
108
		$this->parseTypeAlias();
109
		$aliasParts = explode('.', $this->contenthistoryHelper->typeAlias);
110
111
		if (\JComponentHelper::getParams($aliasParts[0])->get('save_history', 0))
112
		{
113
			$this->parseTypeAlias();
114
			$this->contenthistoryHelper->deleteHistory($this->table);
115
		}
116
	}
117
118
	/**
119
	 * Internal method
120
	 * Parses a TypeAlias of the form "{variableName}.type", replacing {variableName} with table-instance variables variableName
121
	 * Storing result into $this->contenthistoryHelper->typeAlias
122
	 *
123
	 * @return  void
124
	 *
125
	 * @since   3.2
126
	 */
127
	protected function parseTypeAlias()
128
	{
129
		// Needed for PHP < 5.4.0 as it's not passing context $this to closure function
130
		static::$_myTableForPregreplaceOnly = $this->table;
131
132
		$this->contenthistoryHelper->typeAlias = preg_replace_callback('/{([^}]+)}/',
133
			function($matches)
134
			{
135
				return ContentHistory::$_myTableForPregreplaceOnly->{$matches[1]};
136
			},
137
			$this->typeAliasPattern
138
		);
139
	}
140
}
141