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

RApiHalHelperSiteContent::createContent()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 32
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 32
rs 9.2408
cc 5
nc 4
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 = (object) $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
			return json_encode($article->getProperties());
79
			
80
		}else {
81
			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

81
			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...
82
			return false;
83
		}
84
	}
85
	
86
	/**
87
	 * @throws Exception
88
	 */
89
	private function editContent($data): string
90
	{
91
		if (empty($data->article_id))
92
		{
93
			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...
94
		}
95
		
96
		$article = JTable::getInstance('content');
97
		$id      = $data->article_id;
98
		$imgSrc  = '';
99
		$article->load($id);
100
		$article->set('title', $data->title);
101
		
102
		if (isset($data->image) && $data->image)
103
		{
104
			$imgSrc     = $this->checkImages($data->image, $id);
105
		}
106
		
107
		$article->set('introtext', '<p>'.$data->description.'</p>' . $imgSrc);
108
		
109
		if (!$article->store()) {
110
			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

110
			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...
111
			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...
112
		}
113
		;
114
		return json_encode($article->getProperties());
115
	}
116
	
117
	
118
	private function checkImages($image, $articleId)
119
	{
120
		$baseImgs  = $image;
121
		$baseImgs  = json_decode($baseImgs);
122
		$imgSrc    = '';
123
		
124
		foreach ($baseImgs as $baseImg)
125
		{
126
			$imgSrc .= $this->processImages($baseImg, $articleId);
127
		}
128
		return $imgSrc;
129
	}
130
	
131
	private function processImages($imgBase64, $articleId)
132
	{
133
		$imageInfo  = explode(";base64,", $imgBase64);
134
		$imgExt     = str_replace('data:image/', '', $imageInfo[0]);
135
		$image      = str_replace(' ', '+', $imageInfo[1]);
136
		$md5Name    = md5($imgBase64);
137
		$imageName  = $md5Name.".".$imgExt;
138
		$path       = JPATH_BASE.'/images/aesir/'.$articleId.'/';
139
	
140
		if (!is_dir($path)) {
141
			mkdir($path,0777,true);
142
		}
143
		
144
		if (file_put_contents($path . $imageName, base64_decode($image)))
145
		{
146
			return '<p><img src="images/aesir/'.$articleId.'/'.$imageName.'" /></p>';
147
		}
148
	}
149
}
150
151