Passed
Pull Request — develop (#924)
by
unknown
04:19
created

RApiHalHelperSiteContent::editContent()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 26
rs 9.4888
cc 5
nc 5
nop 1
1
<?php
2
/**
3
 * @package     Redcore
4
 * @subpackage  Api
5
 *
6
 * @copyright   Copyright (C) 2008 - 2021 redWEB.dk. All rights reserved.
7
 * @license     GNU General Public License version 2 or later, see LICENSE.
8
 */
9
10
defined('JPATH_BASE') or die;
11
12
/**
13
 * Api Helper class for overriding default methods
14
 *
15
 * @package     Redcore
16
 * @subpackage  Api Helper
17
 * @since       1.8
18
 */
19
class RApiHalHelperSiteContent
20
{
21
	/**
22
	 * Service for creating content.
23
	 *
24
	 * @param string $data content
25
	 *
26
	 * @return  boolean         True on success. False otherwise.
27
	 * @throws Exception
28
	 */
29
	public function save($data): string
30
	{
31
		if (version_compare(JVERSION, '3.0', 'lt')) {
32
			JTable::addIncludePath(JPATH_PLATFORM . 'joomla/database/table');
33
		}
34
		
35
		$data = (array) $data;
36
		
37
		if (isset($data['job_type']) && $data['job_type'] == 'create')
38
		{
39
			return $this->createContent($data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->createContent($data) returns the type string which is incompatible with the documented return type boolean.
Loading history...
40
		}
41
		
42
		if (isset($data['job_type']) && $data['job_type'] == 'edit')
43
		{
44
			return $this->editContent($data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->editContent($data) returns the type string which is incompatible with the documented return type boolean.
Loading history...
45
		}
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 42 is false. This is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
46
	}
47
	
48
	/**
49
	 * @throws Exception
50
	 */
51
	private function createContent($data): string
52
	{
53
		$article = JTable::getInstance('content');
54
		$article->title            = $data['title'];
0 ignored issues
show
Bug Best Practice introduced by
The property title does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
55
		$article->alias            = JFilterOutput::stringURLSafe($data['title']);
56
		$article->introtext        = '<p>'.$data['description'].'</p>';
57
		$article->created          = JFactory::getDate()->toSQL();;
58
		$article->created_by_alias = $data['user'];
0 ignored issues
show
Bug introduced by
The property created_by_alias does not seem to exist on JTable.
Loading history...
59
		$article->state            = 1;
60
		$article->access           = 1;
0 ignored issues
show
Bug Best Practice introduced by
The property access does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
61
		$article->metadata         = '{"page_title":"'.$data['title'].'","author":"'.$data['user'].'","robots":""}';
62
		$article->language         = '*';
0 ignored issues
show
Bug introduced by
The property language does not seem to exist on JTable.
Loading history...
63
		
64
		if (!$article->check()) {
65
			throw new Exception($article->getError());
0 ignored issues
show
Deprecated Code introduced by
The function JObject::getError() has been deprecated: 12.3 JError has been deprecated ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

65
			throw new Exception(/** @scrutinizer ignore-deprecated */ $article->getError());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
66
			return false;
0 ignored issues
show
Unused Code introduced by
return false is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
67
		}
68
		
69
		if ($article->store(TRUE)) {
70
			
71
			if (isset($data['image']) && $data['image'])
72
			{
73
				$articleId  = $article->get('id');
74
				$imgSrc     = $this->checkImages($data['image'], $articleId);
75
				$article->set('introtext', '<p>'.$data['description'].'</p>' . $imgSrc);
76
				$article->store();
77
			}
78
			
79
			return json_encode($article->getProperties());
80
			
81
		}else {
82
			throw new Exception($article->getError());
0 ignored issues
show
Deprecated Code introduced by
The function JObject::getError() has been deprecated: 12.3 JError has been deprecated ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

82
			throw new Exception(/** @scrutinizer ignore-deprecated */ $article->getError());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
83
			return false;
84
		}
85
	}
86
	
87
	/**
88
	 * @throws Exception
89
	 */
90
	private function editContent($data): string
91
	{
92
		if (empty($data['article_id']))
93
		{
94
			return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the type-hinted return string.
Loading history...
95
		}
96
		
97
		$article = JTable::getInstance('content');
98
		$id      = $data['article_id'];
99
		$imgSrc  = '';
100
		$article->load($id);
101
		$article->set('title', $data['title']);
102
		
103
		if (isset($data['image']) && $data['image'])
104
		{
105
			$imgSrc = $this->checkImages($data['image'], $id);
106
		}
107
108
		$article->set('introtext', '<p>'.$data['description'].'</p>' . $imgSrc);
109
		
110
		if (!$article->store()) {
111
			throw new Exception($article->getError());
0 ignored issues
show
Deprecated Code introduced by
The function JObject::getError() has been deprecated: 12.3 JError has been deprecated ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

111
			throw new Exception(/** @scrutinizer ignore-deprecated */ $article->getError());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
112
			return false;
0 ignored issues
show
Unused Code introduced by
return false is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
113
		}
114
		
115
		return json_encode($article->getProperties());
116
	}
117
	
118
	
119
	private function checkImages($images, $articleId)
120
	{
121
		$imgSrc    = '';
122
		$extTypes  = ['gif','jpg','jpe','jpeg','png'];
123
		
124
		foreach ($images as $image)
125
		{
126
			$typeImg = strtolower(explode('/', $image['type'])[1]);
127
	
128
			if (!in_array($typeImg, $extTypes) || $image['error'] != 0)
129
			{
130
				continue;
131
			}
132
133
			$imgSrc .= $this->processImages($image, $articleId);
134
		}
135
		
136
		return $imgSrc;
137
	}
138
	
139
	private function processImages($image, $articleId)
140
	{
141
		$path      = JPATH_BASE.'/images/aesir/'.$articleId.'/';
142
		$pathFile  = '/images/aesir/'.$articleId.'/'.$image['name'];
143
	
144
		if (!is_dir($path)) {
145
			mkdir($path,0777,true);
146
		}
147
148
		if (move_uploaded_file($image['tmp_name'], $path.$image['name']))
149
		{
150
			return '<p><img src="'.$pathFile.'" /></p>';
151
		}
152
	}
153
}
154
155